我正在使用pexpect+pxssh与远程服务器通信。Somme 命令可能有点长,以下代码不起作用:
import pxssh
channel = pxssh.pxssh()
channel.login('192.168.93.129', 'tester', 'tester', 22, auto_prompt_reset=False, quiet=False)
channel.PROMPT = 'tester' + "@[^#$]+[#$]"
channel.sendline('echo 1234567890abcdefghojklmnopqrstuvwxyzmyveryverylongaworkspaceaoneamoreafolder >log.txt 2>&1')
print channel
index = channel.expect(['echo 1234567890abcdefghojklmnopqrstuvwxyzmyveryverylongaworkspaceaoneamoreafolder >log.txt 2>&1'], timeout=10, )
print channel
由于期望函数,它会产生超时。命令'echo 1234567890 >log.txt 2>&1'
完全没有问题。在长路径位置的短命令中也会发生此问题,例如
sendline('cd folder1')
sendline('cd folder2')
...
sendline('cd foldern')
pexpect('cd foldern')
如果文件夹 1/文件夹 2/.../文件夹的长度太长,由于显示当前路径的"提示"的长度,期望也不起作用。
下面是代码生成的输出:
/usr/bin/python2.7 /data/eskenazi/stash/alb-tools/testing_framework/tests/build/remote_build_test.py
<pexpect.pxssh.pxssh object at 0x7f6402096990>
version: 3.1
command: /usr/bin/ssh
args: ['/usr/bin/ssh', '-l', 'tester', '192.168.93.129']
searcher: <pexpect.searcher_re object at 0x7f6402096a50>
buffer (last 100 chars): ' '
before (last 100 chars): ": rnWelcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.16.0-30-generic x86_64)rnrn * Documentation: https://help.ubuntu.com/rnrn517 packages can be updated.rn296 updates are security updates.rnrnNew release '16.04.1 LTS' available.rnRun 'do-release-upgrade' to upgrade to it.rnrnLast login: Mon Feb 6 16:56:30 2017 from 192.168.93.130rrntester@ubuntu:~"
after: '$'
match: <_sre.SRE_Match object at 0x7f640209a850>
match_index: 1
exitstatus: None
flag_eof: False
pid: 7004
child_fd: 3
closed: False
timeout: 30
delimiter: <class 'pexpect.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
Traceback (most recent call last):
File "/data/eskenazi/stash/alb-tools/testing_framework/tests/build/remote_build_test.py", line 23, in <module>
index = channel.expect(['echo 1234567890abcdefghojklmnopqrstuvwxyzmyveryverylongaworkspaceaoneamoreafolder >log.txt 2>&1'], timeout=10, )
File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1417, in expect
timeout, searchwindowsize)
File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1431, in expect_list
timeout, searchwindowsize)
File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1542, in expect_loop
raise TIMEOUT(str(err) + 'n' + str(self))
pexpect.TIMEOUT: Timeout exceeded.
<pexpect.pxssh.pxssh object at 0x7f6402096990>
version: 3.1
command: /usr/bin/ssh
args: ['/usr/bin/ssh', '-l', 'tester', '192.168.93.129']
searcher: <pexpect.searcher_re object at 0x7f6402096a10>
buffer (last 100 chars): 'eaoneamoreafolder >log. x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08txt 2>&1rntester@ubuntu:~$ '
before (last 100 chars): ' echo 1234567890abcdefghojklmnopqrstuvwxyzmyveryverylongaworkr<67890abcdefghojklmnopqrstuvwxyzmyveryverylongaworks x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08paceaoneamoreafolder >logrr<vwxyzmyveryverylongaworkspaceaoneamoreafolder >log. x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08x08txt 2>&1rntester@ubuntu:~$ '
after: <class 'pexpect.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 7004
child_fd: 3
closed: False
timeout: 30
delimiter: <class 'pexpect.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
Process finished with exit code 1
任何帮助将不胜感激,以解决此问题。
塞布·
接受的答案不正确。
- 您必须在登录后添加对setwinsize ONE的调用。
-
您必须在调用 setwinsize 后匹配提示。
s = pxssh.pxssh() hostname = ipaddress username = ssh_username password = ssh_pass print("############ Logging into remote machine ############") s.login(hostname, username, password) print("logged into host: " + hostname) print("setting winsize") s.setwinsize(100, 1000) s.prompt()
在登录前调用 setwinsize 会给你错误消息"pxssh"对象没有属性"ptyproc">
自己找到了答案。因为我认为这与 pexpect 打开的命令行窗口的长度有关。默认值为 80 个字符。您可以在创建后立即使用它进行配置channel.setwinsize(24, channel.maxread)
import pxssh
channel = pxssh.pxssh()
channel.setwinsize(24, channel.maxread)
channel.login('192.168.93.129', 'tester', 'tester', 22, auto_prompt_reset=False, quiet=False)
channel.PROMPT = 'tester' + "@[^#$]+[#$]"
channel.sendline('echo 1234567890abcdefghojklmnopqrstuvwxyzmyveryverylongaworkspaceaoneamoreafolder >log.txt 2>&1')
print channel
index = channel.expect(['echo 1234567890abcdefghojklmnopqrstuvwxyzmyveryverylongaworkspaceaoneamoreafolder >log.txt 2>&1'], timeout=10, )
print channel