让我先说一下这个问题,我说使用像Expect这样的工具非常新,所以解决这个问题的方法可能相当简单明了。
我们正在尝试在AS400上自动关闭我们的WebSphere服务器,以实现我们必须每月运行的特定流程。这是通过调用从语句文件运行QSH会话的CL程序来实现的。实际上调用期望程序似乎工作正常。
我们遇到的问题是,看起来用于比较预期文本的任何内部变量(可能是exp0)在新命令运行时实际上并没有被赋予新值。以下是运行的语句文件中的代码:
#!/usr/local/bin/expect -f
set timeout 320
exp_internal 1
send "cd /qibm/proddata/websphere/appserver/v85/express/bin\r"
expect "$"
send "stopServer WAS85SVR\r"
expect {
default {exit 2}
"Username:" {send "username\r"; exp_continue}
"Password:" {send "password\r"; exp_continue}
"$"
}
expect "$"
期望说“用户名”和“密码”的发送通常包含实际的用户名和密码文本。以下是转储到运行日志文件中的内容:
send: sending "cd /qibm/proddata/websphere/appserver/v85/express/bin\r" to { exp0 cd /qibm/proddata/websphere/appserver/v85/express/bin
}
expect: does "" (spawn_id exp0) match glob pattern "$"? yes
expect: set expect_out(0,string) ""
expect: set expect_out(spawn_id) "exp0"
expect: set expect_out(buffer) ""
send: sending "stopServer WAS85SVR\r" to { exp0 stopServer WAS85SVR
}
expect: does "" (spawn_id exp0) match glob pattern "Username:"? no
"Password:"? no
"$"? yes
expect: set expect_out(0,string) ""
expect: set expect_out(spawn_id) "exp0"
expect: set expect_out(buffer) ""
expect: does "" (spawn_id exp0) match glob pattern "$"? yes
expect: set expect_out(0,string) ""
expect: set expect_out(spawn_id) "exp0"
expect: set expect_out(buffer) ""
“\ r \ n”已包含在日志文件的可读性中,我不相信它们最终会转换为传递的传输。此外,值得注意的是,“stopServer”命令在“Username:”提示出现之前运行大约10秒。我们已经尝试在预期“用户名:”之前加入延迟,但这似乎也没有用。
如果值得一看,这就是QSH中的手动/交互式会话看起来像“用户名:”提示:
$
> cd /qibm/proddata/websphere/appserver/v85/express/bin
$
> stopServer WAS85SVR
ADMU0116I: Tool information is being logged in file /QIBM/UserData/WebSphere/AppServer/V85/Express/profiles/WAS85SVR/logs/WAS85SVR/stopServer.log
ADMU0128I: Starting tool with the WAS85SVR profile
ADMU3100I: Reading configuration for server: WAS85SVR
Realm/Cell Name: <default>
Username: