I'm trying to run a python script which ssh from a linux machine to a windows server and runs a batch file.
After some research, I realized that a pxssh class from pexpect is a ood module to use. When I try this module on linux to ssh to linux machine there is no problem. When I do it from a linux to windows it fails with the following error : pxssh failed on login.
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\\][\\$\\#] ').
I remember from before windows had different way of ending lines and such (carriage return etc) from linux. I would like to know if anyway knows a solution to this problem. Please note that I'm manually able to use a shell from linux machine and ssh to a windows machine, it only fails when I try to write a python script and use pxssh. Thank you for your help. Please note that my next step would be to run a batch file and close the connection. My simple script:
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)
I use exit instead of logout because I think ssh client server on windows doesn't support logout
Edit: I have disabled the auto_prompt_reset now I got the following:
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")
Some resource which might be useful: http://pexpect.readthedocs.io/en/stable/overview.html
Setting the login parameters as follow solved my problem
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)
However, the connection is still very slow and I cannot launch a program remotely. Also another hint: In order to use the sendline method add carriage to the beginning and end of your string