Skip to content

Instantly share code, notes, and snippets.

@konstantinj
Created May 5, 2020 19:05
Show Gist options
  • Save konstantinj/b8f4f550cb4f2e6fd13d3b61863f6421 to your computer and use it in GitHub Desktop.
Save konstantinj/b8f4f550cb4f2e6fd13d3b61863f6421 to your computer and use it in GitHub Desktop.
A python script to automate otp in openvpn sessions
#!/usr/bin/env python3
import sys
# https://github.com/tadeck/onetimepass
import onetimepass as otp
# https://github.com/pexpect/pexpect
import pexpect
secret = open('.home-office.secret', 'r').read().strip()
code = str(otp.get_totp(secret)).zfill(6)
conn = pexpect.spawn('sudo openvpn --config home-office.ovpn --auth-retry interact')
conn.logfile = sys.stdout.buffer
conn.expect('CHALLENGE')
conn.delaybeforesend = 1
conn.sendline(code)
conn.expect('Initialization Sequence Completed')
print("Escape character is '^]'.\n")
sys.stdout.buffer.write(conn.after)
sys.stdout.buffer.flush()
conn.interact()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment