1.通过netmiko模块连接交换机,并发送命令,将日志记录给文本
代码:
from netmiko import ConnectHandler SW={ 'device_type':'huawei', 'ip':'192.168.56.2', 'username':'wanghang', 'password':'wanghang@123' } connect=ConnectHandler(**SW) #连接 print("Sucessfully cinnected to "+SW['ip']) config_commands=['int loop 1','ip address 2.2.2.2 24'] #定义一个命令的列表 output=connect.send_config_set(config_commands) #发送列表,将输出的结果赋给output print(output) result=connect.send_command('dis cur int loop 1') output_cur=connect.send_command('dis cur') print(output_cur) filename=SW['device_type']+'.txt' #定义一个文件 with open(filename,'w')as config_cur: config_cur.write(output_cur) #将配置信息写入文件 config_cur # print(result)
2.编写crt可执行脚本
脚本1代码:
# $language = "Python" # $interface = "1.0" def more(): while True: if crt.Screen.WaitForString("More",5): #判断5秒内输出信息是否含”More" crt.Screen.Send(" ") #发送回车键 else: break #没有 退出循环 with open("list_commands.txt",'r') as cmd: #打开命令文本 list_commands=cmd.readlines() #以列表的形式读出来 def commands(list_commands,filename): crt.Session.Log(False) #关闭之前的会话日志 crt.Session.LogFileName = filename #定义会话日志名称 crt.Session.Log(True) #打开会话日志 crt.Screen.Synchronous = True #屏显同步开启 for command in list_commands: #循环命令列表 crt.Screen.Send(command+" ") more() crt.Screen.Synchronous=False #屏显同步关闭 crt.Session.Log(False) #会话日志关闭 commands(list_commands,"dis_crt.txt")
脚本2代码:
1 # $language="Python" 2 # $interface="1.0" 3 def more(): 4 """ 5 调用函数时,检测屏幕是否出现“More”内容, 6 :return: 出现""More""就换行,没出现不执行内容 7 """ 8 while True: 9 if crt.Screen.WaitForString("More",1): 10 crt.Screen.Send(" ") 11 else: 12 break 13 14 15 def commands(filename): 16 """ 17 调用时执行巡检脚本,将文本xunjian.txt的内容输入给屏幕 18 :param filename: 19 :return: 得到一个巡检的日志 20 """ 21 crt.Session.Log(False) 22 crt.Session.LogFileName=filename 23 crt.Session.Log(True) 24 crt.Screen.Synchronous=True 25 with open("xunjian.txt", 'r') as f: 26 for cmd in f.readlines(): 27 crt.Screen.Send(cmd+" ") 28 more() 29 f.close() 30 crt.Screen.Synchronous=False 31 crt.Session.Log(False) 32 33 34 def get_name_log(): 35 """ 36 在巡检日志中查找设备名称 37 :return: 得到一个设备名称的txt文件名 38 """ 39 with open("log.txt",'r')as f_log: 40 read_log=f_log.readlines() 41 for i in read_log: 42 if 'sysname' in i: 43 filename=i.rstrip()[8:]+".txt" 44 break 45 return filename 46 47 48 def copy_file(): 49 """ 50 复制巡检日志 51 :return: 得到一个以设备名称命名的巡检日志 52 """ 53 with open(get_name_log(),'w')as f_log: 54 with open("log.txt",'r') as log: 55 for i in log.readlines(): 56 f_log.write(i) 57 f_log.close() 58 59 commands("log.txt") 60 copy_file()
3.paramiko模块连接交换机
代码:
1 import paramiko 2 import time 3 ip="192.168.56.3" 4 username="test" 5 password="test" 6 7 ssh_client=paramiko.SSHClient() 8 ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)#不使用自己的公钥,接受来自SSH服务器端提供的公钥 9 ssh_client.connect(hostname=ip,username=username,password=password)#连接 10 print("Successfully connected to ",ip)#打印连接成功的ip地址 11 command=ssh_client.invoke_shell() #调用shell方法来使用命令行 12 command.send("system-view ")#命令行发送内容 13 command.send("int loop 1 ") 14 command.send("ip address 3.3.3.3 24 ") 15 command.send("q ") 16 command.send("q ") 17 command.send("save ") 18 command.send("Y ") 19 time.sleep(2) #时间等待两秒 20 output=command.recv(65535) #接收命令行输出的内容65535(最大值)个字节 21 print(output) 22 ssh_client.close()#关闭连接
4.paramiko模块连接多个交换机
代码:
1 import paramiko 2 import time 3 4 username=input('Username:') 5 password=input('Password:') 6 for i in range(2,6): 7 ip="192.168.56."+str(i) 8 ssh_client=paramiko.SSHClient() 9 ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())#使用服务器发来的公钥 10 ssh_client.connect(hostname=ip,username=username,password=password) 11 print("Successfully connect to ",ip) 12 command=ssh_client.invoke_shell() #调用命令行 13 command.send("system-view ") 14 for n in range(10,21): #创建vlan10-20 15 print("Creating VLAN "+str(n)) 16 command.send("vlan "+str(n)+" ") #注意加空格 17 time.sleep(1) 18 command.send("q ") 19 command.send("q ") 20 command.send("save ") 21 command.send("y ") 22 output=command.recv(65535) #抓取屏幕信息 23 print(output) 24 ssh_client.close()#关闭连接