与 Cisco 路由器的持久 ssh 会话

发布于 2024-10-21 07:33:44 字数 686 浏览 4 评论 0原文

我已在此站点和多个其他位置进行搜索,但无法解决在一个命令后连接和维护 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 会话 不被重复使用。非常感谢任何解决此问题的帮助。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

趁年轻赶紧闹 2024-10-28 07:33:44

我使用 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

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
雪落纷纷 2024-10-28 07:33:44

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

You need to create, connect and close connection outside the while loop.

墨落成白 2024-10-28 07:33:44

您的循环将

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()

初始化和设置移到循环之外。
编辑:移近()

Your loop does that

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()

Move the initialisation and setup outside the loop.
EDIT: Moved close()

郁金香雨 2024-10-28 07:33:44

当我
需要配置模式,因为 ssh
会话不被重用

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

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

The above program does not work when I
need to configuration mode because ssh
session is not reused

Your ssh session will be reused once you move the connect and close outside of the loop, but each exec_command() happens in a new shell (through a new channel), and are unrelated. You will need to format your commands so that they don't require any state from the shell.

If I remember correctly, some Cisco devices only allow a single exec, and then close the connection. In that case, you will need to use invoke_shell(), and work interactively using the pexpect module (which you already have imported, but aren't using).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文