Skip to main content

Best Practices of Using Command Line Tools for Network Analysis

· 4 min read

Introduction​

Network analysis is a critical skill for system administrators and network engineers. Command line tools like ping, traceroute, netstat, tcpdump, nmap, iftop, and wireshark provide powerful capabilities for monitoring, diagnosing, and troubleshooting network issues. This article provides a comprehensive guide to the best practices of using these tools, covering various scenarios such as capturing and filtering network traffic, displaying network statistics, and diagnosing network issues.


Basic Network Analysis​

Ping​

To check connectivity to a host, use the ping command:

ping google.com

This command sends ICMP echo requests to the specified host and displays the response time.

Traceroute​

To trace the route to a host, use the traceroute command:

traceroute google.com

This command shows the path packets take to reach the specified host.

Netstat​

To display network statistics and active connections, use the netstat command:

netstat -a

This command displays all active connections and listening ports.

Netcat​

To test network connections and transfer data, use the nc (netcat) command:

nc -zv google.com 80

This command checks if port 80 is open on google.com.


Advanced Network Analysis​

Tcpdump​

To capture and analyze network traffic, use the tcpdump command:

tcpdump -i eth0

This command captures all traffic on the eth0 interface.

Wireshark​

To capture and analyze network traffic with a graphical interface, use wireshark:

wireshark

This tool provides advanced filtering and analysis capabilities.

Nmap​

To scan for open ports and services on a host, use the nmap command:

nmap 192.168.1.1

This command scans the specified IP address for open ports and services.

Iftop​

To monitor network traffic in real-time, use the iftop command:

iftop -i eth0

This command displays real-time network traffic on the eth0 interface.


Combining Commands​

Using grep with tcpdump​

To filter tcpdump output, use grep:

tcpdump -i eth0 | grep "HTTP"

This command filters tcpdump output to show only HTTP traffic.

Using awk with netstat​

To process netstat output, use awk:

netstat -an | awk '/:80 / {print $5}'

This command extracts the IP addresses of connections on port 80.

Using sed with nmap​

To process nmap output, use sed:

nmap 192.168.1.1 | sed -n '/open/p'

This command filters nmap output to show only open ports.


Advanced Scenarios​

Analyzing HTTP Traffic​

To capture and analyze HTTP traffic, use tcpdump with port filtering:

tcpdump -i eth0 port 80

Monitoring Real-Time Traffic​

To monitor real-time traffic on a specific interface, use iftop:

iftop -i eth0

Diagnosing Network Issues​

To diagnose network issues, use a combination of ping, traceroute, and tcpdump:

ping google.com
traceroute google.com
tcpdump -i eth0 host google.com

Conclusion​

Network analysis is a critical skill for maintaining and troubleshooting network infrastructure. By mastering the various options and scenarios covered in this article, you can effectively use command line tools like ping, traceroute, netstat, tcpdump, nmap, iftop, and wireshark to monitor, diagnose, and troubleshoot network issues.


References​

  1. ping Man Page
  2. traceroute Man Page
  3. netstat Man Page
  4. nc (netcat) Man Page
  5. tcpdump Man Page
  6. Wireshark Documentation
  7. nmap Man Page
  8. iftop Man Page

By following these best practices, you can leverage these network analysis tools to their full potential and efficiently manage your network monitoring and troubleshooting tasks.