The new era of network engineering ability to become more important than the ability to knock command, the big tide has been unable to change. I've been working on Python's Netmiko as an SSH method recently. Share the first script and let's do some research.
The direction is simple, through the Python open method reads the IP address inside the iplist.txt, these IP is the switch IP address which the command needs to be issued. Then read what commands are required to be issued in the cmdlist. Connect to the switch automatically via Python's Netmiko ssh
h3c_check.py
Coding:utf-8
Import time
From Netmiko Import *
Import Sys
Import OS
Import Getpass
#读取iplist the IP address inside the. txt document
IPList = open (' Iplist.txt ', ' r ', encoding = ' ASCII ')
IP_ADDR = Iplist.readlines ()
Iplist.close ()
cmdlist = open (' Cmdlist.txt ', ' r ', encoding = ' ASCII ')
Cmd_line = Cmdlist.readlines ()
Cmdlist.close ()
#遍历ip_addr这个列表里面的ip进行连接
For IP in ITER (IP_ADDR):
Print (IP)
Try
S5130 = {
' Device_type ': ' Hp_comware ',
' IP ': IP,
' username ': ' h3c ',
' Password ': ' H3C ',
}
net_connect = ConnectHandler(**S5130) for cmd in iter(cmd_line): cmd_result = net_connect.send_command(cmd) print(‘-----------------------------------------------------------------‘) print(cmd) print(cmd_result) print(‘-----------------------------------------------------------------‘) print(‘‘) net_connect.disconnect()except (EOFError, NetMikoTimeoutException): print(‘Can not connect to Device‘)except (EOFError, NetMikoAuthenticationException): print(‘username/password wrong!‘)
Iplist.txt
192.168.11.112
192.168.11.110
Cmdlist.txt
Display VLAN
Display version
Display IP Inter Brief
H3C Switch python command issued script