How to check telnet for multiple IP at a time using Bash

How to check telnet for multiple IP at a time using Bash

Hello,

If you have multiple number of server and you would like check whether a particlur port open or not, generally you use telnet as below

[root@localhost ~]# telnet 192.168.56.102 22
Trying 192.168.56.102...
Connected to 192.168.56.102.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.5
^]

telnet> q
Connection closed.
[root@localhost ~]#

Once connectivity got established, you have to give CTRL+] and q to exit from telnet session.

For single IP this is fine but if you have multiple IPs then doing this for each IP is very annoying. But we can execute a perl command with inside a bash for loop with multiple IPs and after the threshold at alarm passed, it will exit automatically.

for ip in "192.168.56.102" "localhost"
do
perl -e "alarm 10; exec @ARGV" "telnet "${ip}" 22"
done

Here we dont have to pass any parameter to exit from telnet client and it will exit automatically after 10 sec.

[root@localhost ~]# for ip in "192.168.56.102" "localhost"; do perl -e "alarm 10; exec @ARGV" "telnet "${ip}" 22"; done
Trying 192.168.56.102...
Connected to 192.168.56.102.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.5
Alarm clock
Trying ::1...
Connected to localhost.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.5
Alarm clock
[root@localhost ~]#

Hope it will help you.

0 comments:

Post a Comment