expect实现Linux自动登陆远程机器脚本实例
expect 是由Don Libes基于Tcl(Tool Command Language )语言开发的,主要应用于自动化交互式操作的场景,借助Expect处理交互的命令,可以将交互过程如:ssh登录,ftp登录等交互过程,写到Shell脚本里以实现一些自动化操作。
在 Linux 下进行远程登陆的时候,总是要进行 ssh 输入账号密码,相对比较繁琐。
而有时候为了保护线上重要的机器不能随便登录,通常使用从本地登陆到公司的中间机器(跳板机)然后才能登陆到线上的机器。如果 A -> B -> C 三台机器,如果想从 A 直接到 C 只能通过 B 进行登录。下面的脚本就是解决这种有多个依赖的关系。
注意事项:
1. 使用实时 bash version >= 4.0,因为配置中需要使用关联数据
2. 如果需要全局使用直接修改 autologin 的名字,移动到 PATH 路径下即可 eg: mv autologin /usrl/local/bin/to(改为自己想要使用的名字)
脚本代码:
#!/usr/local/bin/bash
# @Version 0.3.1
# @filename to
# 修复等不需要要配置跳板机的时候执行命令,在配置跳板机位置默认填 no 即可
# @Author pemakoa@gmail.com
# Bash version >= 4.0 使用关联数组
# Usage: host user passwd port jump_host command
# 四种情况如下:
# 1. 直接登录目标机器 如 A
# 2. 需要中间机器登陆到目标机器 如 C, 其中 B 为中间机器,会先登录 B在从 B登陆到 C然后执行 command
# 3. 直接登录目标机器并执行相应的命令 如 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
上一篇:apache-zookeeper-3.7.1 安装部署教程
栏 目:其它服务器
下一篇:解决ubuntu安装软件时,status-code=409报错的问题
本文标题:expect实现Linux自动登陆远程机器脚本实例
本文地址:https://zz.feitang.co/server/30713.html
您可能感兴趣的文章
- 12-20Kubernetes中使用临时容器进行故障排查的方法
- 12-20Nginx设置HTTPS的方法步骤
- 12-20二进制方式安装 Kubernetes1.18.3版本实现脚本
- 12-20Nginx工作模式及代理配置的使用细节
- 12-20ZooKeeper分布式协调服务设计核心概念及安装配置
- 12-20Kubernetes部署可视化地图的十个步骤
- 12-20关于docker清理Overlay2占用磁盘空间的问题(亲测有效)
- 12-20Docker compose配置文件写法及命令使用示例
- 12-20openwrt安装docker并启动的操作方法
- 12-20云原生Kubernetes初始化容器Init使用教程


阅读排行
推荐教程
- 12-07一文教你怎么选择Tomcat对应的JDK版本
- 12-07新版Eclipse集成Tomcat时找不到server选项的解决方法
- 12-06IIS7 应用程序池自动回收关闭的解决方案
- 12-05Windows Server 2019安装VMware
- 12-05Windows服务器默认IE浏览器无法下载文件的解决方法
- 12-05Docker安装Jenkins全过程
- 12-19Zabbix SAML SSO 登录绕过漏洞的操作流程
- 12-15Docker-Compose搭建Spark集群的实现方法
- 12-14Docker Desktop无法正常启动解决(failed to start...)
- 12-14k8s 与docker空间使用分析与清理方法





