繁体   English   中英

当我从Linux机器尝试到Windows ssh客户端服务器时,来自pexpect(pxssh)的SSH失败

[英]SSH from pexpect (pxssh) fails when I try from a linux machine to a windows ssh client server

我正在尝试运行一个python脚本,该脚本从linux机器SSH到Windows服务器并运行一个批处理文件。

经过研究,我意识到来自pexpect的pxssh类是要使用的ood模块。 当我在linux上尝试将此模块ssh到linux计算机时,没有问题。 当我从linux到Windows进行操作时,它失败并显示以下错误:pxssh登录失败。

pxssh failed on login.
could not set shell prompt (received: ": \r\n\x1b[2J\x1b[1HMicrosoft Windows [Version 10.0.14393]\r\n(c) 2016 Microsoft Corporation. All rights reserved.\r\n\r\nC:\\Users\\myname>unset PROMPT_COMMAND\nPS1='[PEXPECT]\\$ '\nset prompt='[PEXPECT]\\$ '\n", expected: '\\[PEXPECT\\][\\$\\#] ').

我记得从Windows到Linux有不同的结束行方式(如回车等)之前。 我想知道是否仍然可以解决此问题。 请注意,我可以手动使用从linux机器和ssh到Windows机器的shell,只有当我尝试编写python脚本并使用pxssh时,它才会失败。 谢谢您的帮助。 请注意,我的下一步是运行批处理文件并关闭连接。 我的简单脚本:

from pexpect import pxssh
import getpass
try:                                                            
    s = pxssh.pxssh()
    #hostname = raw_input('hostname: ')
    #username = raw_input('username: ')
    #password = getpass.getpass('password: ')
    s.login ('192.168.0.144', 'username', 'password',  auto_prompt_reset=True)
    s.sendline ('dir')
    s.prompt()             # match the prompt
    s.sendline ('exit')
    s.logout()
    print "I'm here"
except pxssh.ExceptionPxssh, e:
    print "pxssh failed on login."
    print str(e)

我使用退出而不是注销,因为我认为Windows上的ssh客户端服务器不支持注销

编辑:我已经禁用了auto_prompt_reset现在我得到以下信息:

Traceback (most recent call last):
  File "sshLogin.py", line 22, in <module>
    s.logout()
  File "/usr/local/lib/python2.7/dist-packages/pexpect/pxssh.py", line 355, in logout
    index = self.expect([EOF, "(?i)there are stopped jobs"])
  File "/usr/local/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 321, in expect
    timeout, searchwindowsize, async)
  File "/usr/local/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 345, in expect_list
    return exp.expect_loop(timeout)
  File "/usr/local/lib/python2.7/dist-packages/pexpect/expect.py", line 107, in expect_loop
    return self.timeout(e)
  File "/usr/local/lib/python2.7/dist-packages/pexpect/expect.py", line 70, in timeout
    raise TIMEOUT(msg)
pexpect.exceptions.TIMEOUT: Timeout exceeded.
<pexpect.pxssh.pxssh object at 0xb72491cc>
command: /usr/bin/ssh
args: ['/usr/bin/ssh', 'username@192.168.0.144']
buffer (last 100 chars): 'Version 10.0.14393]\r\n(c) 2016 Microsoft Corporation. All rights reserved.\r\n\r\nC:\\Users\\myname>exit\n'
before (last 100 chars): 'Version 10.0.14393]\r\n(c) 2016 Microsoft Corporation. All rights reserved.\r\n\r\nC:\\Users\\myname>exit\n'
after: <class 'pexpect.exceptions.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 4523
child_fd: 5
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
searcher: searcher_re:
    0: EOF
    1: re.compile("(?i)there are stopped jobs")

一些可能有用的资源: http : //pexpect.readthedocs.io/en/stable/overview.html

如下设置登录参数解决了我的问题

s.login(server= hostname, username=username, password='', terminal_type='ansi',
            original_prompt=r"[#$]", login_timeout=10, port=22,
            auto_prompt_reset=False, ssh_key=None, quiet=True,
            sync_multiplier=1, check_local_ip=True)

但是,连接仍然很慢,我无法远程启动程序。 另一个提示:为了使用sendline方法,在字符串的开头和结尾添加回车

暂无
暂无

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

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