美文网首页
shell脚本实现ssh通过跳板机自动登录远程服务器

shell脚本实现ssh通过跳板机自动登录远程服务器

作者: Rohn | 来源:发表于2018-01-12 12:37 被阅读606次

本文主要讲解如何用shell通过跳板机直接登录目标服务器,免去在跳板机操作环节,其中涉及expect、spawn、ssh,没有安装请自行查阅

编写relay.exp文件

赋权限755,mac推荐放到/usr/local/bin/目录下
需要手工设置脚本中的USER和RELAYSVR的值

#!/usr/bin/expect -f
set timeout -1
set USER rohn  #账号名
set RELAYSVR "relay.rohn.com" #跳板机host
set sshhost [lindex $argv 0]
set sshuser [lindex $argv 1]
set sshpasswd [lindex $argv 2]
set sshcmd [lindex $argv 3]
spawn ssh -t $USER@$RELAYSVR
expect {
   "*password:" {
        stty -echo
        send_user -- "\nEnter Password:"
        expect_user -re "(.*)\n"
        send_user "\n"
        stty echo
        set password $expect_out(buffer)
        send -- "$password\r"
        exp_continue
    }
   "*PASSCODE*" {
        send "$password\r"
        exp_continue
    }
   "*$" {
   }
}
expect {
    "*sure you want to continue connecting*" {
        sleep .1
        exp_send "yes\r"
        exp_continue
    }
    "*password*" {
        sleep .1
        exp_send -- "$sshpasswd\r"
    }
    "*$" {
    }
}
expect  {
    "*]$ " {
    send "$sshcmd\r"
    }
}
interact
exit

编写调用文件

名字自定义,本文用rohn.ssh命名
vim rohn.ssh
chmod +x rohn.ssh
文件内容:
/usr/local/bin/relay.exp 目标服务器地址 登录账号 登录密码
cp rohn.ssh /usr/local/bin/.

在命令行执行 rohn.ssh 即可直接登录远程目标的服务器,如果想多开窗口,session共享,免多次重复输入密码登录,请看 https://www.jianshu.com/p/ce5e01375976

相关文章

  • 自动创建samba目录的shell脚本

    1 shell脚本实现ssh自动登录远程服务器 spawn 开启一个子进程expect 预期收到的字符send 发...

  • shell脚本实现ssh通过跳板机自动登录远程服务器

    本文主要讲解如何用shell通过跳板机直接登录目标服务器,免去在跳板机操作环节,其中涉及expect、spawn、...

  • shell脚本实现连接远程服务器(ECS)

    原文地址:shell脚本实现连接远程服务器(ECS) 说明 本篇文章主要使用expect脚本来实现自动连接远程服务...

  • SSH穿越跳板机登录远程服务器

    原文发表在我的个人博客 - SSH穿越跳板机登录远程服务器 公司出于安全考虑,登录业务服务器之前必须先登录到跳板机...

  • 自动登录linux server (python版本)

    该脚本可以实现快速自动登录远程服务器,缺点在于密码明文,不过可以通过其他方式将密码隐藏。

  • ssh远程登录服务器

    1.ssh远程登录 1.1. ssh远程登录服务器:当你第一次通过指令ssh root@192.168.2.2登录...

  • shell多进程

    现象 远程主机监控脚本运行速度感人,想要提速。通过分析脚本各语句,主要耗时在ssh操作上,有多条ssh自动登录并执...

  • 2018-05-03 跳板机

    通过跳板机进行远程连接服务器 作用 1、安全:控制访问服务器方式,控制访问者的权限,所有的ssh连接都是通过跳板机...

  • SSH服务

    SSH secure shell,protocol,22/tcp,安全的远程登录 具体的软件实现 OpenSSH:...

  • SHH部署脚本

    两篇文章简单记录一下这周使用过的几个脚本。首先是shell脚本实现ssh自动输入密码登录主机,并在主机部署脚本并执...

网友评论

      本文标题:shell脚本实现ssh通过跳板机自动登录远程服务器

      本文链接:https://www.haomeiwen.com/subject/ibffoxtx.html