Comment tester la connectivité du serveur avec curl dans Linux

LinuxLinuxBeginner
Pratiquer maintenant

💡 Ce tutoriel est traduit par l'IA à partir de la version anglaise. Pour voir la version originale, vous pouvez cliquer ici

Introduction

In Linux system administration, checking server connectivity is a fundamental skill. This tutorial guides you through the process of testing server connectivity using the cURL tool. cURL (Client URL) is a command-line utility that enables data transfer across various network protocols, making it an essential tool for network diagnostics and troubleshooting.

By the end of this tutorial, you will understand how to use cURL to verify server availability, check response times, analyze HTTP status codes, and troubleshoot connection issues. These skills are valuable whether you are managing web servers, APIs, or any network services in your Linux environment.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux(("Linux")) -.-> linux/RemoteAccessandNetworkingGroup(["Remote Access and Networking"]) linux(("Linux")) -.-> linux/PackagesandSoftwaresGroup(["Packages and Softwares"]) linux(("Linux")) -.-> linux/VersionControlandTextEditorsGroup(["Version Control and Text Editors"]) linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/BasicFileOperationsGroup -.-> linux/chmod("Permission Modifying") linux/SystemInformationandMonitoringGroup -.-> linux/crontab("Job Scheduling") linux/RemoteAccessandNetworkingGroup -.-> linux/telnet("Network Connecting") linux/RemoteAccessandNetworkingGroup -.-> linux/nc("Networking Utility") linux/PackagesandSoftwaresGroup -.-> linux/curl("URL Data Transferring") linux/VersionControlandTextEditorsGroup -.-> linux/nano("Simple Text Editing") subgraph Lab Skills linux/ls -.-> lab-415082{{"Comment tester la connectivité du serveur avec curl dans Linux"}} linux/cat -.-> lab-415082{{"Comment tester la connectivité du serveur avec curl dans Linux"}} linux/chmod -.-> lab-415082{{"Comment tester la connectivité du serveur avec curl dans Linux"}} linux/crontab -.-> lab-415082{{"Comment tester la connectivité du serveur avec curl dans Linux"}} linux/telnet -.-> lab-415082{{"Comment tester la connectivité du serveur avec curl dans Linux"}} linux/nc -.-> lab-415082{{"Comment tester la connectivité du serveur avec curl dans Linux"}} linux/curl -.-> lab-415082{{"Comment tester la connectivité du serveur avec curl dans Linux"}} linux/nano -.-> lab-415082{{"Comment tester la connectivité du serveur avec curl dans Linux"}} end

Understanding cURL Basics

cURL is a powerful command-line tool that allows you to transfer data using various protocols, including HTTP, HTTPS, FTP, and many others. Before diving into connectivity testing, let us understand what cURL is and how to use it for basic operations.

Installing cURL

The cURL utility is pre-installed on most Linux distributions, including your Ubuntu 22.04 environment. To verify that cURL is installed, open your terminal and run:

curl --version

You should see output similar to this:

curl 7.81.0 (x86_64-pc-linux-gnu) libcurl/7.81.0 OpenSSL/3.0.2 zlib/1.2.11 brotli/1.0.9 zstd/1.4.8 libidn2/2.3.2 libpsl/0.21.0 (+libidn2/2.3.2) libssh/0.9.6/openssl/zlib nghttp2/1.43.0 librtmp/2.3 OpenLDAP/2.5.13
Release-Date: 2022-01-05
Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: alt-svc AsynchDNS brotli GSS-API HSTS HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM NTLM_WB PSL SPNEGO SSL TLS-SRP UnixSockets zstd

This confirms that cURL is installed and shows the version along with supported protocols and features.

Basic cURL Syntax

The basic syntax for using cURL is:

curl [options] [URL]

Let us try a simple cURL command to retrieve the content of a website:

curl https://example.com

This command sends a GET request to example.com and displays the HTML response in your terminal. You should see HTML code similar to the following:

<!doctype html>
<html>
  <head>
    <title>Example Domain</title>
    <!-- More HTML content -->
  </head>
  <body>
    <div>
      <h1>Example Domain</h1>
      <p>This domain is for use in illustrative examples in documents...</p>
      <!-- More content -->
    </div>
  </body>
</html>

Saving the Output to a File

Instead of displaying the output in the terminal, you can save it to a file using the -o or --output option:

curl -o example.html https://example.com

This command saves the response from example.com to a file named example.html. To verify that the file was created:

ls -l example.html

You should see output confirming the file exists:

-rw-rw-r-- 1 labex labex 1256 Mar 28 12:34 example.html

To view the contents of the file:

cat example.html

You should see the same HTML content that was previously displayed in the terminal.

Understanding HTTP Methods with cURL

cURL uses the HTTP GET method by default, but you can specify other methods using the -X option. The common HTTP methods include:

  • GET: Retrieve data from a server
  • POST: Submit data to a server
  • PUT: Update existing data on a server
  • DELETE: Remove data from a server
  • HEAD: Similar to GET but retrieves only headers

In later steps, we will explore how to use these different methods to test server connectivity and functionality.

Testing Basic Server Connectivity

Now that you understand the basics of cURL, let us use it to test server connectivity. The ability to check if a server is up and responding correctly is a crucial skill for system administrators and developers.

Simple Connection Test

The most basic connectivity test is to send a request to a server and see if it responds. Let us test connectivity to Google's servers:

curl -I https://www.google.com

The -I option (or --head) tells cURL to send a HEAD request, which only retrieves the headers without the body content. This is useful for quick connectivity checks. You should see output similar to:

HTTP/2 200
content-type: text/html; charset=ISO-8859-1
date: Tue, 28 Mar 2023 12:34:56 GMT
server: gws
content-length: 219
x-xss-protection: 0
x-frame-options: SAMEORIGIN

The HTTP/2 200 indicates a successful connection - the server is up and responding.

Checking HTTP Status Codes

HTTP status codes are standardized responses that servers send to indicate the result of a client request. Some common status codes include:

  • 200: OK - The request was successful
  • 301/302: Redirect - The resource has moved
  • 404: Not Found - The resource does not exist
  • 500: Internal Server Error - Server encountered an error

Let us test a non-existent URL to see a 404 response:

curl -I https://www.google.com/nonexistent-page

The output should include a 404 status code:

HTTP/2 404
content-type: text/html; charset=UTF-8
date: Tue, 28 Mar 2023 12:35:01 GMT
server: gws
content-length: 1565
...

Measuring Response Time

To measure how long it takes for a server to respond, use the -w option with a format string:

curl -s -o /dev/null -w "Connect: %{time_connect}s\nTotal: %{time_total}s\n" https://www.google.com

This command:

  • -s: Operates in silent mode (no progress or error messages)
  • -o /dev/null: Redirects the output to /dev/null (discards it)
  • -w "...": Displays formatted output with timing information

You should see output similar to:

Connect: 0.052s
Total: 0.157s

This tells you how long it took to establish a connection and the total time for the request to complete.

Testing Domain Name Resolution

Sometimes connectivity issues stem from DNS problems. To test if a domain name can be resolved to an IP address:

curl -v https://www.example.com 2>&1 | grep "Trying"

This uses the -v (verbose) option and filters for the "Trying" line, which shows the IP address being connected to. You should see output like:

* Trying 93.184.216.34:443...

This confirms that the domain name was successfully resolved to an IP address.

Creating a Simple Connection Test Script

Let us create a simple shell script to test connectivity to multiple sites. Open a text editor:

nano connection_test.sh

Add the following content to the file:

#!/bin/bash

echo "Testing server connectivity..."

for site in google.com example.com github.com nonexistent-site.xyz; do
  echo -n "Testing $site: "

  ## Use curl with a 5-second timeout
  status_code=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 "https://$site" 2> /dev/null)

  if [ $? -eq 0 ] && [ "$status_code" -lt 400 ]; then
    echo "OK (Status: $status_code)"
  else
    echo "Failed (Status: $status_code)"
  fi
done

echo "Testing complete!"

Save the file by pressing Ctrl+O, then Enter, and exit with Ctrl+X.

Make the script executable:

chmod +x connection_test.sh

Run the script:

./connection_test.sh

You should see output showing the connectivity status of each site:

