我正在尝试运行一个python脚本,这个脚本从linux机器运行到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机器和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
发布于 2017-07-12 19:34:40
如下所示设置登录参数解决了我的问题
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方法,在字符串的开头和结尾添加回车
https://stackoverflow.com/questions/45046152
复制相似问题