1

import pexpect, os, sys

cmd = "sudo su -"

p = pexpect.spawn(cmd, timeout=60)

pass_expect = ".*assword.*" (as I get a prompt saying "[sudo] password for user" )

p.expect(pass_expect)

print "I want the password"

p.sendline("mypassword")

p.logfile = sys.stdout

p.sendline("logout")

p.close()

I am getting the following error while executing the above script

Error

Traceback (most recent call last):

File "test.py", line 11, in p.expect(pass_expect)

File "/usr/lib/python2.6/dist-packages/pexpect.py", line 1311, in expect

return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)

File "/usr/lib/python2.6/dist-packages/pexpect.py", line 1325, in expect_list

return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)

File "/usr/lib/python2.6/dist-packages/pexpect.py", line 1409, in expect_loop

raise TIMEOUT (str(e) + '\n' + str(self))

pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().

version: 2.3 ($Revision: 399 $)

command: /usr/bin/sudo

args: ['/usr/bin/sudo', 'su', '-']

searcher: searcher_re:

0: re.compile(".*assword.*")

buffer (last 100 chars): root@:~#

before (last 100 chars): root@:~#

after:

match: None match_index: None exitstatus: None flag_eof: False pid: 16933 child_fd: 3 closed: False timeout: 60

delimiter: logfile: None logfile_read: None logfile_send: None maxread: 2000 ignorecase: False searchwindowsize: None delaybeforesend: 0.05 delayafterclose: 0.1 delayafterterminate: 0.1

Can someone please help me out as I am been stuck in this problem from few days.

Any help will be appreciated. Thanks !!

sunny
  • 11
  • 6

1 Answers1

0

If you login as a superuser means put root@localhost

child = pexpect.spawn("ssh root@localhost")
child.logfile = open("/tmp/mylog", "w")
child.expect(".*assword:")
child.send("guest\r")
child.expect(".*\$ ")
child.sendline("python -V\r")
print child.before

How to login the super user(root) in remote host system using pexpect?

Community
  • 1
  • 1
Reegan Miranda
  • 2,879
  • 6
  • 43
  • 55