from selenium import webdriver from selenium.common.exceptions import TimeoutException from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By import subprocess import os import re host = "" user = "" timeout = 10 genPass = "cat /tmp/chris_token.fifo | openssl enc -aes256 -a -d -pass fd:0 -pbkdf2 -in " # see https://github.com/krissrex/google-authenticator-exporter to export your GA token genOTP = "" # probably specific for WSL2, but maybe useful in other containers, make chrome happier dbusPidS = "/run/dbus/pid" startS = False if os.path.exists(dbusPidS): pid = open(dbusPidS).read().rstrip() if not os.path.exists('/proc/{}'.format(pid)): startS = True else: startS = True myID = subprocess.run(['id', '-u'], capture_output=True).stdout.decode('utf-8').rstrip() dbusU = "/run/user/{}/bus".format(myID) startU = False ps = subprocess.Popen("ps -eaf | grep "+dbusU, shell=True, stdout=subprocess.PIPE) output = ps.stdout.read().decode('utf-8') ps.stdout.close() ps.wait() if re.search('dbus-daemon', output) is None: startU = True d = "/run/user/"+myID os.environ['XDG_RUNTIME_DIR'] = d print("export XDG_RUNTIME_DIR="+d) a = "unix:path={}/bus".format(d) os.environ['DBUS_SESSION_BUS_ADDRESS'] = a print("export DBUS_SESSION_BUS_ADDRESS="+a) if startS: print("Starting system DBUS") if not os.path.exists('/run/dbus'): subprocess.run(["sudo", "mkdir", "/run/dbus"]) subprocess.run(["sudo", "chown", "dbus", "/run/dbus"]) subprocess.run(["sudo", "runuser", "-u", "dbus", "--", "dbus-daemon", "--config-file=/usr/share/dbus-1/system.conf"]) if startU: print("Starting user session DBUS") if not os.path.exists(d): subprocess.run(["sudo", "mkdir", "-p", d]) n = subprocess.run(['id', '-un'], capture_output=True).stdout.decode('utf-8').rstrip() g = subprocess.run(['id', '-gn'], capture_output=True).stdout.decode('utf-8').rstrip() subprocess.run(["sudo", "chown", "{}:{}".format(n,g), d]) subprocess.Popen(["dbus-daemon", "--session", "--address="+a, "--nofork", "--nopidfile", "--syslog-only"], close_fds=True) driver = webdriver.Chrome("/usr/bin/chromedriver") wait = WebDriverWait(driver, timeout) # The field names need to be discovered by going through the connection process # manually and examine the pages content through the chrome debugger driver.get("https://"+host) try: present = EC.presence_of_element_located((By.XPATH, '//*[@id="sn-preauth-proceed_2"]')) element = wait.until(present) except TimeoutException: print("Timed out waiting for preauth") driver.quit() sys.exit(1) element.click() try: present = EC.presence_of_element_located((By.XPATH, '//*[@id="username"]')) element = wait.until(present) except TimeoutException: print("Timed out waiting for username") driver.quit() sys.exit(1) element.send_keys(user) element = driver.find_element(By.XPATH, '//*[@id="password"]') password = subprocess.run(genPass, capture_output=True, shell=True).stdout.decode('utf-8').rstrip() element.send_keys(password) element = driver.find_element(By.XPATH, '//*[@id="login-button"]') element.click() try: present = EC.presence_of_element_located((By.XPATH, '//*[@id="otp"]')) element = wait.until(present) except TimeoutException: print("Timed out waiting for otp") driver.quit() sys.exit(1) otp = subprocess.run(genOTP, capture_output=True, shell=True).stdout.decode('utf-8').rstrip() element.send_keys(otp) element = driver.find_element(By.XPATH, '/html/body/div[1]/div/div/div[3]/div[2]/div/div/form/div[2]/div/button') element.click() dsid = wait.until(lambda driver: driver.get_cookie("DSID")) driver.quit() print(dsid["value"]) subprocess.run(["sudo", "openconnect", "-C", dsid["value"], "--protocol=pulse", "-u", user, host])