1. 程式人生 > >自動登錄服務器shell腳本

自動登錄服務器shell腳本

shell

#!/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
"

解釋:

  1. expect -c : -c參數跟字符串
  2. spawn ssh $user@$host 連接服務器
  3. expect {
    \"yes\/no\" { send \"yes\n\"; exp_continue}
    \"password:\" { send \"$passwd\n\" }
    }
    輸入並接收用戶名密碼

    "yes/no" { send "yes\n"; exp_continue}
    "password:" { send "$passwd\n" }
    } 轉義後的結果
  4. interact
    執行完成後保持交互狀態,把控制權交給控制臺

自動登錄服務器shell腳本