summaryrefslogtreecommitdiff
path: root/openvpn_otp.py
diff options
context:
space:
mode:
authorOleg Pykhalov <go.wigust@gmail.com>2022-08-27 13:50:26 +0300
committerOleg Pykhalov <go.wigust@gmail.com>2022-09-01 22:33:01 +0300
commit61dff0fc1f3f0fc1b633c54744fe63d2633a79a2 (patch)
tree5396e35f4575a1ce249439d8018d45460dcbc0f2 /openvpn_otp.py
downloadopenvpn-mintotp-61dff0fc1f3f0fc1b633c54744fe63d2633a79a2.tar.gz
Initial commit.v0.0.1
Diffstat (limited to 'openvpn_otp.py')
-rwxr-xr-xopenvpn_otp.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/openvpn_otp.py b/openvpn_otp.py
new file mode 100755
index 0000000..321e69e
--- /dev/null
+++ b/openvpn_otp.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python3
+
+"""OpenVPN wrapper with OTP authentication."""
+
+import os
+import sys
+from pathlib import Path
+
+import mintotp
+import pexpect
+
+
+def main():
+ """Entrypoint."""
+ child = pexpect.spawn(f"openvpn --config {os.environ['OPENVPN_CONFIG_FILE']}")
+ child.logfile = sys.stdout.buffer
+ child.expect("CHALLENGE: Verification code:")
+ child.sendline(
+ mintotp.totp(Path(os.environ["OPENVPN_OTP_SECRET_FILE"]).read_text())
+ )
+ # macos - python script: pexpect hangs on child.wait()? - Stack Overflow
+ # https://stackoverflow.com/questions/58751357/python-script-pexpect-hangs-on-child-wait
+ while True:
+ try:
+ child.read_nonblocking()
+ except Exception:
+ break
+ if child.isalive():
+ child.wait()
+
+
+if __name__ == "__main__":
+ main()