我正在编写一个脚本,使用 Expect shell 收集 AIX 中每台服务器上适配器的微代码级别。
#!/bin/ksh
for hostname in ABCD123 ABCD234 ABCD445
do
expect << 'EOS'
set hos {$hostname}
spawn ssh padmin@$hostname
expect "Password:"
send "ABC1234\n"
expect "$"
send "oem_setup_env\n"
expect "#"
send "lsmcode -A | sed -e 's/^/$hos: /'\n"
expect "#"
send "exit\n"
expect "$"
send "exit\n"
EOS
done
但不幸的是脚本无法运行。你能帮我么
输出应该是这样的
ABCD123 : sys0!system:CL320_040 (t) CL320_040 (p) CL320_040 (t)
ABCD123 : ent0!14101103.CN0110
ABCD123 : ent1!14101103.CN0110
ABCD123 : ent2!14101103.CN0110
ABCD123 : ent3!14101103.CN0110
ABCD123 : ent4!14108802.DV0210
答案1
因为您使用的是带引号的heredoc,所以 shell 不会扩展 shell 变量$hostname
。这样做:通过环境传递期望的变量
export hostname
for hostname in ABCD123 ABCD234 ABCD445
do
expect << 'EOS'
set hos $env(hostname) ;# access the environment variable
spawn ssh padmin@$hos
expect "Password:"
send "ABC1234\r"
expect "$"
send "oem_setup_env\r"
expect "#"
send "lsmcode -A | sed -e 's/^/$hos: /'\r"
expect "#"
send "exit\r"
expect "$"
send "exit\r"
expect eof ;# wait for the connection to close
EOS
done
通常,您可以使用\r
“按 Enter”来发送命令。