Paramiko会话超时,但我需要执行很多命令(Paramiko session times out, but i need to execute a lot of commands)

编程入门 行业动态 更新时间:2024-10-27 16:35:53
Paramiko会话超时,但我需要执行很多命令(Paramiko session times out, but i need to execute a lot of commands)

我正在研究一个运行Cisco IOS的​​远程设备的脚本(python 2.7),所以我需要通过ssh执行很多命令。 很少有命令没有输出,有些命令没有输出,我想要接收输出。 它是这样的:

import paramiko ssh=paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(self._ip, port=22, username=username, password=password stdin, stdout, stderr = ssh.exec_command('command with no output') stdin, stdout, stderr = ssh.exec_command('command with no output') stdin, stdout, stderr = ssh.exec_command('command with output') sh_ver = stdout.readlines()

事情是exec_command是导致通道关闭,它不能被重用,但我不可能打开一个新的通道,以执行另一个命令,因为这是一个命令会话,最终我需要得到输出。

我试图以这种方式执行命令:

stdin, stdout, stderr = ssh.exec_command(''' command command command ''') output = stdout.readlines()

但是这样, output是空的。 即使它不会,我需要对output执行一些检查,然后继续停止的会话。

那么我需要什么? 一种管理此ssh连接而不关闭或启动新连接并轻松接收命令输出的方法。

在此先感谢,美里。 :)

I'm working on a script (python 2.7) that is wotking with a remote device running Cisco IOS, so I need to execute a lot of commands through ssh. Few commands have no output and some of them have, and I want to recieve the output. It goes something like this:

import paramiko ssh=paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(self._ip, port=22, username=username, password=password stdin, stdout, stderr = ssh.exec_command('command with no output') stdin, stdout, stderr = ssh.exec_command('command with no output') stdin, stdout, stderr = ssh.exec_command('command with output') sh_ver = stdout.readlines()

The thing is exec_command is causes the channel to close and it can’t be reused, but it's not possible for me to open a new channel in order to execute another command, because this is a session of commands that in the end I need to get the output.

I've tried to execute the commands this way as well:

stdin, stdout, stderr = ssh.exec_command(''' command command command ''') output = stdout.readlines()

but this way, output is empty. And even if it would'nt, I need to perform a few checks on the output and then continue the session where I stopped.

So what do I need? A way to manage this ssh connection without closing it or starting a new one, and to easily recieve the output from the command.

Thanks in advance, Miri. :)

最满意答案

我认为你需要的是invoke_shell() 。 例如:

ssh = paramiko.SSHClient() ... ... chan = ssh.invoke_shell() # starts an interactive session chan.send('command 1\r') output = chan.recv() chan.send('command 2\r') output = chan.recv() ... ...

该Channel有许多其他方法。 您可以参考该文件以了解更多详情。

I think what you need is invoke_shell(). For example:

ssh = paramiko.SSHClient() ... ... chan = ssh.invoke_shell() # starts an interactive session chan.send('command 1\r') output = chan.recv() chan.send('command 2\r') output = chan.recv() ... ...

The Channel has many other methods. You can refer to the document for more details.

更多推荐

本文发布于:2023-08-03 02:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382913.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:但我   命令   session   Paramiko   lot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!