That works just fine in my bash
:
$ cat hosts.txt
machine1
machine3
$ cat hosts.txt | while read h; do telnet $h; done
Trying 9.190.123.47...
telnet: Unable to connect to remote host: Connection refused
Trying 9.190.123.61...
telnet: Unable to connect to remote host: Connection refused
However, when I connect to a machine that doesn't refuse the connection, I get:
$ cat hosts.txt
machine2
machine1
machine3
$ cat hosts.txt | while read h; do telnet $h; done
Trying 9.190.123.48...
Connected to machine2.
Escape character is '^]'.
Connection closed by foreign host.
That's because I'm actually connecting successfully and all the other host names are being sent to the telnet
session. These are no doubt being used to attempt a login, the remaining host names are invalid as user/password and the session is being closed because of that.
If you just want to log in interactively to each system, you can use:
for h in $(cat hosts.txt); do telnet $h 1023; done
which will not capture the rest of the host names into the first successful session.
If you want to truly automate a telnet session, you should look into a tool such as expect
or using one of the remoting UNIX tools such as rsh
.