Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ssh remote machine and run the 'ls-l' using pexpect

Tags:

python

I wanted to ssh remote machine and run the ls-l using pexpect. i am a system engineer learning the python language and don't have knowledge on coding. Could someone please assist me in this. Thanks in advance.

My code:

import pexpect
child = pexpect.spawn('/usr/bin/ssh [email protected]')
child.expect('password:', timeout=120)
child.sendline('pass123')
child.expect ('prompt# ')
#child.maxread=100000
child.sendline ('uname -a')
child.expect ('prompt# ')
print child.before, child.after

Below is the error output when run the above code.

usr/bin/python /root/PycharmProjects/IS_LAB/pexpect-test.py
Traceback (most recent call last):
  File "/root/PycharmProjects/IS_LAB/pexpect-test.py", line 36, in <module>
    child.expect ('prompt# ')
  File "/usr/lib/python2.6/site-packages/pexpect/__init__.py", line 1451, in expect
    timeout, searchwindowsize)
  File "/usr/lib/python2.6/site-packages/pexpect/__init__.py", line 1466, in expect_list
    timeout, searchwindowsize)
  File "/usr/lib/python2.6/site-packages/pexpect/__init__.py", line 1568, in expect_loop
    raise TIMEOUT(str(err) + '\n' + str(self))
pexpect.TIMEOUT: Timeout exceeded.
<pexpect.spawn object at 0x9b4110>
version: 3.3
command: /usr/bin/ssh
args: ['/usr/bin/ssh', '[email protected]']
searcher: <pexpect.searcher_re object at 0x9b4450>
buffer (last 100 chars): 'cape.canonical.com/\r\n\r\nLast login: Tue Jun 16 10:26:18 2015 from 192.168.32.1\r\r\nroot@ubuntu14:~# '
before (last 100 chars): 'cape.canonical.com/\r\n\r\nLast login: Tue Jun 16 10:26:18 2015 from 192.168.32.1\r\r\nroot@ubuntu14:~# '
after: <class 'pexpect.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 13015
child_fd: 3
closed: False
timeout: 30
delimiter: <class 'pexpect.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1

Process finished with exit code 1
like image 780
eng Avatar asked Feb 09 '23 20:02

eng


1 Answers

Here is what is going on with your code, it may help you understand why your script is not working:

import pexpect

child = pexpect.spawn('/usr/bin/ssh [email protected]')

# This line means, "wait until you see a string that matches password:"
# in the response
child.expect('password:', timeout=120) 

child.sendline('pass123') # Send the characters pass123 and "enter"

# Wait till you see a string matching prompt#
child.expect ('prompt# ')

At this line, your script is searching for the string prompt#, but what the server is returning is root@ubuntu14:~#. As this doesn't match what you have provided for the script to check for, it raises an exception, which basically means "I waited for TIMEOUT period for a string to match your pattern, but I didn't find it."

To solve the problem you can either enter the exact prompt string for your script to search for, like this:

child.sendline('pass123') # Send the characters pass123 and "enter"

# Wait till you see a string matching ~#
child.expect('~#')
child.sendline ('uname -a')

child.expect ('~#')
print child.before, child.after

Or simply pause your script for a few seconds:

import time

child.sendline('pass123') # Send the characters pass123 and "enter"
time.sleep(10)

child.sendline('uname -a')
time.sleep(10)

print child.before, child.after
like image 74
Burhan Khalid Avatar answered Feb 12 '23 09:02

Burhan Khalid