Expect script error : send: spawn id exp4 not open

Hi,

I am executing a expect script in while loop for doing telnet on list of servers stored in file as below :
expect script :

#!/usr/bin/expect -f
set timeout 20
set ip [lindex $argv 0]
set port [lindex $argv 1]
if {[llength $argv] == 0} {
  send_user "Usage: scriptname ip port\n"
  exit 1
}
#exp_internal 1
log_user 0
spawn telnet $ip $port
sleep 5
expect {
timeout { send_user "\nTelnet to $ip on $port was Unsuccessfull\n";exit 1 }
"'^]'"
}
send "\x1D"
expect "telnet> "
send "quit\r"
send_user "\nTelnet to $ip on $port was successfull\n"
expect eof

bash script for looping expect script :

#!/bin/bash
n=`wc -l /tmp/DSlist | awk '{ print $1 }'`
i=1
while [ $i -le $n ]
do
line=`head -$i /tmp/DSlist | tail -1`
ip=`echo $line | cut -f 1 -d ' '`
port=`echo $line | cut -f 2 -d ' '`
./test1.sh $ip $port
sleep 1
i=`expr $i + 1`
done

script is getting executed properly but getting message as below in between the script :

send: spawn id exp4 not open
    while executing
"send "\x1D""
    (file "./test1.sh" line 17)

can someone please let me know where i am going wrong. or are there any additional steps that i have to include in my script to avoid this error.

---------- Post updated at 06:29 AM ---------- Previous update was at 01:48 AM ----------

can someone please assist here..

We are not "on call". If someone doesn't answer your question immediately, and you have no further information to provide, wait! I didn't get here just because you bumped it.

"spawn id exp4 not open" is the message expect gives when the thing you're communicating with, telnet in this case, dies while you're trying to use it. Try telneting to that IP / port manually and see what it does.

Your shell script can probably be made far more efficient -- you don't need the help of head, tail, and cut to read and tokenize lines from a file one by one, there's a command just for that.

while read ip port
do
        echo "$ip $port"
done < /tmp/DSlist
1 Like

Does the error still occur if you remove the `sleep 5` after the spawn?

@Corona688 thanks for the shortening while loop that has worked. Also the error was due to additional space in DSlist file after one of the ips. so the expect script posted in my question is final and working one.

1 Like