【问题标题】:How can I call an expect script with a sequence of arguments如何使用一系列参数调用期望脚本
【发布时间】:2023-03-23 02:29:01
【问题描述】:

我有一个如下所示的期望脚本:

#!/usr/bin/expect
set path_start [lindex $argv 0]
set host [lindex $argv 1]
spawn ssh root@$host telnet jpaxdp
expect {\-> }

set fh [open ${path_start}${host} r]
while {[gets $fh line] != -1} {
    send "$line\r"
    expect {\-> }
}
close $fh

send "exit\r"
expect eof

我将其称为 ./script.sh cmds_ cc1,现在我的主机编号为 1 - 8,我尝试将脚本称为 ./script cmds_ cc[1-8],但这不起作用,因为脚本解释了 host[1-8] 作为论据并向我展示:

spawn ssh root@cc[1-8] telnet jpaxdp
ssh: Could not resolve hostname cc[1-8]: Name or service not known
couldn't open "cmds_cc[1-8]": no such file or directory
    while executing
"open ${path_start}${host} r"
    invoked from within
"set fh [open ${path_start}${host} r]"
    (file "./script.sh" line 7)

我怎样才能做到这一点?

【问题讨论】:

  • 这是一个 Expect 脚本,而不是 bash 脚本。
  • 可能有一个命令行实用程序会递归地执行这样的调用。如果快速谷歌搜索没有从 Linux 中找到任何内容,我会推荐另一个脚本。

标签: linux bash shell ssh expect


【解决方案1】:

cc[1-8] 是文件名通配符,它​​会查找与该模式匹配的文件。如果没有,则通配符本身保留在参数列表中。要获取一系列数字,请使用cc{1..8}。要重复运行该命令,您需要一个 for 循环。

for host in cc{1..8}
do
    ./script.sh cmds_ "$host"
done

【讨论】:

    猜你喜欢
    • 2015-08-07
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 2012-12-29
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多