-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomate.sh
executable file
·63 lines (48 loc) · 1.24 KB
/
automate.sh
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
#!/bin/bash
curpath=`dirname $0`
echo "curpath $curpath"
cd $curpath
if [ $# -ne 1 ];then
echo "Usage:$0 host"
exit 1
fi
host=$1
config_yaml=session.yaml
#generate login.exp
expfile="login_$host.exp"
function create_expfile()
{
echo "#!/usr/bin/expect" > $expfile
}
function append_expfile()
{
echo $1 >> $expfile
}
#create login.exp
create_expfile
#get automate lenth
len=`cat $config_yaml | shyaml get-length $host.automate`
[[ $len -gt 0 ]] && last_index=`expr $len - 1` || last_index=0
echo "len:$len,last_index:$last_index"
#generate first jump
sshlogin=`cat $config_yaml|shyaml get-value $host.sshlogin`
append_expfile "spawn $sshlogin"
#loop automate
for ((i = 0; i < $len; i++))
do
echo "i:$i"
expect=`cat $config_yaml | shyaml get-value $host.automate.$i.expect`
send=`cat $config_yaml| shyaml get-value $host.automate.$i.send`
#support google MFA
if [ "$expect" == "OTP" ];then
send=`python3 ~/go/src/github.com/grahammitchell/google-authenticator/google-authenticator.py|awk '{print $2}'`
fi
#append to expfile
if [ $i -eq $last_index ];then
append_expfile "expect \"$expect\" {send \"$send\\n\";interact}"
else
append_expfile "expect \"$expect\" {send \"$send\\n\"}"
fi
done
chmod +x $expfile
./$expfile