《大话SecureCRT脚本》之批量设备维护工具(startGo.exe)

本文介绍了一个名为startGo.exe的工具,该工具能够读取IP参数并启动SecureCRT,简化了批量操作网络设备的过程。通过双击startGo.exe,用户可以便捷地进行设备维护工作。

读取IP参数和启动secureCRT的代码,达到只需要双击startGo.exe文件,即自动开始批量操作设备。

功能:读取IP参数和启动secureCRT的代码。

源码:startGo.exe文件

# -*- coding:gb2312 -*-
import time
import logging
import shutil
import random
import os
import sys
import subprocess

import pythoncom
#get username
import getpass

#get pid, 2016/8/9 18:45:00
import psutil
import string

#get new testLog, 2016/8/10 14:51:29
import datetime

#打印异常,2016/10/18
import traceback

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#作用-读取文件到列表。使用方法:arrYear = ReadArrayFromFile("c:\years.txt")
#fileName-文件名
#返回数组列表
def ReadArrayFromFile(fileName):
  try:
    linelist=[]
    with open(fileName,'r') as f:
      for line in f.readlines():
          linestr = line.strip() #删除字符串开头、结尾空格,也可指定待删除字符
          linelist.append(linestr)

          #linestrlist = linestr.split("\n")
          #linelist = map(int,linestrlist)# 方法一
      f.close()
      return linelist  #返回列表
  except Exception, e:
    exstr = traceback.format_exc()
    print( "ReadArrayFromFile(), error!" + '\n' + exstr)
    pass  #空语句 do nothing
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#def main():
if __name__ == "__main__":
 try:
  #获取当前的路径
  currentPath = os.path.dirname(os.path.realpath(sys.argv[0] )) +'\\' #类似D:/jenkins/xx/
  if not os.path.exists(currentPath):
    print('程序退出,根路径不存在:' + currentPath)
    sys.exit(1)  #currentPath路径不存在:退出
  
  fileName=currentPath +"IpSeting.txt" #参数文件
  if not os.path.exists(fileName):
    print('IpSeting.txt参数文件不存在,程序退出!' + currentPath)
    sys.exit(1)  #currentPath路径不存在:退出
    
  with open(fileName,'r') as f: #获取secureCRT安装路径和待运行的软件名
      for line in f.readlines():
          linestr = line.rstrip() #删除字符串开头、结尾空格,也可指定待删除字符
          if "securecrtPath=" in linestr:   #1, secureCRT安装路径
             securecrtPath=linestr.split("=")[1]
             print("secureCRT安装路径--" +securecrtPath)
          if "softName=" in linestr:   #2, 设备SSH登录IP
             softName=currentPath +linestr.split("=")[1]
             print("待运行的软件--" +softName)
      f.close()
  
  if False == "SecureCRT.exe" in linestr:   #验证1, secureCRT设置
        print("在IpSeting.txt文件中CRT路径设置错误,程序退出!" +securecrtPath)
  
  if not os.path.exists(softName): #验证2, softName设置
    print("在IpSeting.txt文件设置的vbs路径不存在,程序退出!" +softName)
    sys.exit(1)  #softName路径不存在:退出
  
  softRun=securecrtPath +" /SCRIPT " + softName #防路径空格,程序位置与名称
  #os.popen(softRun).read() #阻塞执行exe程序,不能处理空格
  #retcode = subprocess.call(softRun) #阻塞执行exe程序
  subprocess.Popen(softRun) #非阻塞
  
  print("程序启动")
  
  #time.sleep(1)
  sys.exit(0)
 except Exception, e:
    exstr = traceback.format_exc()
    print( "startCheck.exe, error!" + '\n' + exstr)
    pass  #空语句 do nothing

参数设置文件IpSeting.txt:

#securecrtPath: 设置secureCRT程序路径,如"D:\SecureCRT\SecureCRT.exe "
#softName:执行的vbs操作程序
#username/userpwd: 设备用户名/密码
#ip: 需登录设备的ip地址:如"10.78.20.1",可添加多个
#说明:“=”是判断关键信息的分隔符,分割线以上部分绝不可出现
#``````````````````````````````````````````````````````````````
securecrtPath=D:\Securecrt\SecureCRT.exe
softName=doCheck.vbs
username=root
userpwd=root
ip=173.16.1.1
ip=192.168.2.1

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值