自动登录跳板机->开发机

#!/usr/bin/expect

# relay用户名

set USER [lindex $argv 0]

# relay PIN码

set PIN [lindex $argv 1]

# [可选]开发机地址, 如:user@host

set HOST [lindex $argv 2]

puts $HOST

# [可选]开发机密码

set PASSWORD [lindex $argv 3]

# 登录relay

spawn ssh $USER@relay01.duxiaoman-int.com

# 打开调试模式

#exp_internal 1

# relay PIN码 + Token(Token需手动输入)
interact
expect {

    -re "password:*" {

        send_user "PIN:****** + Token:"

        expect_user {

            -timeout -1

            -re "(.*)\n"

        }

        set TOKEN $expect_out(buffer)

        send "$PIN$TOKEN\r"

        exp_continue
    }
    # 自动登录开发机
    -re "" {
        if { "$HOST" != "" } {
            send "$HOST\r"

            # [可选]自动开发机输入密码

            if { "$PASSWORD" != "" } {

                expect -re "password:" { send "$PASSWORD\r" }

            }

        }

    }

}

# 关闭调试模式

#exp_internal 0

interact

exit
  1. 首先安装  expect
  2. 将上面的脚本copy至你的电脑上
  3. expect  .relay.expect username ping hostname
posted @ 2020-07-15 13:02  花泪哲  阅读(484)  评论(0编辑  收藏  举报