P3- Basic Bash Scripting
Hi Everyone! Today we are learning basic bash scripting. So let's begin.
We will learn about the following commands
grep: it will narrow down our results, for example, if we ping any IP, and we want to gather info, if ping was valid or not, so for that, we will use grep to narrow down, let say we have a list of pings of different IP addresses, and we want to get only those pings that were valid, we will use grep on that list and so narrowing our results.
cut, tr, both also narrow down our results.
let's get started
First, I am gonna ping my very own machine with my IP address
If we wanna send only one packet to see if the host machine is alive or not, we will use "-c 1" to send only one packet to the host.
Now as you can see, if I write -c 3, it will send only 3 packets, so it depends.
I am gonna put the ping results into a text file, and after placing the results in i.txt file, I cat the file, so it will give me the results that have been saved into the saved file that are the three packets that I have sent and have been saved in the file as shown below in the figure
and there is a possibility that the host machine is not alive, so in this case the ping command will not give anything in return
now moving onto narrowing the results, if we have a network and we want only those hosts that give us a "64 bytes" result, means that the host is alive, we will use grep.
so in my case, as I saved the results in a text file named ip.txt, we are gonna narrow down our saved results on that file through grep command
I write " | grep "64 bytes", which means that give me only those lines from the file that have "64 bytes" string in it, a pipe sign "|" is used when we are doing a combination of commands.
so we have narrow down our results, it is not giving us the extra things that have been written into the file,
again I make the results more specific, of the ip.txt file by tr command.
first, look at the command and its syntax
I write the command "| cut -d " " -f 4"
as described, "|" pipe sign is for the combination, that we are combining cut command with grep and cat command, "-d" is a delimiter, a delimiter is what we will be cutting on, which says that cut the spaces " ", then we have "-f" which means the field we want as a result.
so as a whole the command says that "cat ip.txt | grep "64 bytes" | cut -d " " -f 4", means display the txt file, pick up the lines that have "64 bytes " but give me only the 4th field, each word/field separated by spaces.
so as our 4th field is the IP address, so it will give us only the IP address from the file.
It has given us the IP address, but as we can see that it has given us the colons with the IP's, that we don't want, but as the colon is not separated by spaces, so it is considered as the part of the word/field.
To more cut down the result, to remove these colons from our result, we are gonna use the tr command
tr command simply means translate, with tr command I am adding another delimiter ":", which will take out this colon from the results and as you can see it is only giving us the IP's from the file ip.txt.
Now we know how to narrow down our results. now moving on to the scripting part.
I just made a file with the name of ipsweep.sh and wrote a script inside of it and saved it, the script that I wrote is shown below
The very first line "#/bin/bash" is showing that it is a bash script
Ok now moving towards the actual script, first, we see the line that we are familiar with that is "ping -c 1 $1.$ip | grep "64 bytes" | cut -s " " -f 4 | tr -d ":" &", okay I am describing this line in small parts for better understanding, "-c 1" means only send 1 packet that we already know, after that "$1.$ip" is written, $ip is the variable that we just declared within the for loop just above this line, and $1 means input, so each input will come in $ip variable, else in this line everything is same except for the ampersand at the end of this line, which means threading, if we are not gonna put an ampersand & at the end of line, then we have to do this thing one by one.
now coming to the for loop, it say "for IP in 'seq 1 254'; do", "IP" after "for" is just the variable as I described earlier, and then this thing " `seq 1 254` " means that whatever the IP we give in the sequence of 1-254, do something. so means we will just give the 3 octets of IP and it will add a sequence of 1-254 one by one inside the loop. and at the end a "done" is written that means the for loop is closed.
after saving the script, I am gonna give the input of 3 octets of IP and see the results.
(but first, we have to make this ipsweep.sh file an executable file, to changing its mode to executable by chmod +x ipsweep.sh as shown below)
if we ls, ipsweep.sh has been changed to green, which means that the mode has been changed to executable
so coming back to the execution of the script, I am gonna give input to the script by the command " ./ipsweep.sh <ip> ".
I am blurring my private IP address, but it is shown that I gave 3 octets of IP address.
so our script with for loop has been executed successfully and it gave me all those hosts in my network that are alive at the moment.
now I am writing this result into another separate file named "iplist.txt" again by executing the file and saving the result side by side by the below command. (this is saved for future use)
now let's just improve our script a little bit by using the if-else statement.
as you can see I put the if-else statement, that is saying that if the "$1" input equals to nothing, it will give a message, else it will execute the below code that we just executed, so for checking if-else output, I am giving nothing as an input.
the script is expecting three octets, but if we give one octet, we can just change the script, but it was a very basic example of Bash scripting.
The last thing we are doing today is looping in one line.
ok I saved our results in a text file that I named iplist.txt, we are gonna be doing Nmap scan on all of the saved IP addresses on the saved IP's inside the list with the help of a loop as shown below
I write the loops " for IP in $(cat iplist.txt); do Nmap -sS -p 80 -T4 $ip & done " says that for all IP's in the list file of list.txt, do a Nmap stealth scan on port 80, so in the above figure, the processes are starting and in below image, it is giving us the results, here is the example of it.
so what we did is, we did the scan on all the IP's in one time instead of to copy and paste it. it's just a basic example of bash scripting, it can be more advanced, but for today That's pretty much it.
If you have any questions regarding the bash scripting, let me know in the comment section, I will try my best to reply.
That's it for today! :)
We will learn about the following commands
- grep
- cut
- tr
- scripting with bash
- for loop
grep: it will narrow down our results, for example, if we ping any IP, and we want to gather info, if ping was valid or not, so for that, we will use grep to narrow down, let say we have a list of pings of different IP addresses, and we want to get only those pings that were valid, we will use grep on that list and so narrowing our results.
cut, tr, both also narrow down our results.
let's get started
First, I am gonna ping my very own machine with my IP address
If we wanna send only one packet to see if the host machine is alive or not, we will use "-c 1" to send only one packet to the host.
Now as you can see, if I write -c 3, it will send only 3 packets, so it depends.
I am gonna put the ping results into a text file, and after placing the results in i.txt file, I cat the file, so it will give me the results that have been saved into the saved file that are the three packets that I have sent and have been saved in the file as shown below in the figure
and there is a possibility that the host machine is not alive, so in this case the ping command will not give anything in return
now moving onto narrowing the results, if we have a network and we want only those hosts that give us a "64 bytes" result, means that the host is alive, we will use grep.
so in my case, as I saved the results in a text file named ip.txt, we are gonna narrow down our saved results on that file through grep command
I write " | grep "64 bytes", which means that give me only those lines from the file that have "64 bytes" string in it, a pipe sign "|" is used when we are doing a combination of commands.
so we have narrow down our results, it is not giving us the extra things that have been written into the file,
again I make the results more specific, of the ip.txt file by tr command.
first, look at the command and its syntax
I write the command "| cut -d " " -f 4"
as described, "|" pipe sign is for the combination, that we are combining cut command with grep and cat command, "-d" is a delimiter, a delimiter is what we will be cutting on, which says that cut the spaces " ", then we have "-f" which means the field we want as a result.
so as a whole the command says that "cat ip.txt | grep "64 bytes" | cut -d " " -f 4", means display the txt file, pick up the lines that have "64 bytes " but give me only the 4th field, each word/field separated by spaces.
so as our 4th field is the IP address, so it will give us only the IP address from the file.
It has given us the IP address, but as we can see that it has given us the colons with the IP's, that we don't want, but as the colon is not separated by spaces, so it is considered as the part of the word/field.
To more cut down the result, to remove these colons from our result, we are gonna use the tr command
tr command simply means translate, with tr command I am adding another delimiter ":", which will take out this colon from the results and as you can see it is only giving us the IP's from the file ip.txt.
Now we know how to narrow down our results. now moving on to the scripting part.
I just made a file with the name of ipsweep.sh and wrote a script inside of it and saved it, the script that I wrote is shown below
The very first line "#/bin/bash" is showing that it is a bash script
Ok now moving towards the actual script, first, we see the line that we are familiar with that is "ping -c 1 $1.$ip | grep "64 bytes" | cut -s " " -f 4 | tr -d ":" &", okay I am describing this line in small parts for better understanding, "-c 1" means only send 1 packet that we already know, after that "$1.$ip" is written, $ip is the variable that we just declared within the for loop just above this line, and $1 means input, so each input will come in $ip variable, else in this line everything is same except for the ampersand at the end of this line, which means threading, if we are not gonna put an ampersand & at the end of line, then we have to do this thing one by one.
now coming to the for loop, it say "for IP in 'seq 1 254'; do", "IP" after "for" is just the variable as I described earlier, and then this thing " `seq 1 254` " means that whatever the IP we give in the sequence of 1-254, do something. so means we will just give the 3 octets of IP and it will add a sequence of 1-254 one by one inside the loop. and at the end a "done" is written that means the for loop is closed.
after saving the script, I am gonna give the input of 3 octets of IP and see the results.
(but first, we have to make this ipsweep.sh file an executable file, to changing its mode to executable by chmod +x ipsweep.sh as shown below)
if we ls, ipsweep.sh has been changed to green, which means that the mode has been changed to executable
so coming back to the execution of the script, I am gonna give input to the script by the command " ./ipsweep.sh <ip> ".
I am blurring my private IP address, but it is shown that I gave 3 octets of IP address.
so our script with for loop has been executed successfully and it gave me all those hosts in my network that are alive at the moment.
now I am writing this result into another separate file named "iplist.txt" again by executing the file and saving the result side by side by the below command. (this is saved for future use)
now let's just improve our script a little bit by using the if-else statement.
as you can see I put the if-else statement, that is saying that if the "$1" input equals to nothing, it will give a message, else it will execute the below code that we just executed, so for checking if-else output, I am giving nothing as an input.
the script is expecting three octets, but if we give one octet, we can just change the script, but it was a very basic example of Bash scripting.
The last thing we are doing today is looping in one line.
ok I saved our results in a text file that I named iplist.txt, we are gonna be doing Nmap scan on all of the saved IP addresses on the saved IP's inside the list with the help of a loop as shown below
I write the loops " for IP in $(cat iplist.txt); do Nmap -sS -p 80 -T4 $ip & done " says that for all IP's in the list file of list.txt, do a Nmap stealth scan on port 80, so in the above figure, the processes are starting and in below image, it is giving us the results, here is the example of it.
so what we did is, we did the scan on all the IP's in one time instead of to copy and paste it. it's just a basic example of bash scripting, it can be more advanced, but for today That's pretty much it.
If you have any questions regarding the bash scripting, let me know in the comment section, I will try my best to reply.
That's it for today! :)
https://www.youtube.com/channel/UCLs0xuT8eJ5FeifPjemHzhw/playlists?view_as=subscriber
ReplyDeleteExcellent, plz also share the clear output of said cmd
ReplyDeletecat ip.txt | grep "64 bytes" | cut -d " " -f 4
2nd, use virtual machine so you dont need to hide ip & other things etc which not impacts on reader good.
Thank you, Sure i will..
Deleteand also the output is there. take a look. (irrespective of the fact that IP is blurred, but i explained each and every thing) thanks .
DeleteSelling USA FRESH SSN Leads/Fullz, along with Driving License/ID Number with good connectivity.
ReplyDelete**Price for One SSN lead 2$**
All SSN's are Tested & Verified. Fresh spammed data.
**DETAILS IN LEADS/FULLZ**
->FULL NAME
->SSN
->DATE OF BIRTH
->DRIVING LICENSE NUMBER
->ADDRESS WITH ZIP
->PHONE NUMBER, EMAIL
->EMPLOYEE DETAILS
->Bulk order negotiable
->Hope for the long term business
->You can asked for specific states too
**Contact 24/7**
Whatsapp > +923172721122
Email > leads.sellers1212@gmail.com
Telegram > @leadsupplier
ICQ > 752822040