Linux expect自动登录服务器 查看日志

Linux 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
#!/usr/bin/expect
set param0 [lindex $argv 0]
set log_name "api"
# 判断是否选择日志
if {$argc < 1} {
send_user "请选择: 1.api 2.timer\n"
exit
}
# 选择日志
if {${param0} == 1} {
set param0 "api"
send_user "api日志\n"
} elseif {${param0} == 2} {
set param0 "timer"
set log_name "timer"
send_user "timer日志\n"
} else {
send_user "错误的选择\n请选择: 1.api 2.timer\n"
exit
}
# 用户名@服务器地址
spawn ssh admin@192.168.0.1
expect "*password:"
send "你的密码"
# 进入指定目录
expect "~" {send "cd web/apache-tomcat-${param0}/logs\n"}
# 获取最新日志文件名称
expect "logs" {send "lgname=`ls -t yangka.${log_name}.log.*`\n"}
expect "logs" {send "lgname=`echo \$lgname | awk '{print \$1}'`\n"}
# 查看日志
expect "logs" {send "tail -fn 200 \$lgname\n"}
interact