Question

I am trying to connect to an SSH on localhost using pexpect and trying to List files after establishing SSH session. The session gets established, but I don't get the output of 'ls'. I have been able to do it in FTP but not with SSH, here is the code:

import pexpect
import time
import sys
child=pexpect.spawn('ssh 127.0.0.1')
child.logfile=sys.stdout
child.expect('[P|p]assword')
child.sendline('abcd@123?')
child.expect('Welcome')
print child.before,
print child.after
time.sleep(3)
child.sendline('ls -l')
time.sleep(3)
child.expect('$')
print child.before,
print child.after
child.sendline('logout')
child.close

Here I tried to sleep the process also but it prints 'ls -l' in between the banner of SSH of localhost. But doesn't list the files. Plz provide some hint. Further plz suggest some good book or link for studying python in depth for pentesting purpose. Thanks alot...(I'll be online after 12 hours only, it is time to sleep)

Was it helpful?

Solution

The pattern passed to expect() is a regex, not a literal string. So $ matches the end of a string, which can be anywhere because of the way pexpect works.

You can either escape the dollar signs - \$ - or use expect_exact(), which takes a plain string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top