import time from netmiko import ConnectHandler from netmiko.ssh_exception import NetMikoTimeoutException from netmiko.ssh_exception import NetMikoAuthenticationException import sys import getpass from datetime import datetime import os def NetworkDevice(username,password,iplist,enablepass): rt = { 'device_type':'cisco_ios', 'username':username, 'password':password, 'ip': iplist, 'secret':enablepass } print('-' * 50) print(u'[+] connecting to network device {0}...'.format(iplist)) net_connect = ConnectHandler(**rt) net_connect.enable() hostname = net_connect.find_prompt().replace("#", "") print (u'[+] hostname:{0}'.format(hostname)) timestr = time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time())) cmd = 'show running' filename = (u'{0}_{1}_{2}.txt'.format(iplist,cmd,timestr)) filepath = r'C:\netpy\5pyresult\/' if os.path.exists(filepath): message = 'OK,the "%s" dir exists.' else: message = "Now, I will create the %s" os.makedirs(filepath) save = open(filepath + filename,'w') print(u'[+] executing {0} command'.format(cmd)) output = net_connect.send_command(cmd) time.sleep(2) save.write(output) print(u'[+] {0} command executed,result was saved at {1}!'.format(cmd,filename)) save.close() net_connect.disconnect() if __name__ == '__main__': print "[+] The Program is running......." username = raw_input('Username:') password = getpass.getpass() enablepass = raw_input('enable:') filepath2 = r'C:\netpy\\' if os.path.exists(filepath2): message = 'OK,the "%s" file exists.' else: message = "Now, I will create the %s" os.makedirs(filepath2) for ips in open(r'C:\netpy\iplist.txt','r'): start_time = datetime.now() iplist = ips.replace('\n', '') try: NetworkDevice(username,password,iplist,enablepass) except (EOFError, NetMikoTimeoutException): print ('Can not connect to Device') except (EOFError, NetMikoAuthenticationException): print ('username/password wrong!') except (ValueError,NetMikoAuthenticationException): print ('enable password wrong!') print "Time elapsed: {0}\n".format(datetime.now() - start_time) time.sleep(2)
一次性查看多個設備信息,暫時不支持H3C。python
#coding=utf-8 from netmiko import ConnectHandler import datetime,time cisco_3750 = { 'device_type': 'cisco_ios', 'ip': '10.0.1.254', 'username': 'admin', 'password': 'huasan@123', 'verbose': True, } cisco_sw3 = { 'device_type': 'cisco_ios', 'ip': '10.0.1.3', 'username': 'admin', 'password': 'huasan@123', 'verbose': True, } cisco_sw4 = { 'device_type': 'cisco_ios', 'ip': '10.0.1.4', 'username': 'admin', 'password': 'huasan@123', 'verbose': True, } cisco_sw5 = { 'device_type': 'cisco_ios', 'ip': '10.0.1.5', 'username': 'admin', 'password': 'huasan@123', 'verbose': True, } cisco_sw6 = { 'device_type': 'cisco_ios', 'ip': '10.0.1.6', 'username': 'admin', 'password': 'huasan@123', 'verbose': True, } cisco_sw7 = { 'device_type': 'cisco_ios', 'ip': '10.0.1.7', 'username': 'admin', 'password': 'huasan@123', 'verbose': True, } cisco_sw8 = { 'device_type': 'cisco_ios', 'ip': '10.0.1.8', 'username': 'admin', 'password': 'huasan@123', 'verbose': True, } cisco_sw9 = { 'device_type': 'cisco_ios', 'ip': '10.0.1.9', 'username': 'admin', 'password': 'huasan@123', 'verbose': True, } all_devices = [cisco_3750,cisco_sw3,cisco_sw4,cisco_sw5,cisco_sw6,cisco_sw7,cisco_sw8,cisco_sw9] start_time = datetime.datetime.now() for a_device in all_devices: net_connect = ConnectHandler(**a_device) net_connect.find_prompt() output = net_connect.send_command("show ip int brief") print "\n\n>>>>>>>>> Device {0} <<<<<<<<<".format(a_device['device_type']) print output output = net_connect.send_command("show run ") print output print ">>>>>>>>> End <<<<<<<<<"
https://blog.csdn.net/u012703479/article/details/79837161ios