0

I started using the Pexpect library and for some reason I am having problems getting expressions to get matched.

For example in the following code

   import pexpect

   child=pexpect.spawn('su')
   i=child.expect_exact('Password:')
   print "value of i is %d" %i
   if i==1:
          p=input("Please enter root password : ")
          child.sendline(p)
          child.sendline('echo piggy')

problem i never equals 1

metric-space
  • 541
  • 4
  • 14
  • 3
    perhaps `su` doesn't work without a terminal? – Karoly Horvath Apr 24 '13 at 21:07
  • eh any documentation to back up your claim? – metric-space Apr 24 '13 at 21:10
  • it was just an idea... I assumed you tested it with some other app and that worked.. did you? :) – Karoly Horvath Apr 24 '13 at 21:13
  • Well I have no documentation to prove otherwise so was just asking.By other apps you mean direct posix calls and modules like subprocess...no.Tbh it's either that or I have no clue as to what you are asking.Pretty new to this biz just learnt about Pexpect an hour ago. – metric-space Apr 24 '13 at 21:15
  • @KarolyHorvath well you were right . – metric-space Apr 24 '13 at 21:19
  • I'd be surprised if pexpect doesn't start up a pseudotty. If it doesn't, you can use the pty module to get one. You probably should run your pexpect-utilizing process under strace -f to see what it's doing. In particular, there may be something on stderr that would be informative. – dstromberg Apr 24 '13 at 22:27
  • can anyone here give me resources to __understanding child processes and stuff related to it relevant to my question__ this is getting way beyond me. – metric-space Apr 24 '13 at 22:47

4 Answers4

2

su refuses to run when not invoked from a terminal:

$ echo blah | su
su: must be run from a terminal
mata
  • 67,110
  • 10
  • 163
  • 162
  • No friggin way ! well the code is legit .So well ..oh well isn't there any way to enter root mode via an automated root? – metric-space Apr 24 '13 at 21:18
  • Also a big plus for that not being possible. That would cause such a security threat and headaches :P –  Apr 24 '13 at 21:19
  • @nerorevenge run the script with sudo maybe? –  Apr 24 '13 at 21:20
0

IMO, when pexpect is the answer, you're asking the wrong question.

In this case, you could probably use sudo more effectively. It can be configured to run things as root, when invoked by a particular user, without demanding a password.

dstromberg
  • 6,954
  • 1
  • 26
  • 27
  • mate, could you dumbify what you typed for me please? – metric-space Apr 24 '13 at 22:56
  • Perhaps this will help: http://www.cyberciti.biz/tips/allow-a-normal-user-to-run-commands-as-root.html It's mostly a matter of putting the correct incantation in /etc/sudoers, and then running "sudo privileged_command" to confirm that it runs as root without requiring a password. – dstromberg Apr 25 '13 at 02:31
0

According to the documentation, when you pass a string to expect_exact(), it returns 0 on a match, not 1.

Have you tried comparing i to 0?

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • you mean to say you are referring to this line in the documentation __If the pattern was not a list this returns index 0 on a successful match__ , hmm never knew index 0 means number zero. – metric-space Apr 24 '13 at 22:53
0

First you login in super user use for this comment,pxssh is This class extends pexpect.spawn to specialize setting up SSH connections.This adds methods for login, logout, and expecting the shell prompt.

import pexpect
import pxssh

hostname ='172.16.0.120 -p 2222'
username = 'root'  # must you login in super user add this line
password = 'zilogic'
host = pxssh.pxssh()
host.login (hostname, username, password)
host.logfile = open(logfile, "w")
host.sendline ('whoami') 
print host.before

you will use for pxssh module don’t need for child.expect_exact('Password:'),this password expect take care of pxssh module see this link Python: How can remote from my local pc to remoteA to remoteb to remote c using Paramiko and How to login the super user(root) in remote host system using pexpect?

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