Created
September 1, 2023 11:45
-
-
Save vadviktor/16fbf54d384b29194384bda447677ee8 to your computer and use it in GitHub Desktop.
Asking the OTP for an SSH connection form 1password Connect, interacting with the prompt and yielding the console back to the user at the end.
This file contains 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
import pexpect | |
def otp(): | |
import requests | |
vault_id = "xxxxxxxxx" | |
item_id = "xxxxxxxx" | |
url = f"http://localhost:18080/v1/vaults/{vault_id}/items/{item_id}" | |
headers = { | |
"Authorization": "Bearer xxxx", # noqa: E501 | |
"Content-Type": "application/json", | |
} | |
response = requests.request("GET", url, headers=headers) | |
if not response.ok: | |
print(response.status_code) | |
exit(1) | |
jresp = response.json() | |
if "fields" not in jresp.keys(): | |
print("No fields in response") | |
exit(1) | |
match = next((f for f in jresp["fields"] if f["type"] == "OTP"), None) | |
if match is None: | |
print("No OTP") | |
exit(1) | |
# print(match["totp"]) | |
return match["totp"] | |
def login_to_production(target="console"): | |
print(f"OTP: {otp()}") | |
child = pexpect.spawnu(f"servicecommand {target} production") | |
child.expect("Verification") | |
child.sendline(otp()) | |
child.expect("Verification") | |
child.sendline(otp()) | |
child.interact() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment