0

I'm trying to create a script that will bring up a vpn connection. I modeled my script from this question and it works fine for my purposes but I find that as soon as I close the script the vpn connection is interrupted. When using the "-b" option I get the following at the end of my child.read():

Established DTLS connection (using GnuTLS). Ciphersuite (DTLS0.9)-(RSA)-(AES-256-CBC)-(SHA1).\r\nSSL operation canceled\r\nUser detached from session (SIGHUP); exiting.\r\n'

Here is my code:

import os, sys, subprocess, time, re, pexpect
import signal

def signal_handler(sig, frame):
        print("sigHUUUUUP")
        sys.exit

child = pexpect.spawn('sudo openconnect -b --script /etc/vpnc/vpnc-script remote.host')

child.expect('.*')
child.sendline('yes')

child.expect('.*')
child.sendline('ipsec')

child.expect('.*')
child.sendline('username')

child.expect('.*')
child.sendline('password')
signal.signal(signal.SIGHUP, signal_handler)
time.sleep(15)

I have a strong preference to remain in python but I'm open to other ways of running openconnect and feeding it the expected passwords. Mainly looking for a way to have the vpn setup without needing to run the script continuously.

I've tried using ignore_sighup=True and that doesn't work.

LMP
  • 79
  • 1
  • 1
  • 9
  • `expect('.*')` is usually not correct. `.*` can match anything including nothing (empty string). – pynexj Aug 04 '18 at 01:04
  • yes I know - but that's not causing the problem because the password is accepted and the vpn connects for a second before a SIGHUP call occurs – LMP Aug 06 '18 at 15:45

1 Answers1

1

I have found a way accomplish what I wanted:

import os, sys, subprocess, time, re, pexpect
import signal

def signal_handler(sig, frame):
        print("sigHUUUUUP")
        sys.exit

child = pexpect.spawn('sudo screen openconnect remote.host')

child.expect('.*')
child.sendline('yes')

child.expect('.*')
child.sendline('ipsec')

child.expect('.*')
child.sendline('username')

child.expect('.*')
child.sendline('password')
child.sendline('\01d')

I added screen to my spawn line and added "child.sendline('\01d')" I hope this isn't the only way to accomplish this.

LMP
  • 79
  • 1
  • 1
  • 9