Skip to content

Instantly share code, notes, and snippets.

@paulbrodner
Last active June 29, 2022 08:24
Show Gist options
  • Select an option

  • Save paulbrodner/ab05c6f3e27e5ebe3c28e0ce65fd4eba to your computer and use it in GitHub Desktop.

Select an option

Save paulbrodner/ab05c6f3e27e5ebe3c28e0ce65fd4eba to your computer and use it in GitHub Desktop.

about

choose between already defined VPN connections and automatically logs in!

OS: tested on windows only

Pay attention that this script will focus the VPN dialog and type the password. If the focus is lost, the script can type and sent the password to active windows - so if you run the script -> don't click on other applications, use it with caution

prerequisites

  • you need pyautogui installed:
pip install pyautogui

steps

a) create one file vpn-connect.py with the content of this file

pwd="add-your-password-here"

from time import sleep
import pyautogui
import subprocess

current_connection = subprocess.Popen("rasdial", shell=True, stdout=subprocess.PIPE).stdout.read()
print(current_connection)

# VPN-Name sis the actual VPN name
connections = [
    "VPN-Name1",
    "VPN-Name2",
    "VPN-Name3"
    ]

index = 1
print("Available VPN Connections:")
for connection in connections:
    print("\t{} - {}".format(index, connection))
    index+=1


conn_id = raw_input('\t What VPN do you want to setup?: ')
chosen_connection = connections[int(conn_id)-1]

## use this if you alrady have your password set in settings.xml file so we can reuse it ;)
#cmd =  "cat ~/.m2/settings.xml | grep 'password' | head -n 1 "
#output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read()
#pwd=output.split(" <password>")[-1].split("</password>")[0]


subprocess.Popen('rasphone -d "{}"'.format(chosen_connection), shell=True, stdout=subprocess.PIPE)
sleep(2)
pyautogui.typewrite('{}\n'.format(pwd))

# rasdial 
# rasdial "The VPN" /DISCONNECT

b) [optional ]create a new BAT file: vpn.bat with the content of this file

or choose another approach to run the vpn-connect.py

# this will use powershell to execute the python script
# you can create a shortcut for this on your desktop
powershell.exe -ExecutionPolicy Bypass -Command "python.exe vpn-connect.py"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment