【发布时间】:2016-06-23 06:09:47
【问题描述】:
我正在努力向多个主机发送多个命令,我正在使用从文件输入的命令:
commands.txt
sh ip int bri
sh run
sh ver
hosts.txt
router 1
router 2
router 3
然后我运行以下
from future import print_function from netmiko import ConnectHandler ##For SSH import re import getpass while True: #create loop for whole program username = input ("Enter Username") jumphostpassword = getpass.getpass( "输入 Jumphost 密码") elif (op == 2): TACACSpassword = getpass.getpass ("输入 TACACS 密码") elif(in1=="c"): commandsfile = input ("请输入 CommandsFile 路径为 c:/example / \n :") hostsfile = input ("请输入主机路径为 c:/example/ \n :") # hosts = open((hostsfile) , "r") hosts = [hosts for hosts in (hosts.strip () for hosts in open(hostsfile)) if hosts] for host1 in hosts: with open (host1+".txt","w") as file: commands1 = open( (commandsfile), "r+") jumphost = {'设备类型': 'linux','ip': '172.27.200.26', 'username': (username),'password': (jumphostpassword)} net_connect = ConnectHandler(**jumphost) output = net_connect.send_command("ssh" + str(host1)) print (output) else: output = net_connect.send_command(TACACSpassword) print (output) output = net_connect.send_command("term leng 0") print (output) cmd1 = [cmd1 for cmd1 in (cmd1.strip () for cmd1 in open(commandsfile)) if cmd1] for cmd1 in commands1: print ("保存在 c:\saad\saad.txt 中的文件 ") output += net_connect.send_config_set(cmd1) print (output) net_connect.disconnect print ("保存在 c:\saad\saad.txt 中的文件") 文件。写(输出)file.close()继续
【问题讨论】: