Get pexpect to work

J

Jurian Sluiman

Hi all,
For my first script, I'm trying to build a program to set up a vpn
connection. I'm using pexpect to handle this, but I can't get it to work.
The sendline() is causing troubles. I tested it in the interactive promt,
with these results:
import pexpect
child = pexpect.spawn("vpnc-connect tudelft\ nopass.conf")
child.expect(".* password .*: ") 0
child.sendline("[here_my_password]")
7

After this peace of code, I don't have a vpn connection. A "child.logfile
= sys.stdout" gave the information that after the spawn, vpnc was asking
for a password. I'm sure my password was correct, but my outside ip address
hasn't been changed (what should be with this connection) and there was no
vpnc process running.

What's going wrong?

Thanks in advance,
Jurian Sluiman
 
J

Jurian Sluiman

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 (e-mail address removed): 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.
 
D

drake

Jurian said:
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 (e-mail address removed): 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top