August 17, 2015

Shell Script to find Live Machine and Open Port

This article provides how shell scripts could be used to find live machine and open ports.


1. Scan Live Host in your Network [Ping Sweep]
In this example  the shell script will scan all the host within network 192.168.8.0. ICMP ping method is used to find the live host. Following shell script will send single ping packet to all hosts within address range 192.168.8.1-255. If  reply was successful then it means host is live otherwise it may be or may not be as targeted host machine might have configured firewall to block ICMP probe packets.

#!/bin/bash for ip in {1..255}
do
ping 192.168.8.$ip -c 1 >  /dev/null
[ $? -eq 0 ]     &&    echo "Host 192.168.8.$ip is UP"
done

NOTE: Above shell script will not show proper result if target machine has blocked icmp probe or packet get drop due to network failure.



2. Find Open TCP/UDP Port
As per http://netcat.sourceforge.net/,  Netcat is a networking utility which reads and write data across network connections, using the TCP/IP protocol. Netcat has been used in below shell script to find the open ports.
#!/bin/bash
for port in {1..65535} 
do
nc -z    $1  $port  |  awk  '{print  $4, $6}'  |  tr   -d  "[]"
nc -uz  $1  $port  |  awk  '{print  $4, $6}'  |  tr   -d  "[]"
done



No comments:

Post a Comment