前言
现在要想访问公司服务器都必须通过跳板机再跳到目标服务器,这么做是运维人员为了安全性考虑和可以高效管理公司庞大的服务器集群。但是我们都知道安全性提高后必定降低我们日常工作的效率,必须先登录到跳板机,再选目标服务器,每次访问服务器都增加了半分钟的操作成本,作为最会偷懒的我来说写个跳板机自动登录脚本是非常必要的。
expect 脚本
我们可以用expect脚本来实现自动化交互任务,不需要人为干预。如果不用expect脚本我们登录跳板机需要人机交互来输入执行命令,而使用expect脚本可以根据程序提示模拟标准输入提供给程序,从而实现自动化交互执行。
#!/usr/bin/expect
#等待30秒无操作退出
set timeout 30
# use ssh login your jump server
spawn ssh #your jump server eg: username@ip#
# start expect
expect {
# simulate input jump server password
"Password:"
{send "your password\n";exp_continue}
# simulate select server group
"Select group:"
# this case select 0 group
{send "0\n";exp_continue}
# simulate select page
"Select page:"
{send "0\n";exp_continue}
"Select server:"
# serverIp that you input
{send "[lindex $argv 0]";exp_continue}
"Input accou