mac使用expect自动登录跳版机

痛点:MAC登录跳版机,每次都要输入用户名和密码选择服务器后,比较繁琐。

解决:发现expect很好用,share一下,下面是工作中真实使用的脚本。

前提:安装expect

 1 # expect交互的脚本
 2 #!/usr/bin/expect
 3 
 4 # 连接跳板机
 5 spawn ssh -p2222 [email protected]
 6 
 7 # 如果返回的内容包含*yes/no,发送yes并且换行
 8 expect {
 9     "*yes/no" {send "yes\r"; exp_continue}
10     "*password:" {send "zxcv1234\r"}
11 }
12 # 选择服务器
13 expect {
14     "*Opt>" {send "***-test001\r"}
15 }
16 # 服务器切换用户
17 expect {
18     "*test-user@***-test001*" {send "sudo su - exuser\r"}
19 }
20 # 连接接redis
21 expect {
22     "*exuser@***-test001*" {send "redis-cli -h redis的IP地址\r"}
23 }
24 # 输入鉴权
25 expect {
26     "redis的IP地址:6379" {send "auth 123456\r"}
27 }
28 # 选择redis的1库
29 expect {
30     "redis的IP地址:6379" {send "select 1\r"}
31 }
32 expect {
33     "redis的IP地址:6379*" {send "keys v*\r"}
34 }
35 # 保持在远端  
36 interact

猜你喜欢

转载自www.cnblogs.com/t-gonna/p/11547289.html