双层跳板机登陆服务器-shell脚本实现自动登陆

#!/usr/bin/expect

### 两层跳板机版本 ###

set timeout 15
set jumpusr [lindex $argv 0]
set jumphost [lindex $argv 1]
set jumppassword [lindex $argv 2]
set usr [lindex $argv 3]
set host [lindex $argv 4]
set port [lindex $argv 5]
set password [lindex $argv 6]
set server [lindex $argv 7]

# ssh 跳板机 输入账户名 密码
catch {spawn ssh $jumpusr@$jumphost}
expect "*assword:*" {send "$jumppassword\r"}

# 选择跳板机
expect "Select server:*" {send "$server\r"}

# ssh 目标机器 输入账户名 密码
expect "*$jumphost*" { send "ssh -p $port $usr@$host\r" }
expect "*assword:*" {send "$password\r"}

# 保持交互状态,并把控制权交给控制台
expect "*$host*"; interact
posted @ 2019-06-26 11:28  穷开心y  阅读(1134)  评论(0编辑  收藏  举报