Copyright — https://www.misecurity.net/content/images/size/w960/2022/10/image.jpeg

TCM — Practical Ethical Hacking Course — Bash Scripting

Shivansh Seth

--

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 :

  1. ping <ip_address> -c 1 > ip.txt → This command is pinging the IP Addresses and storing the responses into the file named ip.txt
  2. cat ip.txt | grep "64 bytes" → This comand is opening the file iip.txt and finding 64 bytes in the text, and will print the records wherever it finds the exact same string.
  3. 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.
  4. 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 :

  1. mousepad ipsweep.sh → Creating a file name ipsweep and opening it with text editor mousepad.
  2. Then we will be specifying the Shebang in the script file : #!/bin/bash
  3. 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.

  1. Save the file.
  2. Provide it the permissions from rw to rwx using : chmod +x ipsweep.sh
  3. 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.

--

--