In: Computer Science
In Kali Linux
Write a script that ask the user to enter an IP address and a port number, then use the provided entries from the user to perform a query on your local network and determine if the given port is open on the provide network. Need to submit solutions for both below.
1.A short report showing the functionality of your code
2. your bash script
As per your convenience, I have created this script in Kali
Linux Operating system. Here is the code:
Input:
Here is the screen shot of the execution of the code:
Output:
chmod +777 is used to provide 3 types of permissions to the user that are the ability to read, write and execute the file. This should be performed before the execution of the file or else it won't work.
1) At first, the user is asked to enter the ip address and port
number which are then stored in 'hostname' and 'port'
respectively.
We use the simple technique of echoing some port value using bash
network redirection feature. The bash returns the value either True
or False depending on the ability to connect to that port on the
host.
This is achieved using the code "2>/dev/null echo >
/dev/tcp/$hostname/$port" which allows us to connect to the
respective port using a Transmission Control Protocol
(TCP) on the given IP Address by the user.
Once there is response by the TCP, an if clause is used to print
the ports whether it is open or closed. The term
"$?" in " if [ $? == 0 ] " checks
the return value of grep . In this scenario,
'0' is Referred to as "True" or "Success". If
return value is a success, echo should output as " $port
requested is open" and if it is closed, "$port
requested is close" will be printed.
2) Bash Script
echo -n "Enter an IP Address: "
read hostname
echo -n "Enter a port number: "
read port
2>/dev/null echo > /dev/tcp/$hostname/$port
if [ $? == 0 ]
then
echo " $port requested is open"
else
echo " $port requested is closed"
fi
If you have any doubts, leave a comment below before rating. I'll happy to assist you further.
Do upvote this if it helped you. Thank you