前言
现在要想访问公司服务器都必须通过跳板机再跳到目标服务器,这么做是运维人员为了安全性考虑和可以高效管理公司庞大的服务器集群。但是我们都知道安全性提高后必定降低我们日常工作的效率,必须先登录到跳板机,再选目标服务器,每次访问服务器都增加了半分钟的操作成本,作为最会偷懒的我来说写个跳板机自动登录脚本是非常必要的。
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 account:"
# account parameter
{send "[lindex $argv 1]\n";exp_continue}
#simulate input server's password
"[lindex $argv 1]@[lindex $argv 0]'s password:"
{send "[lindex $argv 2]\n"}
}
interact
以上就是模拟通过跳板机访问服务器的expect脚本。
iterm2配置脚本
![请添加图片描述](https://img-blog.csdnimg.cn/79f0a0313fb34d699b3883cf4b0cd699.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbGFuY2XlsI_noIHljKA=,size_20,color_FFFFFF,t_70,g_se,x_16)
配置好脚本将脚本放到/usr/local/bin目录下。
解决lrzsz工具配合expect脚本失效问题
你需要使用shell脚本调用expect脚本,如下:
#!/bin/sh
export LC_CTYPE=en_US
export /usr/local/bin/login.sh $1 $2 $3