Check-host.net - проверяем пинг и ssh port через ноды

Материал из Wiki - Iphoster - the best ever hosting and support. 2005 - 2026
Перейти к:навигация, поиск

Check-host.net - проверяем пинг и ssh port через ноды

#!/bin/bash

read -p "Введите IP:порт (например XX.XX.XX.XX:22): " HOST

IP="${HOST%%:*}"
PORT="${HOST##*:}"

NODES="ru1.node.check-host.net&node=ru2.node.check-host.net&node=ru3.node.check-host.net"

TMP=$(mktemp)

########################################
# TCP
########################################

echo
echo "========== Проверка TCP (порт $PORT) =========="
echo "Проверяется доступность TCP-порта $PORT с российских нод Check-Host."

REQ_ID=$(curl -s -H "Accept: application/json" \
"https://check-host.net/check-tcp?host=$HOST&node=$NODES" \
| python3 -c "import sys,json;print(json.load(sys.stdin)['request_id'])")

echo "Request ID: $REQ_ID"

sleep 4

curl -s -H "Accept: application/json" \
"https://check-host.net/check-result/$REQ_ID" > "$TMP"

python3 - "$TMP" <<'PY'
import json,sys

with open(sys.argv[1]) as f:
    data=json.load(f)

print(f'{"Node":<30} {"Address":<20} {"Time":<10} Status')
print("-"*75)

for node,res in data.items():

    if res is None:
        print(f'{node:<30} {"-":<20} {"-":<10} PENDING')
        continue

    if isinstance(res,list) and res:
        r=res[0]

        if isinstance(r,dict) and "time" in r:
            print(f'{node:<30} {r["address"]:<20} {r["time"]:.3f}s   OK')
        else:
            err=r.get("error","unknown") if isinstance(r,dict) else str(r)
            print(f'{node:<30} {"-":<20} {"-":<10} NOT OK ({err})')
PY

########################################
# PING
########################################

echo
echo "========== Проверка Ping (ICMP) =========="
echo "Проверяется доступность сервера по ICMP (ping) с российских нод Check-Host."

PING_REQ=$(curl -s -H "Accept: application/json" \
"https://check-host.net/check-ping?host=$IP&node=$NODES" \
| python3 -c "import sys,json;print(json.load(sys.stdin)['request_id'])")

echo "Request ID: $PING_REQ"

# Ждем готовности результата (до 20 секунд)
for i in {1..20}; do
    curl -s -H "Accept: application/json" \
    "https://check-host.net/check-result/$PING_REQ" > "$TMP"

    if ! grep -q "null" "$TMP"; then
        break
    fi

    sleep 1
done

python3 - "$TMP" <<'PY'
import json,sys

with open(sys.argv[1]) as f:
    data=json.load(f)

print(f'{"Node":<30} {"Avg RTT":<12} Status')
print("-"*60)

for node,res in data.items():

    if res is None:
        print(f'{node:<30} {"-":<12} TIMEOUT')
        continue

    first=res[0]

    if isinstance(first,str):
        print(f'{node:<30} {"-":<12} {first}')
        continue

    if isinstance(first,list):
        rtts=[]

        for p in first:
            if isinstance(p,list) and len(p)>=2:
                try:
                    rtts.append(float(p[1]))
                except:
                    pass

        if rtts:
            avg=sum(rtts)/len(rtts)
            print(f'{node:<30} {avg:.2f} ms     OK')
        else:
            print(f'{node:<30} {"-":<12} NO REPLY')
    else:
        print(f'{node:<30} {"-":<12} UNKNOWN')
PY

rm -f "$TMP"
×
Реклама
ИКС