Created
September 25, 2025 14:39
-
-
Save firedotguy/deaa67fc316d0ed601a5b5f2a0783ee6 to your computer and use it in GitHub Desktop.
Automatically connect to OpenConnectVPN GUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from os import startfile, system | |
| from time import sleep | |
| from importlib.util import find_spec | |
| REQUIREMENTS = ['pyautogui', 'win32gui', 'pyuac'] | |
| if None in [find_spec(req) for req in REQUIREMENTS]: | |
| print('some of requirements is not installed') | |
| print('installing requirements:', ' '.join([req for req in REQUIREMENTS if find_spec(req) is None])) | |
| system('pip install pyautogui pywin32 pyuac') | |
| print('installed') | |
| from pyautogui import click, write | |
| from win32gui import FindWindow, SetForegroundWindow, PostMessage | |
| from pyuac import isUserAdmin, runAsAdmin | |
| from win32con import WM_CLOSE | |
| PASSWORD = 'your-openconnect-password' | |
| EXECUTABLE = r'C:\Program Files\OpenConnect-GUI\openconnect-gui.exe' | |
| if not isUserAdmin(): | |
| print('running script as admin') | |
| runAsAdmin() | |
| print('script executed') | |
| quit() | |
| # start openconnect gui | |
| print('running openconnect executable') | |
| startfile(EXECUTABLE) | |
| sleep(0.05) | |
| # focus window | |
| print('finding window') | |
| window = FindWindow(None, 'OpenConnect VPN') | |
| assert window, 'Window not found' | |
| print(f'window found: id={window}') | |
| SetForegroundWindow(window) | |
| print('window focused') | |
| sleep(0.5) | |
| # click connect button | |
| print('clicking on connect button') | |
| click(915, 551) | |
| sleep(0.5) | |
| # write password | |
| print('writing password') | |
| write(PASSWORD) | |
| sleep(0.1) | |
| #click OK | |
| print('clicking on ok') | |
| click() # same pos as connect button | |
| sleep(1) | |
| #close window | |
| print('closing window') | |
| PostMessage(window, WM_CLOSE, 0, 0) | |
| sleep(0.1) | |
| print('window closed') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment