Python Pexpect+;长字符串的pxssh处理

Python Pexpect+;长字符串的pxssh处理,python,pexpect,pxssh,Python,Pexpect,Pxssh,我正在使用pexpect+pxsh与远程服务器通信。SOME命令可能有点长,以下代码不起作用: 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 12345

我正在使用pexpect+pxsh与远程服务器通信。SOME命令可能有点长,以下代码不起作用:

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
由于expect函数,它会创建一个超时。命令
'echo 1234567890>log.txt 2>&1'
不会产生任何问题。在长路径位置执行短命令时也会出现此问题,例如

sendline('cd folder1')
sendline('cd folder2')
...
sendline('cd foldern')
pexpect('cd foldern')
如果folder1/folder2/../folder的长度太长,expect也不会工作,因为显示当前路径的“提示”的长度太长

以下是代码生成的输出:

/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): ": \r\nWelcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.16.0-30-generic x86_64)\r\n\r\n * Documentation:  https://help.ubuntu.com/\r\n\r\n517 packages can be updated.\r\n296 updates are security updates.\r\n\r\nNew release '16.04.1 LTS' available.\r\nRun 'do-release-upgrade' to upgrade to it.\r\n\r\nLast login: Mon Feb  6 16:56:30 2017 from 192.168.93.130\r\r\ntester@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.                         \x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08txt 2>&1\r\ntester@ubuntu:~$ '
before (last 100 chars): ' echo 1234567890abcdefghojklmnopqrstuvwxyzmyveryverylongawork\r<67890abcdefghojklmnopqrstuvwxyzmyveryverylongaworks                         \x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08paceaoneamoreafolder >log\r\r<vwxyzmyveryverylongaworkspaceaoneamoreafolder >log.                         \x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08txt 2>&1\r\ntester@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
/usr/bin/python2.7/data/eskenazi/stash/alb tools/testing\u framework/tests/build/remote\u build\u test.py
版本:3.1
命令:/usr/bin/ssh
参数:['/usr/bin/ssh','-l','tester','192.168.93.129']
搜索者:
缓冲区(最后100个字符):“”
之前(最后100个字符):“:\r\n请访问Ubuntu 14.04.2 LTS(GNU/Linux 3.16.0-30-generic x86_64)\r\n\r\n*文档:https://help.ubuntu.com/\r\n\r\n517包可以更新。\r\n296更新是安全更新。\r\n\r\n新版本“16.04.1 LTS”可用。\r\n运行“do release upgrade”升级到它。\r\n\r\n上次登录:2017年2月6日星期一16:56:30,从192.168.93.130开始\r\n\ntester@ubuntu:~"
之后:“$”
匹配:
匹配索引:1
退出状态:无
flag_eof:False
pid:7004
儿童:3
关闭:错误
超时时间:30
分隔符:
日志文件:无
日志文件读取:无
日志文件发送:无
maxread:2000
忽略情况:错误
searchwindowsize:无
发送前延迟:0.05
延迟后关闭:0.1
延迟后终止:0.1
回溯(最近一次呼叫最后一次):
文件“/data/eskenazi/stash/alb tools/testing_framework/tests/build/remote_build_test.py”,第23行,在
index=channel.expect(['echo 1234567890abcdefghojklmnopqrstuvxyzmyverylongaworkspaceaonemoreafolder>log.txt 2>&1',超时=10,)
expect中的文件“/usr/lib/python2.7/dist packages/pexpect/_init__.py”,第1417行
超时,搜索窗口大小)
expect\u列表中的第1431行文件“/usr/lib/python2.7/dist packages/pexpect/\uuuuu init\uuuuuuuuuu.py”
超时,搜索窗口大小)
expect\u循环中的第1542行文件“/usr/lib/python2.7/dist packages/pexpect/\uuuu init\uuuuuu.py”
提升超时(str(err)+'\n'+str(self))
pexpect.TIMEOUT:超过超时时间。
版本:3.1
命令:/usr/bin/ssh
参数:['/usr/bin/ssh','-l','tester','192.168.93.129']
搜索者:
缓冲区(最后100个字符):'eaoneamoreafolder>log\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08txt 2>&1\r\n\ntester@ubuntu:~$ '
之前(最后100个字符):'echo 1234567890abcdefghojklmnopqrstuvxyzmyverylongawork\rlog\r\rlog\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08txt 2>&1\r\n\ntester@ubuntu:~$ '
之后:
匹配:无
匹配索引:无
退出状态:无
flag_eof:False
pid:7004
儿童:3
关闭:错误
超时时间:30
分隔符:
日志文件:无
日志文件读取:无
日志文件发送:无
maxread:2000
忽略情况:错误
searchwindowsize:无
发送前延迟:0.05
延迟后关闭:0.1
延迟后终止:0.1
进程已完成,退出代码为1
如果您能帮助解决这个问题,我们将不胜感激


SebE自己找到了答案。我认为这与pexpect打开的命令行窗口的长度有关。默认值为80个字符。您可以在创建后立即使用
channel.setwinsize(24,channel.maxread)


公认的答案是错误的

  • 您必须在登录后添加对setwinsize的调用
  • 调用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
    version
    4.6.0
    上尝试,得到一个
    AttributeError
    表示“pxsh”对象没有属性“ptyproc”`
        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()