Network diagnostics
Table of contents
- ping and traceroute
- netstat and ss
- nslookup and dig
- curl and wget
- Introduction to tcpdump
- Hands-on exercises
1 - ping and traceroute
ping - Test connectivity
# Ping basique
ping google.com
# Nombre de paquets limité
ping -c 4 google.com
# Intervalle personnalisé
ping -i 2 google.com # 2 secondes entre chaque
# Taille de paquet
ping -s 1000 google.com
# Ping avec timeout
ping -W 2 -c 3 192.168.1.1
Typical output:
PING google.com (142.250.74.238) 56(84) bytes of data.
64 bytes from par21s17-in-f14.1e100.net: icmp_seq=1 ttl=117 time=12.3 ms
64 bytes from par21s17-in-f14.1e100.net: icmp_seq=2 ttl=117 time=11.8 ms
--- google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 11.800/12.050/12.300/0.250 ms
| Information | Meaning |
|---|---|
ttl | Time To Live (number of hops remaining) |
time | Latency in milliseconds |
packet loss | Percentage of packets lost |
traceroute - Network path
# Installer si nécessaire
sudo apt install traceroute
# Tracer le chemin
traceroute google.com
# Avec ICMP (comme ping)
sudo traceroute -I google.com
# Limiter les sauts
traceroute -m 15 google.com
Typical output:
traceroute to google.com (142.250.74.238), 30 hops max
1 192.168.1.1 (192.168.1.1) 1.234 ms 1.123 ms 1.098 ms
2 10.0.0.1 (10.0.0.1) 5.432 ms 5.321 ms 5.210 ms
3 * * *
4 72.14.215.85 (72.14.215.85) 12.345 ms 12.234 ms 12.123 ms
note
The * * * indicate a router that does not respond (often a firewall).
mtr - An improved traceroute
# Installer
sudo apt install mtr
# Mode interactif
mtr google.com
# Mode rapport
mtr -r -c 10 google.com
🔝 Back to table of contents
2 - netstat and ss
ss - Socket Statistics (modern)
# Toutes les connexions
ss
# Connexions TCP établies
ss -t
# Connexions en écoute
ss -l
# Avec numéros de ports (pas de résolution)
ss -n
# Avec processus associés
ss -p
# Combinaison courante
ss -tulnp
| Option | Description |
|---|---|
-t | TCP |
-u | UDP |
-l | Listening |
-n | Numeric (no DNS resolution) |
-p | Process |
-a | All (established + listening) |
Practical ss examples
# Ports en écoute
sudo ss -tulnp
# Connexions HTTP
ss -t state established '( dport = :80 or sport = :80 )'
# Connexions SSH
ss -t state established '( dport = :22 or sport = :22 )'
# Statistiques
ss -s
netstat - Legacy but useful
# Installer
sudo apt install net-tools
# Ports en écoute
netstat -tulnp
# Connexions établies
netstat -an | grep ESTABLISHED
# Statistiques
netstat -s
Correspondence table
| netstat | ss | Description |
|---|---|---|
netstat -tulnp | ss -tulnp | Listening ports |
netstat -an | ss -an | All connections |
netstat -r | ip route | Routing table |
netstat -i | ip -s link | Interface statistics |
🔝 Back to table of contents
3 - nslookup and dig
nslookup - Simple DNS queries
# Résoudre un nom
nslookup google.com
# Avec un serveur DNS spécifique
nslookup google.com 8.8.8.8
# Requête inverse (IP vers nom)
nslookup 8.8.8.8
# Type d'enregistrement spécifique
nslookup -type=MX gmail.com
nslookup -type=TXT example.com
dig - Advanced DNS
# Installer si nécessaire
sudo apt install dnsutils
# Requête simple
dig google.com
# Réponse courte
dig +short google.com
# Enregistrement MX
dig MX gmail.com
# Tous les enregistrements
dig ANY example.com
# Serveur DNS spécifique
dig @8.8.8.8 google.com
# Trace de résolution
dig +trace google.com
# Requête inverse
dig -x 8.8.8.8
DNS record types
| Type | Description | Example |
|---|---|---|
| A | IPv4 | 142.250.74.238 |
| AAAA | IPv6 | 2a00:1450:4007:... |
| CNAME | Alias | www → example.com |
| MX | Mail server | mail.example.com |
| NS | Name server | ns1.example.com |
| TXT | Text | SPF, DKIM |
| PTR | Reverse | IP → name |
host - A simple alternative
# Résolution simple
host google.com
# Type spécifique
host -t MX gmail.com
🔝 Back to table of contents
4 - curl and wget
curl - Data transfer
# Récupérer une page
curl https://example.com
# Sauvegarder dans un fichier
curl -o page.html https://example.com
# Suivre les redirections
curl -L https://example.com
# Headers seulement
curl -I https://example.com
# Headers détaillés (requête + réponse)
curl -v https://example.com
# POST avec données
curl -X POST -d "user=john&pass=secret" https://api.example.com/login
# POST JSON
curl -X POST -H "Content-Type: application/json" \
-d '{"user":"john"}' https://api.example.com/users
# Avec authentification
curl -u user:password https://api.example.com
# Télécharger avec nom original
curl -O https://example.com/file.zip
# Timeout
curl --connect-timeout 5 --max-time 10 https://example.com
wget - Downloading
# Télécharger un fichier
wget https://example.com/file.zip
# Nom de sortie personnalisé
wget -O mon_fichier.zip https://example.com/file.zip
# Téléchargement en arrière-plan
wget -b https://example.com/gros_fichier.iso
# Reprendre un téléchargement interrompu
wget -c https://example.com/gros_fichier.iso
# Télécharger un site entier (mirroring)
wget -m -p -E -k https://example.com
# Limiter la bande passante
wget --limit-rate=500k https://example.com/file.zip
Comparison
| Aspect | curl | wget |
|---|---|---|
| Main use | APIs, tests | Downloading |
| Protocols | HTTP, FTP, SFTP, etc. | HTTP, FTP |
| Recursive | No | Yes |
| Resume | No | Yes (-c) |
| Default output | stdout | File |
🔝 Back to table of contents
5 - Introduction to tcpdump
What is tcpdump?
tcpdump captures and analyzes network traffic in real time.
Basic usage
# Capturer sur toutes les interfaces
sudo tcpdump
# Interface spécifique
sudo tcpdump -i eth0
# Limiter le nombre de paquets
sudo tcpdump -c 10
# Sauvegarder dans un fichier
sudo tcpdump -w capture.pcap
# Lire un fichier
tcpdump -r capture.pcap
Common filters
# Par hôte
sudo tcpdump host 192.168.1.100
# Par port
sudo tcpdump port 80
sudo tcpdump port 443
# Par protocole
sudo tcpdump tcp
sudo tcpdump udp
sudo tcpdump icmp
# Combinaisons
sudo tcpdump host 192.168.1.100 and port 22
sudo tcpdump 'tcp port 80 or tcp port 443'
# Réseau entier
sudo tcpdump net 192.168.1.0/24
Useful options
| Option | Description |
|---|---|
-i | Interface |
-c N | Capture N packets |
-w file | Write to a file |
-r file | Read from a file |
-n | No DNS resolution |
-v, -vv | Verbose |
-A | Display in ASCII |
-X | Display in hex and ASCII |
Practical example
# Voir le trafic HTTP
sudo tcpdump -i eth0 -A -s 0 'tcp port 80'
# Capturer pour analyse Wireshark
sudo tcpdump -i eth0 -w /tmp/capture.pcap
tip
The .pcap file can be opened with Wireshark for graphical analysis.
🔝 Back to table of contents
6 - Hands-on exercises
Exercise 1: Basic diagnostics
# 1. Testez la connectivité à Google
ping -c 4 google.com
# 2. Tracez le chemin
traceroute google.com
# 3. Résolvez le DNS
dig google.com +short
# 4. Vérifiez les ports en écoute
sudo ss -tulnp
Exercise 2: Use curl
# 1. Récupérez les headers d'un site
curl -I https://httpbin.org/get
# 2. Faites une requête GET
curl https://httpbin.org/get
# 3. Faites un POST JSON
curl -X POST \
-H "Content-Type: application/json" \
-d '{"name":"test"}' \
https://httpbin.org/post
Exercise 3: DNS
# 1. Trouvez les serveurs MX de gmail.com
dig MX gmail.com +short
# 2. Trouvez les serveurs de noms de google.com
dig NS google.com +short
# 3. Faites une résolution inverse
dig -x 8.8.8.8 +short
Quiz
Q1. Which command replaces netstat on modern systems?
Answer
ss (Socket Statistics)
Q2. How do you see only the listening ports with ss?
Answer
ss -l or, more complete, ss -tulnp
Q3. Which curl option follows redirects?
Answer
-L or --location
🔝 Back to table of contents
Key takeaways
pingtests connectivity,tracerouteshows the pathss -tulnp= listening ports (replaces netstat)digis more powerful thannslookupfor DNScurlfor APIs,wgetfor downloadstcpdumpto capture traffic (.pcap files)- Always use
-nto avoid slow DNS resolution
🔝 Back to table of contents
← Previous chapter | Next chapter: SSH - Secure connection →