Python ssh会话未激活paramiko
我有一段代码,试图通过ssh使用Python的paramiko库连接到Windows机器。我第一次连接时,它是成功的。但是第二次,我得到错误“SSH会话未激活”。是因为我试图在两个不同的功能中使用它吗?代码如下所示:Python ssh会话未激活paramiko,python,session,ssh,paramiko,Python,Session,Ssh,Paramiko,我有一段代码,试图通过ssh使用Python的paramiko库连接到Windows机器。我第一次连接时,它是成功的。但是第二次,我得到错误“SSH会话未激活”。是因为我试图在两个不同的功能中使用它吗?代码如下所示: class setup():#gets executed first @test.subsection def test_connection(self): #connecting first time ssh = paramiko.
class setup():#gets executed first
@test.subsection
def test_connection(self):
#connecting first time
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("8.29.9.131", username = "user", password = "pass")
ssh.close()
class test1():#gets executed next
@test.setup
def setup(self): #runs first
#connecting second time
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh.connect("8.29.9.131", username = "user", password = "pass")
@test.maintest
def main_test(self): #runs after setup section above
in, out, err = self.ssh.exec_command("cmd /c C:\\\Python27\\\Scripts\\\module.py")
print out.readlines()
self.ssh.close()
2015-01-20T15:42:23: %test-ERROR: stdin, stdout, stderr = self.ssh.exec_command("cmd /c C:\\\Python27\\\Scripts\\\module.py")
2015-01-20T15:42:23: %test-ERROR: File "build/bdist.linux- x86_64/egg/paramiko/client.py", line 341, in exec_command
2015-01-20T15:42:23: %test-ERROR: chan = self._transport.open_session()
2015-01-20T15:42:23: %test-ERROR: File "build/bdist.linux-x86_64/egg/paramiko/transport.py", line 615, in open_session
2015-01-20T15:42:23: %test-ERROR: max_packet_size=max_packet_size)
2015-01-20T15:42:23: %test-ERROR: File "build/bdist.linux-x86_64/egg/paramiko/transport.py", line 696, in open_channel
2015-01-20T15:42:23: %test-ERROR: raise SSHException('SSH session not active')
回溯如下所示:
class setup():#gets executed first
@test.subsection
def test_connection(self):
#connecting first time
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("8.29.9.131", username = "user", password = "pass")
ssh.close()
class test1():#gets executed next
@test.setup
def setup(self): #runs first
#connecting second time
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh.connect("8.29.9.131", username = "user", password = "pass")
@test.maintest
def main_test(self): #runs after setup section above
in, out, err = self.ssh.exec_command("cmd /c C:\\\Python27\\\Scripts\\\module.py")
print out.readlines()
self.ssh.close()
2015-01-20T15:42:23: %test-ERROR: stdin, stdout, stderr = self.ssh.exec_command("cmd /c C:\\\Python27\\\Scripts\\\module.py")
2015-01-20T15:42:23: %test-ERROR: File "build/bdist.linux- x86_64/egg/paramiko/client.py", line 341, in exec_command
2015-01-20T15:42:23: %test-ERROR: chan = self._transport.open_session()
2015-01-20T15:42:23: %test-ERROR: File "build/bdist.linux-x86_64/egg/paramiko/transport.py", line 615, in open_session
2015-01-20T15:42:23: %test-ERROR: max_packet_size=max_packet_size)
2015-01-20T15:42:23: %test-ERROR: File "build/bdist.linux-x86_64/egg/paramiko/transport.py", line 696, in open_channel
2015-01-20T15:42:23: %test-ERROR: raise SSHException('SSH session not active')
我没有使用您正在使用的测试框架,尽管在相当广泛的Python“unittest”框架中,我希望类似的代码可以工作,test1的self.ssh可供main_测试进一步使用。您还使用了一个非常旧的类系统,因为您没有我所期望的“类test1(object)”或更普通的“类TestSshToPc(unittest.TestCase)”。