【问题标题】:Run a command (to backup a configuration) on multiple servers (routers) in Python在 Python 中的多个服务器(路由器)上运行命令(备份配置)
【发布时间】:2019-04-13 10:06:45
【问题描述】:

是否可以让脚本遍历每个 IP 并在本地 tftp 服务器上备份运行配置

   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 --- 我可以使用变量名作为文本文件名吗?

【问题讨论】:

  • 什么是redirect?那是命令吗?你想在哪里执行?在本地还是在路由器上?
  • Redirect 是 Cisco 发布的命令,可在任何交换机/路由器上运行,以通过在本地 PC 上运行的 tftp 服务器推送运行配置。如果您知道更好的方法,请分享。

标签: python networking ssh paramiko cisco


【解决方案1】:
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 attacks 的保护。如需正确解决方案,请参阅Paramiko "Unknown Server"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 2018-12-08
    • 2017-03-16
    • 2023-04-07
    • 2021-08-29
    • 2018-03-25
    相关资源
    最近更新 更多