简体   繁体   English

Pxssh重新连接Python(pexpect)

[英]Pxssh reconnect Python (pexpect)

I need to do more then one connection in one script using pxssh . 我需要使用pxssh在一个脚本中完成一个以上的连接。 Trying to do like this: 尝试这样做:

import pxssh
ip = 'ip'
username = 'username'
password = 'password'
s = pxssh.pxssh()
s.login (ip, username, password)
s.sendline('command')
s.prompt()
print s.before
s.logout()
s.login(ip2, username, password)
etc

But getting error on the second connection: AssertionError: The pid member must be None. 但是在第二个连接上出现错误: AssertionError: The pid member must be None. Only one connection per time is passing. 每次仅通过一个连接。 How to get it work? 如何运作?

You just need to create a new pxssh object. 您只需要创建一个新的pxssh对象。 The constructor for the pxssh object creates a process and spawns ssh. pxssh对象的构造函数创建一个进程并生成ssh。 Logout disconnects from the remote system, which makes the connection useless but doesn't reset the object. 注销会与远程系统断开连接,这会使连接无效,但不会重置对象。 Something like this: 像这样:

...
s.logout()
s.close()
s = pxssh.pxssh()
s.login(ip2, ...)

The s.close() is not strictly necessary but it's a good idea as otherwise, the underlying file descriptor will not be closed until the original object is garbage-collected. s.close()不是严格必需的,但是它是一个好主意,因为否则,在原始对象被垃圾回收之前,基础文件描述符将不会关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM