網(wǎng)站首頁 編程語言 正文
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
相關(guān)推薦
- 2022-11-15 python內(nèi)置模塊OS?實現(xiàn)SHELL端文件處理器_python
- 2022-07-14 如何批量刪除Docker中已經(jīng)停止的容器的幾種方法_docker
- 2022-04-15 C語言的程序環(huán)境與預(yù)處理你真的了解嗎_C 語言
- 2022-03-16 ASP.NET?Core在Linux下為dotnet創(chuàng)建守護進程_基礎(chǔ)應(yīng)用
- 2022-05-12 Kotlin generateSequence 迭代器 從1開始產(chǎn)生1000個素數(shù) take 得介紹
- 2022-04-20 C語言進階輸入輸出重定向與fopen函數(shù)使用示例詳解_C 語言
- 2022-01-15 linux 安裝插件報錯:Loaded plugins: fastestmirror
- 2022-11-07 Android?Framework如何實現(xiàn)Binder_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支