Skip to main content

3 posts tagged with "network"

View All Tags

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.

Best Practices of Using curl

· 4 min read

Introduction​

curl is a powerful command-line tool used for transferring data with URLs. It supports a wide range of protocols, including HTTP, HTTPS, FTP, and more. This article provides a comprehensive guide to the best practices of using curl, covering various scenarios such as checking if an URL is alive, downloading files, making HTTP GET/POST requests, handling authentication, and dealing with HTTPS certificates.


Basic Usage​

Check if an URL is Alive​

To check if an URL is alive, you can use the -I (or --head) option to send a HEAD request:

curl -I https://example.com

This command will return the HTTP headers without downloading the content, allowing you to quickly check if the URL is accessible.

Download a File​

To download a file from a URL, simply use the curl command followed by the URL:

curl -O https://example.com/file.zip

The -O option saves the file with the same name as the remote file. If you want to save the file with a different name, use the -o option:

curl -o myfile.zip https://example.com/file.zip

HTTP Methods​

HTTP GET​

To make an HTTP GET request, use the curl command followed by the URL:

curl https://example.com/api/resource

HTTP POST with JSON Payload​

To make an HTTP POST request with a JSON payload, use the -X POST option and specify the JSON data using the -d option:

curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' https://example.com/api/resource

Authentication​

HTTP Basic Authentication​

To use HTTP Basic Authentication, use the -u option to specify the username and password:

curl -u username:password https://example.com/api/resource

Bearer Token Authentication​

To use Bearer Token Authentication, use the -H option to set the Authorization header:

curl -H "Authorization: Bearer YOUR_TOKEN" https://example.com/api/resource

HTTPS and Certificates​

HTTPS with Certificate​

To use HTTPS with a client certificate, use the --cert and --key options:

curl --cert client.crt --key client.key https://example.com/api/resource

Ignore HTTPS Certificate​

To ignore HTTPS certificate validation (not recommended for production), use the -k (or --insecure) option:

curl -k https://example.com/api/resource

Advanced Scenarios​

Custom Headers​

To send custom headers, use the -H option:

curl -H "X-Custom-Header: value" https://example.com/api/resource

Follow Redirects​

To follow HTTP redirects, use the -L (or --location) option:

curl -L https://example.com/redirect

Verbose Output​

To get verbose output, use the -v (or --verbose) option:

curl -v https://example.com/api/resource

Timeouts​

To set a timeout for the request, use the --connect-timeout and --max-time options:

curl --connect-timeout 5 --max-time 10 https://example.com/api/resource

Conclusion​

curl is a versatile and powerful tool for interacting with web services and downloading files from the command line. By mastering the various options and scenarios covered in this article, you can effectively use curl for a wide range of tasks, from simple URL checks to complex HTTP requests with authentication and custom headers.


References​

  1. curl Documentation
  2. curl Man Page
  3. HTTP Methods
  4. HTTP Authentication
  5. HTTPS

By following these best practices, you can leverage curl to its full potential and efficiently manage your web interactions from the command line.

Best Practices of tcpdump and netstat

· 4 min read

Introduction​

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


tcpdump​

Basic Usage​

To start capturing network traffic, use the following command:

tcpdump

This command captures all traffic on the default network interface.

Filtering Traffic​

To filter traffic by protocol, use the -i option:

tcpdump -i eth0

To filter traffic by IP address, use the host keyword:

tcpdump host 192.168.1.1

To filter traffic by port, use the port keyword:

tcpdump port 80

Saving Captures​

To save captured traffic to a file, use the -w option:

tcpdump -w capture.pcap

Reading Captures​

To read a saved capture file, use the -r option:

tcpdump -r capture.pcap

netstat​

Basic Usage​

To display all active connections, use the following command:

netstat -a

Displaying Active Connections​

To display active TCP connections, use the -t option:

netstat -at

Displaying Listening Ports​

To display listening ports, use the -l option:

netstat -lt

Displaying Network Statistics​

To display network statistics, use the -s option:

netstat -s

Other Network Analysis Commands​

ping​

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

ping google.com

traceroute​

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

traceroute google.com

nmap​

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

nmap 192.168.1.1

iftop​

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

iftop -i eth0

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 tools like tcpdump, netstat, ping, traceroute, nmap, and iftop to monitor, diagnose, and troubleshoot network issues.


References​

  1. tcpdump Man Page
  2. netstat Man Page
  3. ping Man Page
  4. traceroute Man Page
  5. nmap Man Page
  6. 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.