Bash 调用将输入传递给命令的Shell脚本,该命令需要来自终端的输入

Bash 调用将输入传递给命令的Shell脚本,该命令需要来自终端的输入,bash,shell,expect,Bash,Shell,Expect,我正在尝试编写一个脚本来调用以下命令 [root@xxxxx imtadmin]# admin tower ------------------------------------------------------------------- GMS: address=xxxxx-42450, cluster=CLRTY-SA, physical address=xxxxx:45155 ------------------------------------------------------

我正在尝试编写一个脚本来调用以下命令

[root@xxxxx imtadmin]# admin tower

-------------------------------------------------------------------
GMS: address=xxxxx-42450, cluster=CLRTY-SA, physical address=xxxxx:45155
-------------------------------------------------------------------

NSA Tower Shell

Usage:

 history                   Show admin shell command history.
 !<command prefix>         Execute a command maching the given prefix                            from the command history.
 refresh                   Wake up monitor thread, to update clients.
 autodiscovery <on|off>    Turn autodiscovery on or off.
 add <host(s)>             Add given host(s) by address.
 remove <host(s)>          Remove given host(s) by address.
 list clients              Print listing of clients.
 list services             Print listing of client services.
 select <address|index>    Select a specific client host by address.
 connect <address> [port]  Connect to a given Select a specific
                           client host by address.
 trace                     Send trace message on multicast channel.
 trace <on|off>            Turn trace listening on or off.
 password <password>       Change password for this session only.
 reset password            Reset password to local machine value.
 group <group>             Change channel group for this session only.
                           (such as CLRTY for listening to PPM
                           broadcasts.)
 reset group               Reset group to local machine value.
 list group                List group members.
 exit|quit                 Exit the tower.
 ?                         Print this message.

> list clients **TERMINAL INPUT***

Discovered Clients:
--------------------------
1) xxxxx.ec2.internal:9091 [xxxxx-20212]
--------------------------
> refresh **TERMINAL INPUT***
> list clients **TERMINAL INPUT***

Discovered Clients:
--------------------------
1) xxxxxx.ec2.internal:9091 [xxxxx-59053]
2) yyyyy.ec2.internal:9091 [yyyyy-20212]
3) zzzzzz.ec2.internal:9091 [zzzzz]
--------------------------
>

我的脚本确实调用命令admin tower并在提示符>处停止,但它不接受脚本提供的输入或我从终端提供的输入(我的目标是从脚本提供输入)。

这应该可以:

send "list clients\n"
expect eof

下面的步骤奏效了。谢谢,@Philippe暗示我需要在提供输入后添加expect

#!/usr/bin/expect

spawn /opt/clarity/bin/admin tower
expect -re "^>"
send "list clients\r"
expect -re "^>{1}"
send "refresh\r"
expect -re "^>{1}"
send "list clients\r"
expect -re "^>{1}"
send "exit\r"

据我所知,Philippe所做的不仅仅是暗示。在发送“exit”之后,您应该
期望eof
以允许管理程序优雅地退出。在运行每个命令之后,我返回提示符>我应该使用expect eof还是expect-re“^>{1}”?
期望eof
用于识别生成的进程何时结束。如果您发送一个命令并期望得到一个提示,那么
expect-re{>$}
(这是另一个识别提示的正则表达式:一个尖括号和一个行尾空格)。我假设您不希望在发送“退出”后出现提示。没错,我不希望在退出后出现提示。它将退出管理程序。不确定是否必须使用。如果您喜欢编写shell脚本,可以查看我的。
#!/usr/bin/expect

spawn /opt/clarity/bin/admin tower
expect -re "^>"
send "list clients\r"
expect -re "^>{1}"
send "refresh\r"
expect -re "^>{1}"
send "list clients\r"
expect -re "^>{1}"
send "exit\r"