paramiko ssh 通道在执行一个命令后关闭 - Python
paramiko's ssh channel closed after executing one cmd - Python
我正在使用 Python
的 Paramiko
在删除服务器中执行命令。代码简单多了。
这是我对 SSHConn
class 的定义:
class SSHConn:
def __init__(self, hostname, user, pwd, filename=None):
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(hostname, username=user, password=pwd,
key_filename=filename)
self.transport = self.client.get_transport()
self.channel = self.transport.open_session()
所以我运行下面的代码:
local_ssh = ssh.SSHConn(host, user, passwd)
cmds = ('foo', 'bar')
for cmd in cmds:
local_ssh.channel.exec_command(cmd)
self.log.info(local_ssh.channel.recv(1024))
但是,当我执行以下代码片段时,我得到:
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_6.6.1p1)
INFO:paramiko.transport:Authentication (publickey) failed.
INFO:paramiko.transport:Authentication (password) successful!
INFO:paramiko.transport:Secsh channel 1 opened.
INFO:testsets.testcase:
ERROR:testsets.testcase:Channel is not open
Traceback (most recent call last):
File "/root/fds-src/source/test/integration-framework/testsets/testcases/test_100gb_volume.py", line 87, in runTest
local_ssh.channel.exec_command(cmd)
File "/usr/lib/python2.7/dist-packages/paramiko/channel.py", line 209, in exec_command
raise SSHException('Channel is not open')
SSHException: Channel is not open
ERROR:testsets.testcase:Test Case Test100GBVolume failed.
Traceback (most recent call last):
File "/root/fds-src/source/test/integration-framework/testsets/testcases/test_100gb_volume.py", line 87, in runTest
local_ssh.channel.exec_command(cmd)
File "/usr/lib/python2.7/dist-packages/paramiko/channel.py", line 209, in exec_command
raise SSHException('Channel is not open')
SSHException: Channel is not open
如何保持频道畅通?
channel
文档对这一点说的很清楚。您需要为每个 exec_command
.
打开一个新频道
exec_command(*args, **kwds)
Execute a command on the server. If the server allows it, the channel will then be directly connected to the stdin, stdout, and stderr of the command being executed.
When the command finishes executing, the channel will be closed and can’t be reused. You must open a new channel if you wish to execute another command.
SSHClient
对象有一个 exec_command
方法可以为您执行此操作。
我正在使用 Python
的 Paramiko
在删除服务器中执行命令。代码简单多了。
这是我对 SSHConn
class 的定义:
class SSHConn:
def __init__(self, hostname, user, pwd, filename=None):
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(hostname, username=user, password=pwd,
key_filename=filename)
self.transport = self.client.get_transport()
self.channel = self.transport.open_session()
所以我运行下面的代码:
local_ssh = ssh.SSHConn(host, user, passwd)
cmds = ('foo', 'bar')
for cmd in cmds:
local_ssh.channel.exec_command(cmd)
self.log.info(local_ssh.channel.recv(1024))
但是,当我执行以下代码片段时,我得到:
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_6.6.1p1)
INFO:paramiko.transport:Authentication (publickey) failed.
INFO:paramiko.transport:Authentication (password) successful!
INFO:paramiko.transport:Secsh channel 1 opened.
INFO:testsets.testcase:
ERROR:testsets.testcase:Channel is not open
Traceback (most recent call last):
File "/root/fds-src/source/test/integration-framework/testsets/testcases/test_100gb_volume.py", line 87, in runTest
local_ssh.channel.exec_command(cmd)
File "/usr/lib/python2.7/dist-packages/paramiko/channel.py", line 209, in exec_command
raise SSHException('Channel is not open')
SSHException: Channel is not open
ERROR:testsets.testcase:Test Case Test100GBVolume failed.
Traceback (most recent call last):
File "/root/fds-src/source/test/integration-framework/testsets/testcases/test_100gb_volume.py", line 87, in runTest
local_ssh.channel.exec_command(cmd)
File "/usr/lib/python2.7/dist-packages/paramiko/channel.py", line 209, in exec_command
raise SSHException('Channel is not open')
SSHException: Channel is not open
如何保持频道畅通?
channel
文档对这一点说的很清楚。您需要为每个 exec_command
.
exec_command(*args, **kwds)
Execute a command on the server. If the server allows it, the channel will then be directly connected to the stdin, stdout, and stderr of the command being executed.
When the command finishes executing, the channel will be closed and can’t be reused. You must open a new channel if you wish to execute another command.
SSHClient
对象有一个 exec_command
方法可以为您执行此操作。