TCM — Practical Ethical Hacking Course — Bash Scripting
Here are the notes that I made during the Bash Scripting Video :
grep
→ It is used to find a string from a file or directory.
If we’re storing the IP Addresses while pinging an IP Address we can use the command :
ping <ip_address> -c 1 > ip.txt
→ This command is pinging the IP Addresses and storing the responses into the file namedip.txt
cat ip.txt | grep "64 bytes"
→ This comand is opening the fileiip.txt
and finding64 bytes
in the text, and will print the records wherever it finds the exact same string.cat ip.txt | grep "64 bytes"| cut -d " " -f 4
→ This command will additionally remove the words present before 4th space and will print only the 4th word.cat ip.txt | grep "64 bytes"| cut -d " " -f 4 | tr -d ":"
→ This command will additionally remove the:
from the result.
Now to write a bash script we can follow the steps below :
mousepad ipsweep.sh
→ Creating a file nameipsweep
and opening it with text editormousepad
.- Then we will be specifying the Shebang in the script file :
#!/bin/bash
- Now, we can simply add our bash code into it.
#!/bin/bash for ip in 'seq 1 254'; do
ping -c 1 $1.$ip | grep "64 bytes" | cut -d " " -f 4 | tr -d ":" &
done
./ipsweep 192.168.4
Here $1
works as the argument we’re providing in the running command 192.168.4
.
$ip
is working as the iterator mentioned in for
loop.
- Save the file.
- Provide it the permissions from rw to rwx using :
chmod +x ipsweep.sh
- Now, run it using
./ipsweep.sh 192.168.4
Now, we will be making changes as instructed:
#!/bin/bash
if [ "$1" == "" ]
then
echo "You forgot an IP Address!"
echo "Syntax : ./ipsweep.sh 192.168.4"
else
for ip in 'seq 1 254'; do
ping -c 1 $1.$ip | grep "64 bytes" | cut -d " " -f 4 | tr -d ":" &
done
fi
$
signs to make the process faster.
Now as we have created a script to automate the process to collect IP Address responsive in the Network, we will store that in a txt file using : ./ipsweep.sh 192.168.4 > ips.txt
Now we can simply create an automated script that can help us to run nmap
on these collected IPs.
The command will be : for ip in $(cat ips.txt); do nmap $ip; done
Thankyou✨!!
Clap👏 if you like the blog.