Get pexpect to work

drake at ultech.com drake at ultech.com
Sun Oct 29 16:26:47 EST 2006


Jurian Sluiman wrote:
> Ok, somebody helped my and found with "help(child.sendline)" that the
> number (7) is the number of characters from my password.
>
> Still there doesn't seem to be that anything strange is happening. With
> the logfile printed out, I found that child.expect places a 0 behind the
> next rule. Is this always what's happening? And is that 0 causing all my
> troubles?
>
> I'm a newbie with python, so I don't know much about it. This is (again)
> the output, but with a sys.stdout line between it:
>
> >>> child = pexpect.spawn("vpnc-connect tudelft\ nopass.conf")
> >>> child.logfile = sys.stdout
> >>> child.expect(".* password .*: ")
> Enter password for wb1264222 at luchtbrug.tudelft.nl: 0
> >>> child.sendline("[my_password]")
> 7
>
> Any help is really appreciated! I can search on the Internet, but with no
> clue to search for and which keywords to use, all results don't help me.
>
> Thanks,
> Jurian
>
> PS. Sorry for my bad English, I hope you can understand it.

I use a slightly different approach for starting vpnc-connect if that
will help:
1. have your script create a temporary vpnc configuration file
(including your password):
    f1=open('/tmp/vpn.conf', 'w')
    f1.write('IPSec gateway ' + vpnAddress + '\n')
    f1.write('IPSec ID ' + vpnGroup + '\n')....etc.
2. create a temporary results file, such as f2 =
open('/tmp/vpncResults.txt', 'w')
3. start the vpn client: p = subprocess.Popen('/usr/sbin/vpnc-connect
/tmp/vpn.conf', shell=True, stdout=f2).stdout
4. poll the results file to determine whether or not the connection
succeeded
5. delete the temporary configuration file

Creating a temporary configuration file keeps my password out of clear
text other than for the few seconds the configuration file lives on my
hard drive. In reality, my program runs within the Twisted event-driven
framework, so I just create a deferred object when invoking
vpnc-connect and wait for the callback to see if the connection was
successful, but an earlier incarnation of my program worked with the
above code.




More information about the Python-list mailing list