通过 expect 脚本自动登录堡垒机和服务器

正好今天公司的活不忙,虽然加班到十点,但这主要是因为其它业务出现 bug。

在整个白天,除了修复掉一个自己的 bug 之外,大部分时间都是在搞怎么自动登录堡垒机和服务器。

最终,有了一个解决方案,但是依然不够完美,因为在目前的解决方案之下,说不定什么时候就需要更改一些东西。

遗留的问题是,怎么在 expect 脚本中执行 /ip 来搜索。

PS: 此问题已经解决,详情请见:http://wenxiheshang.github.io/blog/2018/10/13/神器-autoexpect-在手-shell-自动化从此不再是梦

# !/usr/bin/expect
# 参数0:j\_string  参数1:jumpserver

if { $argc != 2 } {
	send_user "Usage:  j_string, jumpserver \n"
	exit
}

set timeout -1


set j\_string [lindex $argv 0]

set jumpserver [lindex $argv 1]

# 登录跳板机
catch {spawn ssh $jumpserver}

# 登录目标机
expect "IP:"
send "$j\_string"
send "\r"
expect "USERNAME"
send "username\r"
expect "*assword*"
send "password"
send "\r"
expect "*\~*"
send "sudo su -\r"
expect "*username*"
send "password\r"
expect "*\~*"
send "screen -r supervisor\r"
interact

不知是该恭喜,还是该怎样,总之阅读到该文的,你是第 人。每一次刷新,都是不同的自己。