Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何通过ssh使用paramiko登录网络设备?_Python_Ssh_Paramiko - Fatal编程技术网

Python 如何通过ssh使用paramiko登录网络设备?

Python 如何通过ssh使用paramiko登录网络设备?,python,ssh,paramiko,Python,Ssh,Paramiko,我正在尝试通过ssh登录网络设备。在执行第一个命令后,我获得了正确的输出,但在执行第二个命令时出现了奇怪的错误 import paramiko paramiko.common.logging.basicConfig(level=paramiko.common.INFO) paramiko.util.log_to_file('demo_router_simple.log') try: client = paramiko.SSHClient() client.set_missing

我正在尝试通过ssh登录网络设备。在执行第一个命令后,我获得了正确的输出,但在执行第二个命令时出现了奇怪的错误

import paramiko

paramiko.common.logging.basicConfig(level=paramiko.common.INFO)
paramiko.util.log_to_file('demo_router_simple.log')
try:
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    hostname='192.168.1.1'
    username='test'
    password='test'
    client.connect(hostname,username=username,password=password,look_for_keys = False,allow_agent = False)
    stdin, stdout, stderr = client.exec_command('show ip inter b')
    print stdout.read()
    # error occurs after I execute the second command
    stdin, stdout, stderr = client.exec_command('show ip inter b')
    print stdout.read()
finally:
    client.close()
奇怪的错误如下所示

DEB [20151125-09:26:42.841] thr=1   paramiko.transport: userauth is OK
INF [20151125-09:26:42.852] thr=1   paramiko.transport: Authentication (password) successful!
DEB [20151125-09:26:42.876] thr=2   paramiko.transport: [chan 0] Max packet in: 32768 bytes
DEB [20151125-09:26:42.883] thr=1   paramiko.transport: [chan 0] Max packet out: 4096 bytes
DEB [20151125-09:26:42.883] thr=1   paramiko.transport: Secsh channel 0 opened.
DEB [20151125-09:26:42.901] thr=1   paramiko.transport: [chan 0] Sesch channel 0 request ok
DEB [20151125-09:26:42.925] thr=2   paramiko.transport: [chan 1] Max packet in: 32768 bytes
DEB [20151125-09:26:42.925] thr=1   paramiko.transport: [chan 0] EOF received (0)
DEB [20151125-09:26:42.927] thr=1   paramiko.transport: [chan 0] EOF sent (0)
DEB [20151125-09:26:42.928] thr=1   paramiko.transport: Ignoring message for dead channel 0
DEB [20151125-09:26:42.928] thr=1   paramiko.transport: EOF in transport thread
我还打开路由器上的调试并捕获调试信息

*Mar  1 00:17:09.639: SSH2 0: done calc MAC out #11
*Mar  1 00:17:09.639: SSH0: Session terminated normally
 R1#
根据对这个称为exec_command的方法的解释,这个方法将在服务器上执行一个命令。如果服务器允许,则该通道将直接连接到正在执行的命令的stdin、stdout和stderr。当命令完成执行时,该通道将被关闭,无法重用。如果要执行另一个命令,必须打开一个新通道


让我困惑的是,为什么exec_命令关闭这个ssh连接而不是通道?还有其他人和我一样有同样的问题吗?任何帮助都将不胜感激。

为什么不试试布料呢。这样的工作要容易得多日志只用于第二个
exec\u命令
调用?还是两者兼得?如果两者都是,那么哪个部分用于哪个调用?paramiko日志用于两者,
[chan 0]
表示第一个exec_命令,
[chan 1]
表示第二个exec_命令。但是第二个命令执行失败。路由器日志显示第一个命令的进程,但会话通常在执行该命令后终止。