Testing server connectivity...
Testing google.com: OK (Status: 200)
Testing example.com: OK (Status: 200)
Testing github.com: OK (Status: 200)
Testing nonexistent-site.xyz: Failed (Status: 000)
Testing complete!

This script provides a quick way to check connectivity to multiple servers at once.

Advanced Connectivity Testing with cURL

Now that you understand basic connectivity testing, let us explore more advanced features of cURL that can help with detailed troubleshooting and testing.

Using Verbose Mode for Detailed Debugging

The verbose mode (-v option) is invaluable for troubleshooting connectivity issues as it shows the entire request and response process:

curl -v https://example.com

The output will be comprehensive, showing the DNS resolution, TLS handshake, request headers, response headers, and more:

*   Trying 93.184.216.34:443...
* Connected to example.com (93.184.216.34) port 443 (#0)
* ALPN: offers h2
* ALPN: offers http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN: server accepted h2
* Server certificate:
*  subject: C=US; ST=California; L=Los Angeles; O=Internet Corporation for Assigned Names and Numbers; CN=www.example.org
*  start date: Nov 24 00:00:00 2022 GMT
*  expire date: Nov 24 23:59:59 2023 GMT
*  subjectAltName: host "example.com" matched cert's "example.com"
*  issuer: C=US; O=DigiCert Inc; CN=DigiCert TLS RSA SHA256 2020 CA1
*  SSL certificate verify ok.
* using HTTP/2
* h2 [:method: GET]
* h2 [:path: /]
* h2 [:scheme: https]
* h2 [:authority: example.com]
* h2 [user-agent: curl/7.81.0]
* h2 [accept: */*]
* Using Stream ID: 1
> GET / HTTP/2
> Host: example.com
> user-agent: curl/7.81.0
> accept: */*
>
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* Connection state changed (MAX_CONCURRENT_STREAMS == 100)!
< HTTP/2 200
< age: 587269
< cache-control: max-age=604800
< content-type: text/html; charset=UTF-8
< date: Tue, 28 Mar 2023 12:40:01 GMT
< etag: "3147526947+ident"
< expires: Tue, 04 Apr 2023 12:40:01 GMT
< last-modified: Thu, 17 Oct 2019 07:18:26 GMT
< server: ECS (nyb/1D2B)
< vary: Accept-Encoding
< x-cache: HIT
< content-length: 1256
<
<!doctype html>
<html>
<head>
    <title>Example Domain</title>
    <!-- More HTML content -->
</head>
<body>
    <div>
        <h1>Example Domain</h1>
        <p>This domain is for use in illustrative examples in documents...</p>
        <!-- More content -->
    </div>
</body>
</html>
* Connection #0 to host example.com left intact

This detailed output helps you identify where exactly a connection might be failing.

Testing Different HTTP Methods

Let us test a POST request to a test API endpoint:

curl -X POST -d "name=test&[email protected]" https://httpbin.org/post

This command:

  • -X POST: Specifies a POST request
  • -d "name=test&[email protected]": Sends form data in the request

You should receive a JSON response showing your submitted data:

{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "email": "[email protected]",
    "name": "test"
  },
  "headers": {
    "Accept": "*/*",
    "Content-Length": "32",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.81.0",
    "X-Amzn-Trace-Id": "Root=1-642295b1-0d2340ef34f2e8ea6270241a"
  },
  "json": null,
  "origin": "198.51.100.42",
  "url": "https://httpbin.org/post"
}

Testing with Custom Headers

Many APIs require specific headers for authentication or to specify the content type. Let us test this:

curl -H "User-Agent: MyCustomAgent" -H "Authorization: Bearer test-token" https://httpbin.org/headers

This command:

  • -H "User-Agent: MyCustomAgent": Sets a custom User-Agent header
  • -H "Authorization: Bearer test-token": Sets an Authorization header

The response will show the headers sent in your request:

{
  "headers": {
    "Accept": "*/*",
    "Authorization": "Bearer test-token",
    "Host": "httpbin.org",
    "User-Agent": "MyCustomAgent",
    "X-Amzn-Trace-Id": "Root=1-642295c3-73cac0a73b34b1c93a8ce520"
  }
}

Testing Response Times for Different Endpoints

Let us create a script to compare response times for different servers:

nano response_time.sh

Add the following content:

#!/bin/bash

echo "Testing response times..."

for site in google.com bing.com baidu.com duckduckgo.com yahoo.com; do
  echo -n "$site: "
  curl -s -o /dev/null -w "%{time_total}s" "https://$site"
  echo ""
done

echo "Testing complete!"

Save the file and make it executable:

chmod +x response_time.sh

Run the script:

./response_time.sh

The output will show the response time for each site:

Testing response times...
google.com: 0.187s
bing.com: 0.232s
baidu.com: 0.412s
duckduckgo.com: 0.298s
yahoo.com: 0.342s
Testing complete!

This is useful for comparing the performance of different servers or monitoring a server's performance over time.

Testing TCP Connectivity to Specific Ports

Sometimes you need to test if a specific port is open on a server. cURL can be used for this as well:

curl -v telnet://example.com:80

If the port is open, you will see a successful connection message:

* Trying 93.184.216.34:80...
* Connected to example.com (93.184.216.34) port 80 (#0)

Press Ctrl+C to terminate the connection.

Similarly, you can test secure connections:

curl -v https://example.com:443

The verbose output will show if the connection was successful or if there were any issues.

Creating a Comprehensive Server Monitoring Tool

Now that you have learned various cURL techniques for connectivity testing, let us build a more comprehensive server monitoring tool that combines these techniques.

Comprehensive Server Monitoring Script

Create a new script file:

nano server_monitor.sh

Add the following content:

#!/bin/bash

## Server Monitoring Script
## This script checks the availability and performance of specified servers

## Define colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
NC='\033[0m' ## No Color

## Function to check server status
check_server() {
  local url=$1
  local timeout=5

  echo -e "\n${YELLOW}Testing $url:${NC}"

  ## Test connection and get status code
  status_code=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout $timeout "$url" 2> /dev/null)

  if [ "$status_code" -eq 200 ]; then
    echo -e "${GREEN}✓ Status: $status_code (OK)${NC}"
  elif [ "$status_code" -ge 100 ] && [ "$status_code" -lt 400 ]; then
    echo -e "${GREEN}✓ Status: $status_code (Success/Redirect)${NC}"
  elif [ "$status_code" -ge 400 ] && [ "$status_code" -lt 500 ]; then
    echo -e "${RED}✗ Status: $status_code (Client Error)${NC}"
  elif [ "$status_code" -ge 500 ]; then
    echo -e "${RED}✗ Status: $status_code (Server Error)${NC}"
  else
    echo -e "${RED}✗ Status: Connection failed${NC}"
  fi

  ## Measure response time if connection successful
  if [ "$status_code" -gt 0 ]; then
    response_time=$(curl -s -o /dev/null -w "%{time_total}" --connect-timeout $timeout "$url" 2> /dev/null)
    echo -e "• Response time: ${response_time}s"

    ## Get server headers
    echo "• Server headers:"
    curl -s -I --connect-timeout $timeout "$url" | grep -E 'Server:|Content-Type:|Date:' | sed 's/^/  /'
  fi
}

## Main function
main() {
  echo -e "${YELLOW}===== Server Connectivity Monitor =====${NC}"
  echo "Started at: $(date)"

  ## List of servers to monitor
  servers=(
    "https://www.google.com"
    "https://www.github.com"
    "https://www.example.com"
    "https://httpbin.org/status/404" ## This will return a 404 status
    "https://httpbin.org/status/500" ## This will return a 500 status
  )

  ## Check each server
  for server in "${servers[@]}"; do
    check_server "$server"
  done

  echo -e "\n${YELLOW}===== Monitoring Complete =====${NC}"
  echo "Finished at: $(date)"
}

## Run the main function
main

Save the file and make it executable:

chmod +x server_monitor.sh

Run the script:

./server_monitor.sh

The output will provide a comprehensive status overview of each server:

===== Server Connectivity Monitor =====
Started at: Tue Mar 28 13:15:01 UTC 2023

Testing https://www.google.com:
✓ Status: 200 (OK)
• Response time: 0.186s
• Server headers:
  Date: Tue, 28 Mar 2023 13:15:02 GMT
  Content-Type: text/html; charset=ISO-8859-1
  Server: gws

Testing https://www.github.com:
✓ Status: 200 (OK)
• Response time: 0.247s
• Server headers:
  Server: GitHub.com
  Date: Tue, 28 Mar 2023 13:15:02 GMT
  Content-Type: text/html; charset=utf-8

Testing https://www.example.com:
✓ Status: 200 (OK)
• Response time: 0.132s
• Server headers:
  Content-Type: text/html; charset=UTF-8
  Date: Tue, 28 Mar 2023 13:15:03 GMT
  Server: ECS (nyb/1D2B)

Testing https://httpbin.org/status/404:
✗ Status: 404 (Client Error)
• Response time: 0.189s
• Server headers:
  Date: Tue, 28 Mar 2023 13:15:03 GMT
  Content-Type: text/html; charset=utf-8
  Server: gunicorn/19.9.0

Testing https://httpbin.org/status/500:
✗ Status: 500 (Server Error)
• Response time: 0.192s
• Server headers:
  Date: Tue, 28 Mar 2023 13:15:03 GMT
  Content-Type: text/html; charset=utf-8
  Server: gunicorn/19.9.0

===== Monitoring Complete =====
Finished at: Tue Mar 28 13:15:04 UTC 2023

Scheduling Regular Connectivity Checks

To monitor servers regularly, you could set up a cron job. In a real production environment, you might add this script to crontab to run at regular intervals. For demonstration purposes, let us create a simple wrapper script that runs the monitoring every minute for a specified duration:

nano scheduled_monitor.sh

Add the following content:

#!/bin/bash

## Scheduled monitoring script
## This script runs the server_monitor.sh at regular intervals

## Check if duration parameter is provided
if [ $## -ne 1 ]; then
  echo "Usage: $0 <duration_in_minutes>"
  exit 1
fi

duration=$1
interval=60 ## seconds
iterations=$((duration * 60 / interval))

echo "Starting scheduled monitoring for $duration minutes..."
echo "Press Ctrl+C to stop monitoring"

for ((i = 1; i <= iterations; i++)); do
  echo -e "\n===== Run $i of $iterations ====="
  ./server_monitor.sh

  ## Don't sleep after the last iteration
  if [ $i -lt $iterations ]; then
    echo "Next check in $interval seconds..."
    sleep $interval
  fi
done

echo "Scheduled monitoring completed."

Save the file and make it executable:

chmod +x scheduled_monitor.sh

Run the script for 2 minutes (you can increase or decrease as needed):

./scheduled_monitor.sh 2

This will run the server monitoring script every minute for 2 minutes:

Starting scheduled monitoring for 2 minutes...
Press Ctrl+C to stop monitoring

===== Run 1 of 2 =====
===== Server Connectivity Monitor =====
...
(monitoring output)
...
Next check in 60 seconds...
(waits for 60 seconds)

===== Run 2 of 2 =====
===== Server Connectivity Monitor =====
...
(monitoring output)
...
Scheduled monitoring completed.

In a production environment, you would typically set up a cron job instead, but this script provides a simple way to perform scheduled monitoring during this lab exercise.

Summary

In this lab, you have explored how to use cURL for testing server connectivity in a Linux environment. Starting with the basics, you learned how to send simple HTTP requests and save responses to files. You then advanced to more complex operations, including checking HTTP status codes, measuring response times, and using verbose mode for detailed debugging.

You have created several practical scripts that demonstrate the power of cURL for server monitoring and connectivity testing:

  1. A basic connection test script that checks connectivity to multiple servers
  2. A response time comparison script to measure and compare server performance
  3. A comprehensive server monitoring tool that provides detailed information about server status, response times, and header information
  4. A scheduled monitoring script that automates regular connectivity checks

These tools and techniques are invaluable for system administrators, developers, and anyone working with networked systems. By mastering cURL, you now have a powerful tool in your arsenal for diagnosing and resolving connectivity issues in your Linux environment.

As you continue to work with Linux systems, remember that cURL is not only useful for testing connectivity but also for interacting with APIs, downloading files, and automating various network-related tasks. The skills you have gained in this lab will serve as a foundation for more advanced network operations and troubleshooting in the future.