当前位置: 七九推 > 网络运营>服务器>Linux > expect实现Linux自动登陆远程机器脚本实例

expect实现Linux自动登陆远程机器脚本实例

2022年12月30日 Linux 我要评论
expect是由don libes基于tcl(tool command language )语言开发的,主要应用于自动化交互式操作的场景,借助expect处理交互的命令,可以将交互过程如:ssh登录,

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

到此这篇关于expect实现linux自动登陆远程机器脚本实例的文章就介绍到这了,更多相关linux自动登陆脚本内容请搜索七九推以前的文章或继续浏览下面的相关文章希望大家以后多多支持七九推!

(0)
打赏 微信扫一扫 微信扫一扫

相关文章:

  • Maui Blazor 使用摄像头实现代码

    Maui Blazor 使用摄像头实现代码

    由于maui blazor中界面是由webview渲染,所以再使用android的摄像头时无法去获取,因为原生的摄像头需要绑定界面组件所以我找到了其他的实现方式... [阅读全文]
  • 独立使用umi的核心插件模块示例详解

    独立使用umi的核心插件模块示例详解

    引言今天我们做一个有趣的尝试,将 umi 的核心插件模块独立出来作为另一个框架的基础架构,这里我们将它称为 konos。 介于 umi 自身的源码的独立拆分,要... [阅读全文]
  • KubeSphere接入外部Elasticsearch实战示例

    引言在安装完成时候可以启用日志组件,这样会安装 es 组件并可以收集所有部署组件的日志,也可以收集审计日志,然后可以很方便的在 kubesphere 平台上进行日志查询。但是在实际…

    2022年12月14日 服务器
  • linux 系统进程管理工具systemd详解(systemctl命令、创建自己的systemd服务)

    linux 系统进程管理工具systemd详解(systemctl命令、创建自己的systemd服务)

    linux systemd什么是 systemdlinux 系统在启动过程中,内核完成初始化以后,由内核第一个启动的程序便是 init 程序,路径为 /sbin... [阅读全文]
  • linux服务器CPU飙高排查分析

    linux服务器CPU飙高排查分析

    前言系统cpu飙高,尤其对于后端人员来说,其实应该学会排查,这样也算是综合能力的体现;那么当出现了cpu严重飙高的时候怎么排查呢?一、第一步 top直接在问题服... [阅读全文]
  • Linux文件查找命令总结(下篇)

    Linux文件查找命令总结(下篇)

    前言关于linux文件查找命令总结我们分别介绍了which命令、whereis命令、locate命令、find命令这四个命令,本篇向大家介绍的是find命令。f... [阅读全文]

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2023  七九推 保留所有权利. 粤ICP备17035492号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com