mac使用expect自动登陆跳版机

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

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

前提:安装expectssh

 1 # expect交互的脚本  2 #!/usr/bin/expect  3 
 4 # 链接跳板机  5 spawn ssh -p2222 XXX@jump.XXX.jms  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
相关文章
相关标签/搜索