简体   繁体   English

使用wget在Linux中检查两个服务器之间的连接时,如何获取确切的状态代码?

[英]How to get the exact status code when checking connectivity between two servers in Linux using wget?

We have new Linux DB servers. 我们有新的Linux DB服务器。 I am writing a script to check the connectivity from our servers to new DB servers. 我正在编写一个脚本来检查从我们的服务器到新数据库服务器的连接。 Based on the exit status code, I wrote my script to give an output if it is connected/connection refused or connection timed out. 根据退出状态代码,我编写了脚本以给出连接/连接被拒绝或连接超时的输出。

All our servers dont have telnet, so I have to check with wget . 我们所有的服务器都没有telnet,因此我必须使用wget进行检查。

Ny sample code to capture the status: Ny个示例代码捕获状态:

m=`echo $?`
if [ $m -eq 124 ]; then
b="Connection Timed Out"
else
if [ $m -eq 6 ]; then
b="Connected"
else
if [ $m -eq 0 ]; then
b="Connected"
else

if [ $m -eq 4 ]; then
b="Connection Refused"

But for new Linux DB servers, we are getting a different status code as 258, even though it is showing as connected: 但是对于新的Linux DB服务器,即使显示为已连接,我们也会得到一个不同的状态代码258。

--2019-09-18 09:53:14--  http://xxx:80/
Resolving xxxxx... xxx
Connecting to xxxx|xxxx|:80... connected.
HTTP request sent, awaiting response...  

echo $?
258

This is now misleading even though it has connectivity. 现在,即使具有连接性,这也会产生误导。 Kindly suggest how we can get the exact status. 请提出我们如何获得确切的身份。

You could use the HTTP status codes for checks. 您可以使用HTTP状态代码进行检查。

DB_STATUS_RESPONSE="$(wget --spider --server-response http://ipecho.net/plain 2>&1 | awk '/^  HTTP/{print $2}')"
if [ $DB_STATUS_RESPONSE -eq 200 ]; then b="Connected"; fi
$ echo $b
Connected

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Linux中使用curl分别获取http状态代码和内容 - How to get http status code and content separately using curl in linux 如何检查两台服务器之间的状态? - How to check the status between 2 servers? 两个Linux服务器之间的永久SFTP连接 - Permanent SFTP Connection between two linux servers 如何使用代码获取sas.servers的状态 - How to get the status of sas.servers using codes 如何在二进制模式下使用wget下载 - linux - how to download using wget in binary mode - linux 如何计算两个不同Linux服务器之间的系统时间偏移量? - How can I work out system time offset between two different linux servers? 两台主机但同一域上的Windows和Linux服务器之间的身份验证-可以吗? - Authentication between Windows and Linux servers on two hosts but same domain - possible? 使用 wget 获取某个 web 文件夹并将其保存到本地 linux 服务器 - using wget to get a certain web folder and save it to local linux server 如何获取Linux状态的颠覆 - How To Get Subversion on Linux Status 如何编写无服务器 AWS lambda function 将使用 wget 下载 linux 第三方应用程序,然后从该应用程序执行命令? - How to code a serverless AWS lambda function that will download a linux third party application using wget and then execute commands from that app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM