【发布时间】:2017-12-16 04:49:43
【问题描述】:
我正在尝试运行一个 python 脚本,该脚本从 linux 机器 ssh 到 windows 服务器并运行一个批处理文件。
经过一番研究,我意识到来自 pexpect 的 pxssh 类是一个很好用的模块。当我在 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 机器上的 shell 和 ssh 到 windows 机器,只有当我尝试编写 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
【问题讨论】:
-
"...从之前的windows有不同的结束行的方式和linux这样的(回车等)。我想知道是否知道这个问题的解决方案。" - SSH 协议(有多个 RFC)使用
CRLF作为行尾(CRLF是 WIndows;Linux 是LF;OS X 是CR)。CRLF的使用与其他 RFC 一致,例如 Telnet、Privacy Enhanced Mail 和 FTP。这些是一些最古老的 RFC,它们大约有 30 或 40 年的历史。如果不能处理CRLF,这听起来像是Python软件坏了。但我不相信行尾是问题所在。 -
pxssh failed on login- 我猜测(这只是猜测),Windows 上的密码不是您所期望的。你可以放弃密码认证,使用公钥认证吗?如果不是,您能否验证或确认密码是您所期望的?对于问题(2),验证或确认密码,它可能不是您用于登录机器的 Windows 密码,除非您专门设置了它。 -
@jww 我真的不认为登录有任何问题。从缓冲区中可以看出,shs 能够检索到一些信息:版本 10.0.14393]\r\n(c) 2016 Microsoft Corporation。保留所有权利。\r\n\r\nC:\\Users\\myname>exit\n
-
我不知道它是如何工作的: original_prompt=r"[#$]" 这是登录方法的输入参数
标签: python linux windows ssh pxssh