In operational practices, it is often necessary to retrieve the HTTP status codes of websites and web services from the command line. There are various tools available for this purpose, and here are several common methods:
Using the curl Command
<span>curl</span> is a powerful command-line tool used to send HTTP requests and obtain response information. The specific command to retrieve the status code is as follows:
bash
# curl -I -s -o /dev/null -w "%{http_code}" https://www.baidu.com
200
# curl -I -s -o /dev/null -w "%{http_code}" https://www.jd.com
200
# curl -I -s -o /dev/null -w "%{http_code}" https://www.baidu.com
200
### The status code for QQ.com is 501, possibly due to the website's security mechanism.
# curl -I -s -o /dev/null -w "%{http_code}" https://www.qq.com
501
Parameter Explanation:
- •
<span>-I</span>: Only display the HTTP header information without showing the response body, which reduces unnecessary data transfer. - •
<span>-s</span>: Silent mode, does not display curl’s progress information, making the output cleaner. - •
<span>-o /dev/null</span>: Discards the response body content by directing it to<span>/dev/null</span>, as we are only concerned with the status code. - •
<span>-w "%{http_code}"</span>: Specifies the output format, where<span>%{http_code}</span>indicates the output of the HTTP status code.
After executing this command, the terminal will directly output the HTTP status code, such as <span>200</span>, <span>404</span>, etc.
Using the wget Command
<span>wget</span> is primarily used for downloading files from the web, but it can also be used to obtain HTTP status codes. The command is as follows:bash
[root@dbcmaster ~]# wget --spider -O - https://www.jd.com 2>&1 | grep HTTP | awk '{print $6}'
200
[root@dbcmaster ~]# wget --spider -O - https://www.qq.com 2>&1 | grep HTTP | awk '{print $6}'
200
### The status code for 163.com is not 200, possibly due to the website's security mechanism.
[root@dbcmaster ~]# wget --spider -O - https://www.163.com 2>&1 | grep HTTP | awk '{print $6}'
403
Parameter Explanation:
- •
<span>--spider</span>: Instructs wget to run in “spider” mode, which checks if the target exists without downloading the file.
- •
<span>-O -</span>: Directs the output to standard output, where<span>-</span>indicates standard output. - •
<span>2>&1</span>: Redirects standard error output to standard output, ensuring that error messages (such as status codes) are captured. - •
<span>grep HTTP</span>: Filters the output to include only lines containing<span>HTTP</span>. - •
<span>awk '{print $6}'</span>: Uses the awk tool to extract the sixth field from that line, which is the HTTP status code.
Using the telnet Command (for simple connectivity checks and status code retrieval)
<span>telnet</span> is a tool for remote login and testing network connections. Although it cannot directly retrieve the complete HTTP status code, it can help determine if a connection to the specified HTTP service can be established and allows you to see part of the response information.
bash
telnet localhost 7067
GET /health HTTP/1.1
Host: localhost:7067
- 1. Execute
<span>telnet localhost 7067</span>to attempt to connect to the specified host and port. - 2. If the connection is successful, you will enter telnet interactive mode. At this point, manually input
<span>GET /health HTTP/1.1</span>and<span>Host: localhost:7067</span>, then press Enter twice to send the request. - 3. The server will return response information, and the HTTP status code can be seen in the first line of the response, for example,
<span>HTTP/1.1 200 OK</span>.
Using the httpie Command (requires prior installation)
<span>httpie</span> is a modern, user-friendly command-line HTTP client that provides more aesthetically pleasing and readable output.
bash
# http -h https://www.qq.com | grep HTTP | awk '{print $2}'
200
# http -h https://www.jd.com | grep HTTP | awk '{print $2}'
200
# http -h https://www.baidu.com | grep HTTP | awk '{print $2}'
200
# http -h https://www.163.com | grep HTTP | awk '{print $2}'
200
Parameter Explanation:
- •
<span>-h</span>: Specifies to print only the HTTP header information, which includes the HTTP status code.
All of these tools can help you retrieve HTTP status codes from the command line, and you can choose to use them based on your needs and preferences.