【问题标题】:Persistent ssh session to Cisco router到 Cisco 路由器的持久 ssh 会话
【发布时间】:2011-07-11 09:56:48
【问题描述】:

我在此站点和其他多个位置进行了搜索,但我无法解决在一个命令后连接和维护 ssh 会话的问题。以下是我当前的代码:

#!/opt/local/bin/python

import os  

import pexpect

import paramiko

import hashlib

import StringIO

while True:

      cisco_cmd = raw_input("Enter cisco router cmd:")

      ssh = paramiko.SSHClient()

      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

      ssh.connect('192.168.221.235', username='nuts', password='cisco', timeout =  30)

      stdin, stdout, stderr = ssh.exec_command(cisco_cmd)

      print stdout.read()

      ssh.close()

      if  cisco_cmd == 'exit': break

我可以运行多个命令,但每个命令都会创建一个新的 ssh 会话。 当我需要配置模式时,上面的程序不起作用,因为 ssh session 没有被重复使用。非常感谢您提供解决此问题的任何帮助。

【问题讨论】:

  • 我对一个同时导入 pexpect 和 paramiko 的脚本很着迷……您是同时使用这两种方法,还是尝试一种并迁移?

标签: python cisco paramiko


【解决方案1】:

你需要在while循环之外创建、连接和关闭连接。

【讨论】:

    【解决方案2】:

    你的循环就是这样做的

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect('192.168.221.235', username='nuts', password='cisco', timeout =  30)
    while True:
          cisco_cmd = raw_input("Enter cisco router cmd:")
          stdin, stdout, stderr = ssh.exec_command(cisco_cmd)
          print stdout.read()
          if  cisco_cmd == 'exit': break
    ssh.close()
    

    将初始化和设置移到循环之外。 编辑:移动关闭()

    【讨论】:

    • ssh.close() 也不应该在循环中。
    • 我能够连接并成功运行第一个命令。我无法设置一个持续存在的连接,以便可以运行多个相关命令。
    【解决方案3】:

    上面的程序在我运行时不起作用 需要配置模式,因为 ssh 会话没有被重用

    您的 ssh 会话将在您将 connectclose 移出循环后被重用,但每个 exec_command() 都发生在一个新的 shell 中(通过一个新通道),并且是无关的。您需要格式化您的命令,以便它们不需要来自 shell 的任何状态。

    如果我没记错的话,一些 Cisco 设备只允许一个 exec,然后关闭连接。在这种情况下,您将需要使用invoke_shell(),并使用pexpect 模块(您已经导入但未使用)以交互方式工作。

    【讨论】:

    • 谢谢,仍然尝试调用_shell() 无济于事。当我解决我的问题时,我会在一两天内报告。
    【解决方案4】:

    我使用 Exscript 而不是 paramiko,现在我可以在 IOS 设备上获得持久会话。

    #!/opt/local/bin/python
    import hashlib
    import Exscript
    
    from Exscript.util.interact import read_login
    from Exscript.protocols import SSH2
    
    account = read_login()              # Prompt the user for his name and password
    conn = SSH2()                       # We choose to use SSH2
    conn.connect('192.168.221.235')     # Open the SSH connection
    conn.login(account)                 # Authenticate on the remote host
    conn.execute('conf t')              # Execute the "uname -a" command
    conn.execute('interface Serial1/0')
    conn.execute('ip address 114.168.221.202 255.255.255.0')
    conn.execute('no shutdown')
    conn.execute('end')
    conn.execute('sh run int Serial1/0')
    print conn.response
    
    conn.execute('show ip route')
    print conn.response
    
    conn.send('exit\r')                 # Send the "exit" command
    conn.close()                        # Wait for the connection to close
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-22
      • 2022-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多