Files
kaskad-pro/_test_tcp_ping.py
2026-03-07 15:08:58 +03:00

59 lines
1.7 KiB
Python

import paramiko
host = "185.250.47.205"
user = "root"
pwd = "0i1sbf9NM36FkG5dFH"
test_script = """#!/bin/bash
tcp_ping() {
local ip="$1" port="$2" tout="${3:-3}"
local raw
raw=$(curl -so /dev/null -w '%{time_connect}' --max-time "$tout" --connect-timeout "$tout" "http://${ip}:${port}/" 2>/dev/null)
[ -z "$raw" ] && return 1
local ms
ms=$(awk "BEGIN {v=$raw*1000; if(v<0.5) exit 1; printf \\"%.2f\\", v}" 2>/dev/null) || return 1
echo "$ms"
}
smart_ping() {
local ip="$1" tout="${2:-3}" port="${3:-}"
local ms
ms=$(ping -c 1 -W "$tout" "$ip" 2>/dev/null | sed -n 's/.*time=\\([0-9.]*\\).*/\\1/p')
if [ -n "$ms" ]; then echo "ICMP: ${ms}ms"; return 0; fi
[ -z "$port" ] && { echo "NO_PORT"; return 1; }
ms=$(tcp_ping "$ip" "$port" "$tout")
if [ -n "$ms" ]; then echo "TCP: ${ms}ms"; return 0; fi
echo "TIMEOUT"
return 1
}
echo "=== Test 1: ICMP to 8.8.8.8 ==="
smart_ping "8.8.8.8" 3
echo "=== Test 2: ICMP to 193.124.225.26 (no port) ==="
smart_ping "193.124.225.26" 3
echo "=== Test 3: smart_ping 193.124.225.26 TCP:443 ==="
smart_ping "193.124.225.26" 3 "443"
echo "=== Test 4: tcp_ping raw ==="
result=$(tcp_ping "193.124.225.26" "443" 3)
echo "result=$result rc=$?"
"""
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=user, password=pwd, timeout=10)
sftp = ssh.open_sftp()
with sftp.file("/tmp/_kaskad_test.sh", "w") as f:
f.write(test_script)
sftp.close()
stdin, stdout, stderr = ssh.exec_command("bash /tmp/_kaskad_test.sh", timeout=30)
print(stdout.read().decode(errors="replace"))
err = stderr.read().decode(errors="replace")
if err:
print("STDERR:", err)
ssh.close()