cerr cerr - 1 year ago 56
Linux Question

How can I call an expect script with a sequence of arguments

I have an expect script that looks like:

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

and I call it like
./script.sh cmds_ cc1
, now my hosts are numbered 1 - 8 and I tried to call the script like
./script cmds_ cc[1-8]
but that didn't work as the script interpreted host[1-8] as argument and showed me:

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)

How can I make this work?

Answer Source

cc[1-8] is a filename wildcard, it looks for files that match that pattern. If there aren't any, the wildcard itself is kept in the argument list. To get a range of numbers, use cc{1..8}. And to run the command repeatedly, you need a for loop.

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