Get pexpect to work

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jurian Sluiman

    #1

    Get pexpect to work

    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.sendlin e("[here_my_passwor d]")
    7

    After this peace of code, I don't have a vpn connection. A "child.logf ile
    = 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
  • Jurian Sluiman

    #2
    Re: Get pexpect to work

    Ok, somebody helped my and found with "help(child.sen dline)" 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.logfi le = sys.stdout
    >>child.expect( ".* password .*: ")
    Enter password for wb1264222@lucht brug.tudelft.nl: 0
    >>child.sendlin e("[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.

    Comment

    • drake@ultech.com

      #3
      Re: Get pexpect to work


      Jurian Sluiman wrote:
      Ok, somebody helped my and found with "help(child.sen dline)" 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.logfil e = sys.stdout
      >child.expect(" .* password .*: ")
      Enter password for wb1264222@lucht brug.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.Pope n('/usr/sbin/vpnc-connect
      /tmp/vpn.conf', shell=True, stdout=f2).stdo ut
      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.

      Comment

      Working...