-
Notifications
You must be signed in to change notification settings - Fork 0
/
kp.expect
67 lines (49 loc) · 1.67 KB
/
kp.expect
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/expect -f
log_user 0
stty -echo
set timeout 20
set cmd [lindex $argv 0]
set query [lindex $argv end]
set maxpwtries 3
set backspaces ""
eval spawn -noecho keepassxc-cli $cmd
expect {
-re "Enter password to unlock.*:" {
set loop True
set backspaces [string repeat "\b \b" 44]
while {$maxpwtries != 0} {
send_user "Please enter your database password:\t"
expect_user -re "(.*)\n"
set dbpw $expect_out(1,string)
send "$dbpw\r"
expect {
"Invalid credentials were provided" {
set maxpwtries [incr maxpwtries -1]
if {$maxpwtries == 0} {
send_user "${backspaces}Reached the max number of password attempts. Check your config.\n"
set backspaces [string repeat "\b \b" 65]
exit 1
} else {
send_user "${backspaces}Incorrect Credentials. ${maxpwtries} attempts remaining. "
set backspaces [string repeat "\b \b" 88]
expect ">"
send "$cmd\r"
expect -re "Enter password to unlock.*:"
}
}
">" {
break
}
}
}
}
">"
}
send "$query\r"
expect -re "\r\n(.*)\r\n"
set pwout $expect_out(1,string)
send "exit\r"
expect eof
set hideoutput [string repeat "\b \b" [string length $pwout]]
send_user "${backspaces}$pwout${hideoutput}"
exit 0