网络设备信息采集脚本
在常规测试中,经常需要在测前、测后抓取大量信息留存,不同厂商命令又不一样,黏贴复制采集工作量巨大,效率又低,所以需要写一个自动化脚本帮助自动化采集。自动化脚本有很多种方法编写,这里主要使用简单易用的Python,由于使用的是SecureCRT,所以直接选择了Python与SecureCRT的组合自动化脚本,其实之前写过一些,不过此篇用到了多个Session;
- Python独立自动化脚本,可以参考之前的文章:
- Python 与SecureCRT组合的自动化脚本,可以参考之前的文章:
SecureCRT最开始支持VB脚本,后来逐步转到Python。官网也有很多example可以供参考,如果感兴趣可以参考:
https://www.vandyke.com/support/securecrt/python_examples.html
脚本功能
此自动化脚本可以复用,根据需求任意更改,简单上手;另外Juniper带“table”的命令会扰乱SecureCRT的脚本,更改后恢复正常,代码正文中的是最终版本,同时也去掉了等待时间;其实脚本还可以优化下,但为了简单点,就写了10多个函数;
- 指定路径并按照“日_时分”格式自动创建文件夹;
- SecureCRT自动登录14台路由设备,并开启记录log功能;
- 采集14台设备信息,自动保存log,以“年-月-日-时-分”命名log文件;
- 采集后自动关闭Session;
show route yyyyyyyy extensive table C-CT |no-more\r
代码模块
Python部分
- 获取时间并赋予文件名及文件夹名称;
- 通过系统函数自动创建文件夹;
- 使用time函数等待特定时间;
- while循环函数使用;
SecureCRT部分
- 通过objTab开启多个Session;
- 记录log功能;
- 发送命令函数;
- 等待结果函数;
- 关闭Tab会话;
下面是常用的一些Tab函数:
crt.Session.ConnectInTab()
crt.GetTabCount()
crt.GetTab()
objTab.Activate()
objTab.Clone()
objTab.Close()
objTab.ConnectSftp()
objTab.Index()
objTab.Caption()
objTab.Session.Disconnect()
objTab.Session.Connected()
objTab.Screen.Send()
使用方法
打开SecureCRT -> 选择Script -> 选择Run -> 选择 “xxx.py” 脚本 -> 可以去干别的了~
代码正文
import time
import datetime
import os
NOW = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M')
I = datetime.datetime.now()
FOLDERTIME= str(I.day) + "_" + str(I.hour) + str(I.minute)
DIR = "C:\\info\\"
os.mkdir(DIR + FOLDERTIME)
########## CISCO CMD LIST ##########
cisco_cmd_list = [
"xxxxxxxx\r",
"xxxxxxxx\r"
]
cisco_cmd_list_region_a = [
"xxxxxxxx\r",
"xxxxxxxx\r"
]
cisco_cmd_list_region_b = [
"xxxxxxxx\r",
"xxxxxxxx\r"
]
########## Juniper CMD LIST ##########
juniper_cmd_list = [
"yyyyyyyy\r",
"yyyyyyyy\r"
]
juniper_cmd_list_region_a = [
"yyyyyyyy\r",
"yyyyyyyy\r"
]
juniper_cmd_list_region_c = [
"yyyyyyyy\r",
"yyyyyyyy\r"
]
########## HuaWei CMD LIST ##########
huawei_cmd_list = [
"zzzzzzzz\r",
"zzzzzzzz\r"
]
huawei_cmd_list_region_b = [
"zzzzzzzz\r",
"zzzzzzzz\r"
]
huawei_cmd_list_region_c = [
"zzzzzzzz\r",
"zzzzzzzz\r"
]
########## Juniper RR CMD LIST ##########
juniper_rr_cmd_list = [
"yyyyyyyy\r",
"yyyyyyyy\r"
]
########### function: capture cmd at PR11 ##########
def PR11():
objTab1 = crt.Session.ConnectInTab("/telnet x.x.x.x")
objTab1.Caption = "PR11"
objTab1.Activate
objTab1.Session.Log(False)
objTab1.Session.LogFileName = DIR + FOLDERTIME + "\\" + "PR11-" + NOW + ".txt"
objTab1.Session.Log(True)
objTab1.Screen.Synchronous = True
if (objTab1.Screen.WaitForString("Username:",3) == True):
objTab1.Screen.Send("pdd\r")
if (objTab1.Screen.WaitForString("Password:",3) == True):
objTab1.Screen.Send("pdd123\r")
objTab1.Screen.WaitForString("RP/0/RSP0/CPU0:PR11#")
x = 0
while x < 46:
objTab1.Screen.Send(cisco_cmd_list[x])
objTab1.Screen.WaitForString("RP/0/RSP0/CPU0:PR11#")
x += 1
y = 0
while y < 18:
objTab1.Screen.Send(cisco_cmd_list_region_a[y])
objTab1.Screen.WaitForString("RP/0/RSP0/CPU0:PR11#")
y += 1
objTab1.Screen.Send("clear logg\r")
objTab1.Screen.WaitForString("[y/n]")
objTab1.Screen.Send("y\r")
objTab1.Screen.WaitForString("RP/0/RSP0/CPU0:PR11#")
objTab1.Session.Disconnect()
########## function: capture cmd at PR12 ##########
def PR12():
objTab2 = crt.Session.ConnectInTab("/telnet x.x.x.x")
objTab2.Caption = "PR12"
objTab2.Activate
objTab2.Session.Log(False)
objTab2.Session.LogFileName = DIR + FOLDERTIME + "\\" + "PR12-" + NOW + ".txt"
objTab2.Session.Log(True)
objTab2.Screen.Synchronous = True
if (objTab2.Screen.WaitForString("Username:",3) == True):
objTab2.Screen.Send("pdd\r")
if (objTab2.Screen.WaitForString("Password:",3) == True):
objTab2.Screen.Send("pdd123\r")
objTab2.Screen.WaitForString("RP/0/RSP0/CPU0:PR12#")
x = 0
while x < 46:
objTab2.Screen.Send(cisco_cmd_list[x])
objTab2.Screen.WaitForString("RP/0/RSP0/CPU0:PR12#")
x += 1
y = 0
while y < 18:
objTab2.Screen.Send(cisco_cmd_list_region_a[y])
objTab2.Screen.WaitForString("RP/0/RSP0/CPU0:PR12#")
y += 1
objTab2.Screen.Send("clear logg\r")
objTab2.Screen.WaitForString("[y/n]")
objTab2.Screen.Send("y\r")
objTab2.Screen.WaitForString("RP/0/RSP0/CPU0:PR12#")
objTab2.Session.Disconnect()
########## function: capture cmd at CR5 ##########
def CR5():
objTab3 = crt.Session.ConnectInTab("/telnet x.x.x.x")
objTab3.Caption = "CR5"
objTab3.Activate
objTab3.Session.Log(False)
objTab3.Session.LogFileName = DIR + FOLDERTIME + "\\" + "CR5-" + NOW + ".txt"
objTab3.Session.Log(True)
objTab3.Screen.Synchronous = True
if (objTab3.Screen.WaitForString("Username:",3) == True):
objTab3.Screen.Send("pdd\r")
if (objTab3.Screen.WaitForString("Password:",3) == True):
objTab3.Screen.Send("pdd123\r")
objTab3.Screen.WaitForString("RP/0/RSP0/CPU0:CR5#")
x = 0
while x < 46:
objTab3.Screen.Send(cisco_cmd_list[x])
objTab3.Screen.WaitForString("RP/0/RSP0/CPU0:CR5#")
x += 1
y = 0
while y < 18:
objTab3.Screen.Send(cisco_cmd_list_region_b[y])
objTab3.Screen.WaitForString("RP/0/RSP0/CPU0:CR5#")
y += 1
objTab3.Screen.Send("clear logg\r")
objTab3.Screen.WaitForString("[y/n]")
objTab3.Screen.Send("y\r")
objTab3.Screen.WaitForString("RP/0/RSP0/CPU0:CR5#")
objTab3.Session.Disconnect()
########## function: capture cmd at CR6 ##########
def CR6():
objTab4 = crt.Session.ConnectInTab("/telnet x.x.x.x")
objTab4.Caption = "CR6"
objTab4.Activate
objTab4.Session.Log(False)
objTab4.Session.LogFileName = DIR + FOLDERTIME + "\\" + "CR6-" + NOW + ".txt"
objTab4.Session.Log(True)
objTab4.Screen.Synchronous = True
if (objTab4.Screen.WaitForString("Username:",3) == True):
objTab4.Screen.Send("pdd\r")
if (objTab4.Screen.WaitForString("Password:",3) == True):
objTab4.Screen.Send("pdd123\r")
objTab4.Screen.WaitForString("RP/0/RSP0/CPU0:CR6#")
x = 0
while x < 46:
objTab4.Screen.Send(cisco_cmd_list[x])
objTab4.Screen.WaitForString("RP/0/RSP0/CPU0:CR6#")
x += 1
y = 0
while y < 18:
objTab4.Screen.Send(cisco_cmd_list_region_b[y])
objTab4.Screen.WaitForString("RP/0/RSP0/CPU0:CR6#")
y += 1
objTab4.Screen.Send("clear logg\r")
objTab4.Screen.WaitForString("[y/n]")
objTab4.Screen.Send("y\r")
objTab4.Screen.WaitForString("RP/0/RSP0/CPU0:CR6#")
objTab4.Session.Disconnect()
########## function: capture cmd at CR1 ##########
def CR1():
objTab5 = crt.Session.ConnectInTab("/telnet x.x.x.x")
objTab5.Caption = "CR1"
objTab5.Activate
objTab5.Session.Log(False)
objTab5.Session.LogFileName = DIR + FOLDERTIME + "\\" + "CR1-" + NOW + ".txt"
objTab5.Session.Log(True)
objTab5.Screen.Synchronous = True
if (objTab5.Screen.WaitForString("Username:",3) == True):
objTab5.Screen.Send("pdd\r")
if (objTab5.Screen.WaitForString("Password:",3) == True):
objTab5.Screen.Send("pdd123\r")
objTab5.Screen.WaitForString("<CR01>")
x = 0
while x < 45:
objTab5.Screen.Send(huawei_cmd_list[x])
objTab5.Screen.WaitForString("<CR01>")
x += 1
y = 0
while y < 18:
objTab5.Screen.Send(huawei_cmd_list_region_c[y])
objTab5.Screen.WaitForString("<CR01>")
y += 1
objTab5.Screen.Send("reset logbuffer\r")
objTab5.Screen.WaitForString("[Y/N]")
objTab5.Screen.Send("y\r")
objTab5.Screen.WaitForString("<CR01>")
objTab5.Session.Disconnect()
########## function: capture cmd at CR2 ##########
def CR2():
objTab6 = crt.Session.ConnectInTab("/telnet x.x.x.x")
objTab6.Caption = "CR2"
objTab6.Activate
objTab6.Session.Log(False)
objTab6.Session.LogFileName = DIR + FOLDERTIME + "\\" + "CR2-" + NOW + ".txt"
objTab6.Session.Log(True)
objTab6.Screen.Synchronous = True
if (objTab6.Screen.WaitForString("Username:",3) == True):
objTab6.Screen.Send("pdd\r")
if (objTab6.Screen.WaitForString("Password:",3) == True):
objTab6.Screen.Send("pdd123\r")
objTab6.Screen.WaitForString("<CR02>")
x = 0
while x < 45:
objTab6.Screen.Send(huawei_cmd_list[x])
objTab6.Screen.WaitForString("<CR02>")
x += 1
y = 0
while y < 18:
objTab6.Screen.Send(huawei_cmd_list_region_c[y])
objTab6.Screen.WaitForString("<CR02>")
y += 1
objTab6.Screen.Send("reset logbuffer\r")
objTab6.Screen.WaitForString("[Y/N]")
objTab6.Screen.Send("y\r")
objTab6.Screen.WaitForString("<CR02>")
objTab6.Session.Disconnect()
########## function: capture cmd at PR7 ##########
def PR7():
objTab7 = crt.Session.ConnectInTab("/telnet x.x.x.x")
objTab7.Caption = "PR7"
objTab7.Activate
objTab7.Session.Log(False)
objTab7.Session.LogFileName = DIR + FOLDERTIME + "\\" + "PR7-" + NOW + ".txt"
objTab7.Session.Log(True)
objTab7.Screen.Synchronous = True
if (objTab7.Screen.WaitForString("Username:",3) == True):
objTab7.Screen.Send("pdd\r")
if (objTab7.Screen.WaitForString("Password:",3) == True):
objTab7.Screen.Send("pdd123\r")
objTab7.Screen.WaitForString("<PR7>")
x = 0
while x < 45:
objTab7.Screen.Send(huawei_cmd_list[x])
objTab7.Screen.WaitForString("<PR7>")
x += 1
y = 0
while y < 18:
objTab7.Screen.Send(huawei_cmd_list_region_b[y])
objTab7.Screen.WaitForString("<PR7>")
y += 1
objTab7.Screen.Send("reset logbuffer\r")
objTab7.Screen.WaitForString("[Y/N]")
objTab7.Screen.Send("y\r")
objTab7.Screen.WaitForString("<PR7>")
objTab7.Session.Disconnect()
########## function: capture cmd at PR8 ##########
def PR8():
objTab8 = crt.Session.ConnectInTab("/telnet x.x.x.x")
objTab8.Caption = "PR8"
objTab8.Activate
objTab8.Session.Log(False)
objTab8.Session.LogFileName = DIR + FOLDERTIME + "\\" + "PR8-" + NOW + ".txt"
objTab8.Session.Log(True)
objTab8.Screen.Synchronous = True
if (objTab8.Screen.WaitForString("Username:",3) == True):
objTab8.Screen.Send("pdd\r")
if (objTab8.Screen.WaitForString("Password:",3) == True):
objTab8.Screen.Send("pdd123\r")
objTab8.Screen.WaitForString("<PR8>")
x = 0
while x < 45:
objTab8.Screen.Send(huawei_cmd_list[x])
objTab8.Screen.WaitForString("<PR8>")
x += 1
y = 0
while y < 18:
objTab8.Screen.Send(huawei_cmd_list_region_b[y])
objTab8.Screen.WaitForString("<PR8>")
y += 1
objTab8.Screen.Send("reset logbuffer\r")
objTab8.Screen.WaitForString("[Y/N]")
objTab8.Screen.Send("y\r")
objTab8.Screen.WaitForString("<PR8>")
objTab8.Session.Disconnect()
########## function: capture cmd at CR9 ##########
def CR9():
objTab9 = crt.Session.ConnectInTab("/telnet x.x.x.x")
objTab9.Caption = "CR9"
objTab9.Activate
objTab9.Session.Log(False)
objTab9.Session.LogFileName = DIR + FOLDERTIME + "\\" + "CR9-" + NOW + ".txt"
objTab9.Session.Log(True)
objTab9.Screen.Synchronous = True
if (objTab9.Screen.WaitForString("login:",3) == True):
objTab9.Screen.Send("lab\r")
if (objTab9.Screen.WaitForString("Password:",3) == True):
objTab9.Screen.Send("lab123\r")
objTab9.Screen.WaitForString("lab@CR9-re0> ")
x = 0
while x < 40:
objTab9.Screen.Send(juniper_cmd_list[x])
objTab9.Screen.WaitForString("lab@CR9-re0> ")
x += 1
y = 0
while y < 14:
objTab9.Screen.Send(juniper_cmd_list_region_a[y])
objTab9.Screen.WaitForString("lab@CR9-re0> ")
y += 1
objTab9.Screen.Send("clear log messages\r")
objTab9.Screen.WaitForString("lab@CR9-re0> ")
objTab9.Session.Disconnect()
########## function: capture cmd at CR10 ##########
def CR10():
objTab10 = crt.Session.ConnectInTab("/telnet x.x.x.x")
objTab10.Caption = "CR10"
objTab10.Activate
objTab10.Session.Log(False)
objTab10.Session.LogFileName = DIR + FOLDERTIME + "\\" + "CR10-" + NOW + ".txt"
objTab10.Session.Log(True)
objTab10.Screen.Synchronous = True
if (objTab10.Screen.WaitForString("login:",3) == True):
objTab10.Screen.Send("lab\r")
if (objTab10.Screen.WaitForString("Password:",3) == True):
objTab10.Screen.Send("lab123\r")
objTab10.Screen.WaitForString("lab@CR10-re0> ")
x = 0
while x < 40:
objTab10.Screen.Send(juniper_cmd_list[x])
objTab10.Screen.WaitForString("lab@CR10-re0> ")
x += 1
y = 0
while y < 14:
objTab10.Screen.Send(juniper_cmd_list_region_a[y])
objTab10.Screen.WaitForString("lab@CR10-re0> ")
y += 1
objTab10.Screen.Send("clear log messages\r")
objTab10.Screen.WaitForString("lab@CR10-re0> ")
objTab10.Session.Disconnect()
########## function: capture cmd at PR3 ##########
def PR3():
objTab11 = crt.Session.ConnectInTab("/telnet x.x.x.x")
objTab11.Caption = "PR3"
objTab11.Activate
objTab11.Session.Log(False)
objTab11.Session.LogFileName = DIR + FOLDERTIME + "\\" + "PR3-" + NOW + ".txt"
objTab11.Session.Log(True)
objTab11.Screen.Synchronous = True
if (objTab11.Screen.WaitForString("login:",3) == True):
objTab11.Screen.Send("lab\r")
if (objTab11.Screen.WaitForString("Password:",3) == True):
objTab11.Screen.Send("lab123\r")
objTab11.Screen.WaitForString("lab@pr3-re1> ")
x = 0
while x < 40:
objTab11.Screen.Send(juniper_cmd_list[x])
objTab11.Screen.WaitForString("lab@pr3-re1> ")
x += 1
y = 0
while y < 14:
objTab11.Screen.Send(juniper_cmd_list_region_c[y])
objTab11.Screen.WaitForString("lab@pr3-re1> ")
y += 1
objTab11.Screen.Send("clear log messages\r")
objTab11.Screen.WaitForString("lab@pr3-re1> ")
objTab11.Session.Disconnect()
########## function: capture cmd at PR4 ##########
def PR4():
objTab12 = crt.Session.ConnectInTab("/telnet x.x.x.x")
objTab12.Caption = "PR4"
objTab12.Activate
objTab12.Session.Log(False)
objTab12.Session.LogFileName = DIR + FOLDERTIME + "\\" + "PR4-" + NOW + ".txt"
objTab12.Session.Log(True)
objTab12.Screen.Synchronous = True
if (objTab12.Screen.WaitForString("login:",3) == True):
objTab12.Screen.Send("lab\r")
if (objTab12.Screen.WaitForString("Password:",3) == True):
objTab12.Screen.Send("lab123\r")
objTab12.Screen.WaitForString("lab@PR4-re0> ")
x = 0
while x < 40:
objTab12.Screen.Send(juniper_cmd_list[x])
objTab12.Screen.WaitForString("lab@PR4-re0> ")
x += 1
y = 0
while y < 14:
objTab12.Screen.Send(juniper_cmd_list_region_c[y])
objTab12.Screen.WaitForString("lab@PR4-re0> ")
y += 1
objTab12.Screen.Send("clear log messages\r")
objTab12.Screen.WaitForString("lab@PR4-re0> ")
objTab12.Session.Disconnect()
######### function: capture cmd at RR13 ##########
def RR13():
objTab13 = crt.Session.ConnectInTab("/telnet x.x.x.x")
objTab13.Caption = "RR13"
objTab13.Activate
objTab13.Session.Log(False)
objTab13.Session.LogFileName = DIR + FOLDERTIME + "\\" + "RR13-" + NOW + ".txt"
objTab13.Session.Log(True)
objTab13.Screen.Synchronous = True
if (objTab13.Screen.WaitForString("login:",3) == True):
objTab13.Screen.Send("lab\r")
if (objTab13.Screen.WaitForString("Password:",3) == True):
objTab13.Screen.Send("lab123\r")
objTab13.Screen.WaitForString("lab@RR13-re0> ")
x = 0
while x < 11:
objTab13.Screen.Send(juniper_rr_cmd_list[x])
objTab13.Screen.WaitForString("lab@RR13-re0> ")
x += 1
objTab13.Session.Disconnect()
########## function: capture cmd at RR14 ##########
def RR14():
objTab14 = crt.Session.ConnectInTab("/telnet x.x.x.x")
objTab14.Caption = "RR14"
objTab14.Activate
objTab14.Session.Log(False)
objTab14.Session.LogFileName = DIR + FOLDERTIME + "\\" + "RR14-" + NOW + ".txt"
objTab14.Session.Log(True)
objTab14.Screen.Synchronous = True
if (objTab14.Screen.WaitForString("login:",3) == True):
objTab14.Screen.Send("lab\r")
if (objTab14.Screen.WaitForString("Password:",3) == True):
objTab14.Screen.Send("lab123\r")
objTab14.Screen.WaitForString("lab@RR14-re0> ")
x = 0
while x < 11:
objTab14.Screen.Send(juniper_rr_cmd_list[x])
objTab14.Screen.WaitForString("lab@RR14-re0> ")
x += 1
objTab14.Session.Disconnect()
######### function main() ##########
PR11()
PR12()
CR5()
CR6()
CR1()
CR2()
PR7()
PR8()
CR9()
CR10()
PR3()
PR4()
RR13()
RR14()
#crt.Screen.SendSpecial("MENU_DISCONNECT_ALL")L")