在Python中的多个服务器(路由器)上运行命令(备份配置)

问题描述 投票:0回答:1

是否可以让脚本迭代本地 tftp 服务器上的每个 IP 并备份运行配置

   import paramiko
    import sys
    import time


    USER = "root"
    PASS = "cisco"
    HOST = ["10.10.10.10","11.11.11.11","12.12.12.12"]
    i=0
    while i <len(HOST)
    def fn():
      client1=paramiko.SSHClient()
      #Add missing client key
      client1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      #connect to switch
      client1.connect(HOST,username=USER,password=PASS)
      print "SSH connection to %s established" %HOST

     show run | redirect tftp://10.10.10.20/HOST.cfg 
    print "Configuration has been backed up"for  %HOST
    i+1

show run | redirect tftp://10.10.10.20/HOST.cfg
--- 我可以使用变量名作为文本文件名吗?

python networking ssh paramiko cisco
1个回答
0
投票
for h in HOST:
    client = paramiko.SSHClient()
    #Add missing client key
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    #connect to switch
    client.connect(h, username = USER, password = PASS)
    print("SSH connection to {0} established".format(h))
    command = "show run | redirect tftp://10.10.10.20/{0}.cfg".format(h)
    (stdin, stdout, stderr) = client.exec_command(command)
    for line in stdout.readlines():
        print(line)
    client.close()
    print("Configuration has been backed up for {0}".format(h))

强制性警告:请勿使用

AutoAddPolicy
- 这样做您将失去针对 MITM 攻击的保护。正确的解决方案请参阅Paramiko“未知服务器”

© www.soinside.com 2019 - 2024. All rights reserved.