Commit 3cfa8da6 authored by Brandon Smith's avatar Brandon Smith

Refactor expecting to allow other commands to work

parent df572d31
...@@ -32,31 +32,52 @@ except: ...@@ -32,31 +32,52 @@ except:
ssh = pexpect.spawn(command or "ssh", sys.argv[1:]) ssh = pexpect.spawn(command or "ssh", sys.argv[1:])
winch_handler(None, None) winch_handler(None, None)
index = -1 def passthrough():
pattern_list = ssh.compile_pattern_list([ print()
"Enter additional factors:.*", sys.stdout.write(ssh.match.group())
"----- BEGIN U2F CHALLENGE -----\r?\n([^\r\n]*)\r?\n(.*)\r?\n----- END U2F CHALLENGE -----", try:
"Welcome.*", ssh.interact()
pexpect.EOF except UnboundLocalError:
]) # Work around bug in pexpect 3.1
pass
sys.exit(0)
index = ssh.expect(["Authenticated with partial success.",
"[^ \r\n]+",
pexpect.EOF])
if index == 0:
print(ssh.match.group())
elif index == 1:
passthrough()
elif index == 2:
sys.exit(0)
while True: while True:
index = ssh.expect_list(pattern_list) index = ssh.expect(["Enter additional factors: ",
"----- BEGIN U2F CHALLENGE -----\r\n",
"[^ \r\n]+",
pexpect.EOF])
if index == 0: if index == 0:
try: try:
pin = getpass.getpass(ssh.match.group()) pin = getpass.getpass(ssh.match.group())
except EOFError: except EOFError:
pin = "" pin = ""
ssh.sendline(pin.strip()) ssh.sendline(pin.strip())
elif index == 1: elif index == 1:
p = subprocess.Popen(["u2f-host", "-aauthenticate", u2f_origin = ssh.readline().strip()
"-o", ssh.match.group(1)], u2f_challenge = ssh.readline().strip()
ssh.expect("----- END U2F CHALLENGE -----")
p = subprocess.Popen(["u2f-host", "-aauthenticate", "-o", u2f_origin],
stdin=subprocess.PIPE, stdout=subprocess.PIPE) stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = p.communicate(ssh.match.group(2)) out, err = p.communicate(u2f_challenge)
p.wait() p.wait()
ssh.sendline(out.strip()) ssh.sendline(out.strip())
else:
break elif index == 2:
if index == 3: passthrough()
sys.exit(0)
sys.stdout.write(ssh.match.group()) elif index == 3:
ssh.interact() sys.exit(0)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment