【问题标题】:config a cisco router through paramiko通过 paramiko 配置 cisco 路由器
【发布时间】:2020-08-11 02:07:23
【问题描述】:

我正在尝试通过 paramiko 配置 cisco 路由器。 首先我 ssh 到路由器,然后运行命令。但是当我连接到路由器时,我无法进入配置模式。 我的意思是路由器以用户模式连接并运行enconf t 不起作用!

conn = paramiko.SSHClient()
conn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
conn.connect("20.0.0.1", 22, "cisco", "123")
stdin, stdout, stderr = conn.exec_command('show ip int br')
status = stdout.channel.exit_status_ready()
if status==0:
 mystring = stdout.read()
 print mystring

状态为0,但mystring为空字符串。(结果为:[])

我在 Fedora 20 上。

谢谢

【问题讨论】:

  • stderr 怎么样? en 的 stdout 和 stderr 呢?
  • 其实我是 python 新手。我通过搜索找到了这个代码。对不起,我不明白你的问题!
  • 连接后尝试:print conn.get_transport().is_active() 所以:连接成功了吗?
  • 感谢您的回复。运行print conn.get_transport().is_active() 的结果是true
  • 我发现 exec_command 是一个非阻塞调用。所以我编辑了我的代码,但我没有任何结果!

标签: python ssh paramiko


【解决方案1】:

您的上述命令在 Cisco 881 上适用于我(即“show ip int br”运行正确并播放输出。

如果我添加第二个命令,例如“conf t”或“enable”,它会失败。这是因为 Cisco 上的 exec_command 只允许单个命令。您需要运行 .invoke_shell() 方法来执行多个命令。

这是一个例子:

import paramiko
from getpass import getpass
import time

ip = raw_input("Please enter your IP address: ")
username = raw_input("Please enter your username: ")
password = getpass()

remote_conn_pre=paramiko.SSHClient()
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
remote_conn_pre.connect(ip, port=22, username=username,  
                        password=password,
                        look_for_keys=False, allow_agent=False)

remote_conn = remote_conn_pre.invoke_shell()
output = remote_conn.recv(65535)
print output

remote_conn.send("show ip int brief\n")
time.sleep(.5)
output = remote_conn.recv(65535)
print output

remote_conn.send("conf t\n")
time.sleep(.5)
output = remote_conn.recv(65535)
print output

remote_conn.send("end\n")
time.sleep(.5)
output = remote_conn.recv(65535)
print output

您可能还想查看我一直在开发的这个库。它简化了其中一些机制:

https://pynet.twb-tech.com/blog/automation/netmiko.html

【讨论】:

    【解决方案2】:

    注意:请务必在您的 cisco 交换机上启用 ssh。我使用了思科 VIRL ISO。同时根据您的 ssh 更改用户名和密码。

    import paramiko
    import time
    def attempt():
    
        IP = "192.168.0.52"
        USER = "cisco"
        Password = "cisco"
        PORT=22
          
        try:
    
            ssh = paramiko.SSHClient()
            #ssh.load_system_host_keys()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     
            try:
                ssh.connect(IP , port=PORT, username=USER, password=Password)
                print ("Connected successfully.")
    
                connection = ssh.invoke_shell()
                connection.send("enable\n")
                time.sleep(.5)
                connection.send("cisco\n")
                time.sleep(.5)
               #connection.send("conf t\n")
                connection.send("show ip int brief\n")
                #connection.send("int loop 2\n")
               #connection.send("ip address 2.3.1.1 255.255.255.255\n")
                
                time.sleep(.5)
                router_output = connection.recv(65535)
                time.sleep(.5)
                print("\n\n")
                print(str(router_output) + "\n")
                time.sleep(.5)
    
                connection.send("end\n")
    
            except paramiko.AuthenticationException:
                print ("Incorrect password: "+Password)
    
            except socket.erro:
                print ("Socket Error")
        except:
            print("Something Went Wrong")
    
        
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      相关资源
      最近更新 更多