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

學(xué)無(wú)先后,達(dá)者為師

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

expect實(shí)現(xiàn)Linux自動(dòng)登陸遠(yuǎn)程機(jī)器腳本實(shí)例_Linux

更新時(shí)間: 2023-02-05 編程語(yǔ)言

expect?是由Don Libes基于Tcl(Tool Command Language )語(yǔ)言開(kāi)發(fā)的,主要應(yīng)用于自動(dòng)化交互式操作的場(chǎng)景,借助Expect處理交互的命令,可以將交互過(guò)程如:ssh登錄,ftp登錄等交互過(guò)程,寫(xiě)到Shell腳本里以實(shí)現(xiàn)一些自動(dòng)化操作。

在 Linux 下進(jìn)行遠(yuǎn)程登陸的時(shí)候,總是要進(jìn)行 ssh 輸入賬號(hào)密碼,相對(duì)比較繁瑣。

而有時(shí)候?yàn)榱吮Wo(hù)線上重要的機(jī)器不能隨便登錄,通常使用從本地登陸到公司的中間機(jī)器(跳板機(jī))然后才能登陸到線上的機(jī)器。如果 A -> B -> C 三臺(tái)機(jī)器,如果想從 A 直接到 C 只能通過(guò) B 進(jìn)行登錄。下面的腳本就是解決這種有多個(gè)依賴(lài)的關(guān)系。

注意事項(xiàng):

1. 使用實(shí)時(shí) bash version >= 4.0,因?yàn)榕渲弥行枰褂藐P(guān)聯(lián)數(shù)據(jù)

2. 如果需要全局使用直接修改 autologin 的名字,移動(dòng)到 PATH 路徑下即可 eg: mv autologin /usrl/local/bin/to(改為自己想要使用的名字)

腳本代碼:

#!/usr/local/bin/bash
# @Version 0.3.1
# @filename to
# 修復(fù)等不需要要配置跳板機(jī)的時(shí)候執(zhí)行命令,在配置跳板機(jī)位置默認(rèn)填 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)機(jī)器 如 A 
# 2. 需要中間機(jī)器登陸到目標(biāo)機(jī)器 如 C, 其中 B 為中間機(jī)器,會(huì)先登錄 B在從 B登陸到 C然后執(zhí)行 command
# 3. 直接登錄目標(biāo)機(jī)器并執(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

# 是否輸入登陸機(jī)器
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

# 檢查登陸的機(jī)器是否配置
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

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