- 本公司是通过齐治堡垒机连接远程服务器的环境,因为连接过程中需要自动输入密码和选择主机,所以要使用expect工具,编写expect脚本
- remote.exp
#!/usr/bin/expectif { $argc != 7 } {send_user "usage: expect $argv0 \[JUMP_HOST\] \[JUMP_PORT\] \[JUMP_USER\] \[JUMP_PASSWORD\] \[HOST\] \[USER\] \[PASSWORD\]\n\n\t"send_user "*JUMP开头\t是堡垒机的登录信息\n\t"send_user "*非JUMP开头\t是连接目标主机的登录信息."exit
}set timeout 5set jump_host [lindex $argv 0]
set jump_port [lindex $argv 1]
set jump_user [lindex $argv 2]
set jump_pswd [lindex $argv 3]
set host [lindex $argv 4]
set user [lindex $argv 5]
set pswd [lindex $argv 6]# 连接堡垒机
spawn ssh -p$jump_port $jump_user@$jump_host
# 登录堡垒机
expect {"*(yes/no*)?" {send "yes\r"expect "*assword:"send "$jump_pswd\r"}# 接受到password输入密码"*assword:" {send "$jump_pswd\r"}
}
# 选择登录主机
expect {"*:" {send "$host\r"}
}
# 选择登录用户 1 是 any
expect {"*:" {send "1\r"}
}
# 输入主机用户
expect {"*login:" {send "$user\r"}
}
# 输入主机密码
expect {"*assword:" {send "$pswd\r"}
}interact
- 配置iterm2 profile
- Send text at start: /Users/aaa/scripts/remote.exp 堡垒机ip 堡垒机ssh端口 堡垒机ssh用户 堡垒机ssh密码 跳转主机ip 跳转主机ssh用户 跳转主机ssh密码