Skip to content

Instantly share code, notes, and snippets.

@vadviktor
Created September 1, 2023 11:45
Show Gist options
  • Save vadviktor/16fbf54d384b29194384bda447677ee8 to your computer and use it in GitHub Desktop.
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.
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