Scripting with Bash
Building an ipsweep.sh script step by step, piping ping output through grep, cut, and tr to pull out live IP addresses, then feeding them to nmap.
Isolating an IP from Ping Output
ifconfig
ping 192.168.4.29 -c 1 > ip.txt
-cstands for "count".
cat ip.txt | grep "64 bytes"
- Outputs: 64 bytes from 192.168.4.29: icmp_seq=1 ttl=128 time=0.351 ms.
|= pipe, which runs something after a condition is met.
cat ip.txt | grep "64 bytes" | cut -d " " -f 4
-dstands for "delimiter" and we're setting it to a blank space, " ".-fstands for "field" and '4' is for 4 spaces over.- This greps out just the IP address.
cat ip.txt | grep "64 bytes" | cut -d " " -f 4 | tr -d ":"
trstands for "translate".-d"delimiter" is removing the colon.
Script Arguments
$0= Argument 0 (ipsweep.sh)$1= Argument 1 (IP address)chmod +x ipsweep.shmakes the script executable.
The Script
Usage:
./ipsweep.sh 192.168.4 > ips.txt
#!/bin/bash
if [ "$1" == "" ]
then
echo "You forgot an IP address!"
echo "Syntax: ./ipsweep.sh 192.168.1"
else
for ip in `seq 1 254`; do
ping -c 1 $1.$ip | grep "64 bytes" | cut -d " " -f 4 | tr -d ":" &
done
fi
Then run nmap against each discovered host:
for ip in $(cat ips.txt); do nmap $ip; done