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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicFileOperationsGroup -.-> linux/wc("`Text Counting`") linux/BasicSystemCommandsGroup -.-> linux/exit("`Shell Exiting`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/UserandGroupManagementGroup -.-> linux/set("`Shell Setting`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/ControlFlowGroup -.-> shell/exit_status("`Exit and Return Status`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/AdvancedScriptingConceptsGroup -.-> shell/adv_redirection("`Advanced Redirection`") shell/SystemInteractionandConfigurationGroup -.-> shell/shell_options("`Shell Options and Attributes`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-301481{{"`Network Data Packet Statistics`"}} linux/wc -.-> lab-301481{{"`Network Data Packet Statistics`"}} linux/exit -.-> lab-301481{{"`Network Data Packet Statistics`"}} linux/echo -.-> lab-301481{{"`Network Data Packet Statistics`"}} linux/pipeline -.-> lab-301481{{"`Network Data Packet Statistics`"}} linux/redirect -.-> lab-301481{{"`Network Data Packet Statistics`"}} linux/set -.-> lab-301481{{"`Network Data Packet Statistics`"}} shell/shebang -.-> lab-301481{{"`Network Data Packet Statistics`"}} shell/comments -.-> lab-301481{{"`Network Data Packet Statistics`"}} shell/quoting -.-> lab-301481{{"`Network Data Packet Statistics`"}} shell/variables_decl -.-> lab-301481{{"`Network Data Packet Statistics`"}} shell/variables_usage -.-> lab-301481{{"`Network Data Packet Statistics`"}} shell/for_loops -.-> lab-301481{{"`Network Data Packet Statistics`"}} shell/cond_expr -.-> lab-301481{{"`Network Data Packet Statistics`"}} shell/exit_status -.-> lab-301481{{"`Network Data Packet Statistics`"}} shell/cmd_substitution -.-> lab-301481{{"`Network Data Packet Statistics`"}} shell/subshells -.-> lab-301481{{"`Network Data Packet Statistics`"}} shell/adv_redirection -.-> lab-301481{{"`Network Data Packet Statistics`"}} shell/shell_options -.-> lab-301481{{"`Network Data Packet Statistics`"}} shell/globbing_expansion -.-> lab-301481{{"`Network Data Packet Statistics`"}} end

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.

Summary

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

Other Linux Tutorials you may like