Search

Scripting w Bash

cat ip.txt | grep "64 bytes"

grep finds the lines with 64 bytes and cat prints them

cat ip.txt | grep "64 bytes" | cut -d " " -f 4

-d is delimiter

the “ “ is space for the delimiter

-f is field to count up to (in this case count up to 4)

from this output:

64 bytes from 69.142.177.233: icmp_seq=1 ttl=128 time=1.29 ms

4 delimiters would bring back:

69.142.177.233:
cat ip.txt | grep "64 bytes" | cut -d " " -f 4 | tr -d ":"

tr is translate with a delimiter that removes the : from the IP so the output can be:

69.142.177.233

Making a Script file

mousepad ipsweep.sh

opens a new notepad named that^

Inside mousepad:

#!/bin/bash

known as “shabang” bin bash. Tells the machine to use bash and where it is

#!/bin/bash

for ip in `seq 1 254`; do
ping -c 1 69.142.177.$ip | grep "64 bytes" | cut -d " " -f 4 | tr -d ":" &
done

for IP in sequence from 1 to 254, ping IP and replace $ip for each number in sequence

#!/bin/bash

for ip in `seq 1 254`; do
ping -c 1 $1.$ip | grep "64 bytes" | cut -d " " -f 4 | tr -d ":" &
done

makes it more modular. Command:

./ipsweep.sh 69.142.177

would run the hardcoded IP from previous.

Full script:

#!/bin/bash

if [ "$1" == "" ]
then
echo "You forgot an IP address!"
echo "Syntax: ./ipsweep.sh 10.0.0"

else
for ip in `seq 1 254`; do
ping -c 1 $1.$ip | grep "64 bytes" | cut -d " " -f 4 | tr -d ":" &
done
fi

if statement to make sure there’s always an IP provided by user

the & at the end of ping command makes multiple instances run at once; 2 does not wait for 1 to finish first and 1 does not wait for 0 to finish first.

fi closes the if statement in bash.

Command to run and store IPs:

./ipsweep.sh 69.142.177 > ips.txt

Into command (to see IPs collected):

cat ips.txt

Example nmap scan:

nmap -T4 -A -p-

run nmap, -A is look at everything, -p- is all ports

similar to:

nmap 10.0.0.0

to use the list we checked for valids in nmap:

for ip in $(cat ips.txt); do nmap $ip & done