-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword.py
49 lines (42 loc) · 1.73 KB
/
password.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from termios import error, tcgetattr, ECHO, ICANON, ISIG, ECHOCTL, ECHOKE
import re
import subprocess
import sys
def main(args):
if not sys.stdin.isatty():
data = sys.stdin.read().strip().split('\n')[-1]
return data
else:
print("\a")
print("Tried to read a tty")
def handle_result(args, data, target_window_id, boss):
w = boss.window_id_map.get(target_window_id)
if w is not None:
fd = w.child.child_fd
try:
c_lflag = tcgetattr(fd)[3]
except error as err:
errmsg = 'getecho() may not be called on this platform'
if err.args[0] == errno.EINVAL:
raise IOError(err.args[0], '%s: %s.' % (err.args[1], errmsg))
raise
def send_password():
password = subprocess.run(args[1:], capture_output=True, text=True, check=True).stdout
w.paste(password + '\r')
def is_set(flag):
return bool(c_lflag & flag)
# print(f'c_lflag={c_lflag} icanon={c_lflag & ICANON} echo={c_lflag & ECHO} echoctl={c_lflag & ECHOCTL} echoke={c_lflag & ECHOKE} isig={c_lflag & ISIG} data={repr(data)}')
if is_set(ISIG) and is_set(ICANON) and not is_set(ECHO):
# ruby -rio/console -e 'STDIN.noecho { |io| puts io.gets.inspect }'
# sudo <cmd>
send_password()
elif not is_set(ISIG) and not is_set(ICANON) and not is_set(ECHO) and re.match(r"Password.*:$", data):
# ssh -t <host> <cmd> # same socket mode for kinit and cat :(
send_password()
else:
print("\a")
print("Ooops. Are you at a password prompt?")
handle_result.type_of_input = 'text'
if __name__ == '__main__':
import sys
main(sys.argv)