expect實現(xiàn)Linux自動登陸遠程機器腳本實例
expect 是由Don Libes基于Tcl(Tool Command Language )語言開發(fā)的,主要應用于自動化交互式操作的場景,借助Expect處理交互的命令,可以將交互過程如:ssh登錄,ftp登錄等交互過程,寫到Shell腳本里以實現(xiàn)一些自動化操作。
在 Linux 下進行遠程登陸的時候,總是要進行 ssh 輸入賬號密碼,相對比較繁瑣。
而有時候為了保護線上重要的機器不能隨便登錄,通常使用從本地登陸到公司的中間機器(跳板機)然后才能登陸到線上的機器。如果 A -> B -> C 三臺機器,如果想從 A 直接到 C 只能通過 B 進行登錄。下面的腳本就是解決這種有多個依賴的關(guān)系。
注意事項:
1. 使用實時 bash version >= 4.0,因為配置中需要使用關(guān)聯(lián)數(shù)據(jù)
2. 如果需要全局使用直接修改 autologin 的名字,移動到 PATH 路徑下即可 eg: mv autologin /usrl/local/bin/to(改為自己想要使用的名字)
腳本代碼:
#!/usr/local/bin/bash
# @Version 0.3.1
# @filename to
# 修復等不需要要配置跳板機的時候執(zhí)行命令,在配置跳板機位置默認填 no 即可
# @Author pemakoa@gmail.com
# Bash version >= 4.0 使用關(guān)聯(lián)數(shù)組
# Usage: host user passwd port jump_host command
# 四種情況如下:
# 1. 直接登錄目標機器 如 A
# 2. 需要中間機器登陸到目標機器 如 C, 其中 B 為中間機器,會先登錄 B在從 B登陸到 C然后執(zhí)行 command
# 3. 直接登錄目標機器并執(zhí)行相應的命令 如 D
declare -A _server_config
_server_config['A']="a_host a_user a_passwd a_port"
_server_config['B']="b_host b_user b_passwd b_port"
_server_config['C']="c_host c_user c_passwd c_port B '(command eg) ls .'"
_server_config['D']="d_host d_user d_passwd d_port no 'cd /home && ll'"
_config_keys=(${!_server_config[@]})
_length=${#_server_config[@]}
_login_server=$1
_config_status=false
# 是否輸入登陸機器
if [ "$_login_server" == "" ];then
echo -e "\033[40m\033[31m Please input login server, you can choose one follows list \033[0m"
for i in "${_config_keys[@]}";do
echo -e "\033[41;37m $i \033[0m "
done
exit
fi
# 檢查登陸的機器是否配置
for i in "${_config_keys[@]}";do
if [ "$_login_server" == "$i" ];then
_config_status=true
fi
done
if [ "${_config_status}" == "false" ];then
echo -ne "\033[40m\033[31m
Not config server info ...
Please config in _server_config like
Host User Passwd Port Jump Command\033[0m"
exit
fi
# 登陸 如果配置跳板機,先登陸跳板機在登陸到目標機器
_host=$(echo ${_server_config["${_login_server}"]} | awk '{print $1}')
_user=$(echo ${_server_config["${_login_server}"]} | awk '{print $2}')
_passwd=$(echo ${_server_config["${_login_server}"]} | awk '{print $3}')
_port=$(echo ${_server_config["${_login_server}"]} | awk '{print $4}')
_jump=$(echo ${_server_config["${_login_server}"]} | awk '{print $5}')
_command=$(echo ${_server_config["${_login_server}"]} | awk -F"'" '{print $2}')
if [ "${_command}" != "" ]; then
_command="expect \"*]*\"
send \"${_command}\r\""
fi
if [ "${_jump}" != "" ] && [ "${_jump}" != "no" ]; then
_jump_host=$(echo ${_server_config["${_jump}"]} | awk '{print $1}')
_jump_user=$(echo ${_server_config["${_jump}"]} | awk '{print $2}')
_jump_passwd=$(echo ${_server_config["${_jump}"]} | awk '{print $3}')
_jump_port=$(echo ${_server_config["${_jump}"]} | awk '{print $4}')
expect -c "
set timeout 30
spawn ssh -p${_jump_port} ${_jump_user}@${_jump_host}
expect {
\"yes/no\" { send \"yes\r\"; exp_continue }
\"assword\" { send \"${_jump_passwd}\r\" }
}
expect \"*]*\"
send \"ssh -p${_port} ${_user}@${_host}\r\"
expect \"assword:\"
send \"${_passwd}\r\"
${_command}
interact"
else
expect -c "
set timeout 30
spawn ssh -p${_port} ${_user}@${_host}
expect {
\"yes/no\" {send \"yes\r\"; exp_continue }
\"*assword:\" { send \"$_passwd\r\" }
}
${_command}
interact
"
fi到此這篇關(guān)于expect實現(xiàn)Linux自動登陸遠程機器腳本實例的文章就介紹到這了,更多相關(guān)Linux自動登陸腳本內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
CentOS 6.5平臺實現(xiàn)快速部署FTP的方法
這篇文章主要介紹了CentOS 6.5平臺實現(xiàn)快速部署FTP的方法,結(jié)合實例形式分析了CentOS6.5平臺配置與部署FTP的具體步驟、相關(guān)操作命令與注意事項,需要的朋友可以參考下2018-04-04
Linux/CentOS系統(tǒng)同步網(wǎng)絡時間的2種方法詳解
我們知道Windows系統(tǒng)下可以同步網(wǎng)絡時間,那么Linux系統(tǒng)下有沒有相同的功能呢,今天為大家介紹2Linux系統(tǒng)下同步網(wǎng)絡時間的方法2018-10-10
CentOS7中使用shell腳本安裝python3.8環(huán)境(推薦)
這篇文章主要介紹了CentOS7中使用shell腳本安裝python3.8環(huán)境,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
詳解Linux中PostgreSQL和PostGIS的安裝和使用
這篇文章主要介紹了詳解Linux中PostgreSQL和PostGIS的安裝和使用,并把需要注意點做了分析和解釋,需要的朋友學習下。2018-02-02

