一些背景: 使用一台安装了VMA(vSphere Management Assistant)的计算机尝试telnet一个IP,通过脚本修改VM的某些配置。
1.首先,我必须说我可以telnet手动连接的IP,但是当我尝试通过shell脚本进行telnet时,它会一直失败并收到Connection closed by foreign host
。
2.然后我更改为使用Expect,并执行相同的操作,我可以成功连接到IP但脚本在中间运行,会话似乎超时,请参阅以下日志:
vi-admin@localhost:~>
vi-admin@localhost:~> more telnetTest.sh
#!/usr/bin/expect
set timeout 20
set ip 10.222.99.108
set port 7001
set user "admin"
set password ""
spawn telnet $ip $port
expect "'^]'."
send "\r";
expect {
"login:" {
send "$user\r"
expect "Password:"
send "$password\r"
expect "FGVM*"
send "config system interface\r"
}
"FGVM*#" { send "config system interface\r"}
}
vi-admin@localhost:~> expect telnetTest.sh
spawn telnet 10.222.99.108 7001
Trying 10.222.99.108...
Connected to 10.222.99.108.
Escape character is '^]'.
FGVM02AO20310001 #
FGVM02AO20310001 # \777\774%vi-admin@localhost:~>
vi-admin@localhost:~>