日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

expect實現(xiàn)Linux自動登陸遠程機器腳本實例_Linux

更新時間: 2023-02-05 編程語言

expect?是由Don Libes基于Tcl(Tool Command Language )語言開發(fā)的,主要應(yīng)用于自動化交互式操作的場景,借助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
# 修復(fù)等不需要要配置跳板機的時候執(zhí)行命令,在配置跳板機位置默認填 no 即可
# @Author pemakoa@gmail.com
# Bash version >= 4.0 使用關(guān)聯(lián)數(shù)組

# Usage: host user passwd port jump_host command 
# 四種情況如下:
# 1. 直接登錄目標(biāo)機器 如 A 
# 2. 需要中間機器登陸到目標(biāo)機器 如 C, 其中 B 為中間機器,會先登錄 B在從 B登陸到 C然后執(zhí)行 command
# 3. 直接登錄目標(biāo)機器并執(zhí)行相應(yīng)的命令 如 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

# 登陸 如果配置跳板機,先登陸跳板機在登陸到目標(biāo)機器
_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
欄目分類
最近更新