Exit status redirection

Hi,

I'm having this simple code below, the file serverlist has a list of IPs one per line. When executed the while loop is executed only once, after that the program terminates. How should i redirect the exit status, so that the entire list of IP will get executed?

#!/bin/bash
exec < serverlist
while read i
do
echo "Source IP: 192.168.1.2"
echo "Destination IP: $i"
echo "#ping $i"
ping $i
echo "#telnet $i 22"
telnet $i 22
echo "#traceroute -m 5 $i"
traceroute -m 5 $i 2>/dev/null
done

Hi.

Try

...
exec 3< serverlist
while read i <&3
...
1 Like

Thank you, it worked.

Could you reason, what was causing that?

Hi.

You're reading standard input from a file, which telnet closes when it's finished.

In ssh, for example, there's the -n option to get around this, but I couldn't see any equivalent telnet option, so instead, read the file from a different descriptor.

Thank you for your time in this, that was informational.