与 Cisco 路由器的持久 ssh 会话 [英] Persistent ssh session to Cisco router
问题描述
我在该站点和其他多个位置进行了搜索,但是在执行一个命令后,我无法解决连接和维护 ssh 会话的问题.以下是我当前的代码:
I have search on this site and multiple other locations but I have been unable to resolve my problem of connecting and maintaining ssh session after one command. Below is my current code:
#!/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未重复使用.非常感谢您对解决此问题的任何帮助.
I can run multiple commands but for every commands a new ssh session is created. The above program does not work when I need to configuration mode because ssh session is not reused.Any assistance in resolving this matter is greatly appreciated.
推荐答案
我使用 Exscript 而不是 paramiko,现在我可以在 IOS 设备上获得持久会话.
I used Exscript instead of paramiko and I am now able to get persistent session on IOS device.
#!/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
这篇关于与 Cisco 路由器的持久 ssh 会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!