【问题标题】:Send Multiple commands using Netmiko使用 Netmiko 发送多个命令
【发布时间】: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()继续

【问题讨论】:

    标签: command router


    【解决方案1】:

    按照以下格式将您的 IP 放入 ips.csv 文件中...

    Host
    192.168.1.1
    192.168.1.2
    

    然后使用如下代码,用法 python code.py -c ips.csv

    #!/usr/bin/python
    
    import getpass
    import re
    import csv
    import paramiko
    import netmiko
    from argparse import ArgumentParser
    from netmiko import ConnectHandler
    
    if __name__ == '__main__':
        parser = ArgumentParser(description='Arguments:')
        parser.add_argument('-c', '--csv', required=True, action='store',
                            help='Location of CSV file of IPs')
    
    
    args = parser.parse_args()
    
    ssh_username = 'yoursshusername'
    
    
    ssh_password = 'yoursshpassword'
    
    with open(args.csv, 'r') as file:
        reader = csv.DictReader(file)
        for device_row in reader:
            try:
                ssh_session = ConnectHandler(device_type='cisco_ios',
                        ip=device_row['Host'],
                        username=ssh_username, password=ssh_password)
                print '********* {0} *********'.format(device_row['Host'
                        ])
    
            # Specify your commands here, you can add more commands just follow the same syntax
                 print ssh_session.send_command('show running-config | i hostname')
            # Specify exceptions here 
            except paramiko.AuthenticationException:
        print ('{0}'.format(device_row['Host']),"Authenticaiton Problem!")
                pass
    

    【讨论】:

      猜你喜欢
      • 2012-09-20
      • 1970-01-01
      • 2016-07-05
      • 2015-12-14
      • 1970-01-01
      • 2011-11-08
      • 2011-10-05
      • 1970-01-01
      • 2021-11-14
      相关资源
      最近更新 更多