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

问题描述

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

[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]
--------------------------
>

我的脚本:

#!/usr/bin/expect

spawn /opt/clarity/bin/admin tower
set timeout -1
expect -re "^>{1}"
send "list clients"

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

解决方法

这应该有效:

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"