from netmiko import connecthandler for i in range(20,24): sw={ 'device_type':'huawei', 'ip':'10.0.1.'+str(i), 'username':'python', 'password':'123456', } cOnnect=connecthandler(**sw) print('n'+'-----------------'+'成功登录到交换机'+sw['ip']+'-----------------') config_commands=['stp mode stp','dis stp | include cist global info'] output=connect.send_config_set(config_commands) print(output) connect.save_config()
from netmiko import connecthandler with open('ip_list.txt') as f: for line in f.readlines(): host=line.strip() sw={ 'device_type':'huawei', 'ip':host, #host是变量,不要粗心加了引号 'username':'python', 'password':'123456', } cOnnect=connecthandler(**sw) print('n'+'-----------------'+'成功登录到交换机'+sw['ip']+'-----------------') config_commands=['stp mode stp','dis stp | include cist global info'] output=connect.send_config_set(config_commands) print(output) connect.save_config()