Introduction
In this project, you will learn how to create a Zsh script that monitors the network communication status of a specific port. The script will count the number of data packets transmitted and received on the specified port within a 3-second timeframe.
👀 Preview
## Example
$ sh /home/labex/project/netcheck.sh 22
Packages: 2
🎯 Tasks
In this project, you will learn:
- How to create a Zsh script that accepts a port number as an input parameter
- How to use the
tcpdumpcommand to capture and count the number of packets for the specified port - How to set a 3-second timeout for the script execution using the
timeoutcommand - How to output the count of packets in a user-friendly format
🏆 Achievements
After completing this project, you will be able to:
- Develop a script that can monitor the network communication status of a specific port
- Understand how to use common Linux commands like
tcpdumpandtimeoutin a Zsh script - Gain experience in writing and testing shell scripts to solve practical problems
Create the netcheck.sh Script
In this step, you will create the netcheck.sh script that will count the number of packets transmitted and received for a specified port within a 3-second timeframe.
- Open a text editor and create a new file named
netcheck.shin the/home/labex/projectdirectory. - Add the following code to the
netcheck.shfile:
#!/bin/zsh
## This script counts the number of packets transmitted and received for a specified port within a 3-second timeframe using tcpdump.
## Check if the port number argument is provided
if [ $## -eq 0 ]; then
echo "Please provide the port number to be counted as an argument."
exit 1
fi
## Get the input port number
port=$1
## Use the timeout command to set a 3-second timeout and use tcpdump to count the number of packets for the specified port
packages=$(timeout 3 tcpdump -c 0 -i any "port $port" 2> /dev/null | wc -l)
## Output the count of packets
echo "Packages: $packages"
- Save the
netcheck.shfile.
Test the netcheck.sh Script
In this step, you will test the netcheck.sh script to ensure it is working as expected.
- Open a terminal and navigate to the
/home/labex/projectdirectory. - Run the
netcheck.shscript with the port number22as an argument:
sh /home/labex/project/netcheck.sh 22
- The script should output the number of packets transmitted and received on port
22within the 3-second timeframe, for example:
Packages: 2
- Try running the script with different port numbers to ensure it works correctly.
Understand the netcheck.sh Script
In this step, you will understand the code in the netcheck.sh script.
- The script starts with a shebang line
#!/bin/zshto specify that it should be executed using the Zsh shell. - The script checks if a port number argument is provided. If not, it outputs an error message and exits.
- The script assigns the provided port number to the
portvariable. - The script uses the
timeoutcommand to set a 3-second timeout and then uses thetcpdumpcommand to count the number of packets for the specified port. The-c 0option tellstcpdumpto capture all packets, and the"port $port"filter ensures that only packets for the specified port are counted. - The script captures the output of the
tcpdumpcommand and counts the number of lines usingwc -l, which gives the total number of packets. - Finally, the script outputs the count of packets using
echo "Packages: $packages".
Congratulations! You have successfully created and tested the netcheck.sh script to monitor network communication status. Feel free to explore and modify the script further to suit your needs.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



