我试图运行一个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到shell机器的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
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.
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:
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 4523
child_fd: 5
closed: False
timeout: 30
delimiter:
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