- #!/bin/bash
- #Description: auto login the remote server
- #Author:majinxu
- #Version:1.0
- #CreateTime:2018-3-012 18:34:41
- user="majinxu"
- passwd="!Ieric1234"
- host_list=( "st1.qa.bj2.yongche.com"
- "st2.qa.bj2.yongche.com"
- "st3.qa.bj2.yongche.com"
- )
- #seletc the host
- select host in ${host_list[@]};
- do
- break
- done
- #execute the ssh action
- expect -c "
- spawn ssh $user@$host
- expect {
- \"yes\/no\" { send \"yes\n\"; exp_continue}
- \"password:\" { send \"$passwd\n\" }
- }
- interact
- "
解释:
expect -c : -c 参数跟字符串
spawn ssh $user@$host 连接服务器
- expect {
- \"yes\/no\" { send \"yes\n\"; exp_continue}
- \"password:\" { send \"$passwd\n\" }
- }
输入并接收用户名密码
是
- "yes/no" { send "yes\n"; exp_continue}
- "password:" { send "$passwd\n" }
} 转义后的结果
interact
执行完成后保持交互状态, 把控制权交给控制台
来源: http://www.bubuko.com/infodetail-2526271.html