bash - Shell Script - telnet multiple hosts:ports -
i'm not expert in shell script means. got structure idea post (bash script telnet test multiple addresses , ports) need verify lan connections between specific hosts , ports via telnet.
the reason using telnet fact both lan , machines heavily secure , don't have access netcat, nmap or /dev/tcp. i'm no near comfortable python or pearl try route... ( know silly me, i'll there though :p ).
the following code works, reasons beyond understanding while loop iterates once , no more... :( .
note: important me know if connection failed due timeout or refused (port closed @ endpoint).
can me in 1) fixing , 2) understanding why?
fyi: else might have similar need here's operational updated code script. in case connection refused being handled success (testing firewall rules) can changed failed depending on necessities.
#!/bin/bash path=`pwd`; touch $path/test_telnet.out || exit; touch $path/success.log || exit; touch $path/failed.log || exit; echo "10.192.168.1 1200 10.10.10.2 80 10.220.2.8 6090 10.220.2.9 6090" | ( while read host port; telnet $host $port </dev/null > $path/test_telnet.out 2>&1 & sleep 1; kill $!; if grep connected $path/test_telnet.out >/dev/null; echo @ $(date +"%b %d %h:%m %y") $host:$port [ open ] | tee -a $path/success_log.txt; elif grep refused $path/telnet_test.txt >/dev/null; echo @ $(date +"%b %d %h:%m %y") $host:$port [ refused ] | tee -a $path/success_log.txt; else echo @ $(date +"%b %d %h:%m %y") $host:$port [ timeout ] | tee -a $path/failed_log.txt; fi; cp /dev/null $path/test_telnet.out; done ) 2>/dev/null #avoid bash messages
as etan commented, telnet eating rest of input. fix redirect input telnet.
change this:
telnet $host $port > ~/test_con/telnet_test.txt
to this:
telnet $host $port </dev/null > ~/test_con/telnet_test.txt
Comments
Post a Comment