Linux Expect自动化登录脚本

之前写了一个自动化登录的脚本,最近运维升级了堡垒机,导致这段时间无法登录,经过摸索排查,发现需要把每个send后的\n换成\r,然后又可以愉快的玩耍了。

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
#!/usr/bin/expect
set user hohode
set host jump.hohode.com
set password xxxxx
spawn ssh -i /Users/hohode/Documents/company/keys/online/hohode-jumpserver.pem $user@$host
expect "*Opt>*"


if { $argc == 1 } {
set seq [lindex $argv 0]
}

if { $seq == "nck" } {
send "hadoop019\r"
expect "jump@hadoop*"
send "sudo su -\r"
send "ssh 190.0.2.129\r"
send "cd /data/\r"
} elseif { $seq == "test" } {
send "190.0.2.73\r"
expect "jump@*"
send "sudo su -\r"
} elseif { $seq == "app001" || $seq == "app002" || $seq == "logserver001" || $seq == "logserver002" || $seq == "logserver003" } {
send "$seq \r"
expect "jump*"
send "sudo su -\r"
if { $seq == "app001" || $seq == "app002" } {
send "cd /data/work/pre_tracker/\r"
}

if { $seq == "logserver001" || $seq == "logserver002" || $seq == "logserver003" } {
send "cd /data/work/\r"
}

} elseif { $seq != "lll" } {

if { $seq == 1 || $seq == 2 || $seq == 6 } {
send "hadoop00$seq\r"
}
if { $seq == 19 } {
send "hadoop0$seq\r"
} else {
send "$seq\r"
}

expect "jump@*"
send "sudo su -\r"

if { $seq == 1 || $seq == 2 || $seq == 19 } {
expect "*root@hadoop*"
send "su - hdfs\r"
if { $seq == 1 } {
expect "*hdfs@hadoop*"
send "cd shell/new/\r"
} elseif { $seq == 2 } {
expect "*hdfs@hadoop*"
send "cd /data/work/shell/\r"
} elseif { $seq == 19 } {
expect "*hdfs@hadoop*"
send "cd shell/\r"
}
}
}

interact
expect eof

分享