自動登錄服務器shell腳本
阿新 • • 發佈:2018-03-13
shell#!/bin/bash
select host in ${host_list[@]};
do
break
done
#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
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
執行完成後保持交互狀態,把控制權交給控制臺
自動登錄服務器shell腳本