Network Data Packet Statistics

LinuxLinuxBeginner
Practice Now

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 tcpdump command 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 timeout command
  • 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 tcpdump and timeout in 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.

  1. Open a text editor and create a new file named netcheck.sh in the /home/labex/project directory.
  2. Add the following code to the netcheck.sh file:
#!/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"
  1. Save the netcheck.sh file.

Test the netcheck.sh Script

In this step, you will test the netcheck.sh script to ensure it is working as expected.

  1. Open a terminal and navigate to the /home/labex/project directory.
  2. Run the netcheck.sh script with the port number 22 as an argument:
sh /home/labex/project/netcheck.sh 22
  1. The script should output the number of packets transmitted and received on port 22 within the 3-second timeframe, for example:
Packages: 2
  1. 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.

  1. The script starts with a shebang line #!/bin/zsh to specify that it should be executed using the Zsh shell.
  2. The script checks if a port number argument is provided. If not, it outputs an error message and exits.
  3. The script assigns the provided port number to the port variable.
  4. The script uses the timeout command to set a 3-second timeout and then uses the tcpdump command to count the number of packets for the specified port. The -c 0 option tells tcpdump to capture all packets, and the "port $port" filter ensures that only packets for the specified port are counted.
  5. The script captures the output of the tcpdump command and counts the number of lines using wc -l, which gives the total number of packets.
  6. 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.

âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Linux Tutorials you may like