From 3f82c7ad10aa8bacf3599045f15672357520fa57 Mon Sep 17 00:00:00 2001 From: anten-ka Date: Sat, 7 Mar 2026 15:42:49 +0300 Subject: [PATCH] Fix smart_ping subshell: return method|ms, update all callers, iptables ICMP hint Made-with: Cursor --- install.sh | 66 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/install.sh b/install.sh index 963df8e..38645c7 100644 --- a/install.sh +++ b/install.sh @@ -227,14 +227,15 @@ probe_server_cli() { local -a pings=() local plost=0 for n in 1 2 3; do - local ms - ms=$(smart_ping "$ip" 3 "$port") - if [ -n "$ms" ]; then + local raw method="" ms="" + raw=$(smart_ping "$ip" 3 "$port") + if [ -n "$raw" ]; then + method="${raw%%|*}"; ms="${raw#*|}" pings+=("$ms") - echo -e " #$n: ${GREEN}${ms}ms${NC} ${CYAN}[${_PING_METHOD}]${NC}" + echo -e " #$n: ${GREEN}${ms}ms${NC} ${CYAN}[$method]${NC}" else ((plost++)) - echo -e " #$n: ${RED}timeout${NC} ${WHITE}[ICMP fail$([ -n "$port" ] && echo ", TCP:$port fail")]${NC}" + echo -e " #$n: ${RED}timeout${NC} ${WHITE}[ICMP$([ -n "$port" ] && echo "+TCP:$port")]${NC}" fi [ "$n" -lt 3 ] && sleep 1 done @@ -297,11 +298,12 @@ probe_server_tg() { local -a pings=() local plost=0 for n in 1 2 3; do - local ms - ms=$(smart_ping "$ip" 3 "$port") - if [ -n "$ms" ]; then + local raw method="" ms="" + raw=$(smart_ping "$ip" 3 "$port") + if [ -n "$raw" ]; then + method="${raw%%|*}"; ms="${raw#*|}" pings+=("$ms") - result+=" #$n: ${ms}ms [${_PING_METHOD}]\n" + result+=" #$n: ${ms}ms [$method]\n" else ((plost++)) result+=" #$n: timeout\n" @@ -428,18 +430,15 @@ tcp_ping() { echo "$ms" } -_PING_METHOD="" - smart_ping() { local ip="$1" tout="${2:-3}" port="${3:-}" - _PING_METHOD="" local ms ms=$(ping -c 1 -W "$tout" "$ip" 2>/dev/null | sed -n 's/.*time=\([0-9.]*\).*/\1/p') - if [ -n "$ms" ]; then _PING_METHOD="ICMP"; echo "$ms"; return 0; fi + if [ -n "$ms" ]; then echo "ICMP|$ms"; return 0; fi [ -z "$port" ] && port=$(get_port_for_ip "$ip") [ -z "$port" ] && return 1 ms=$(tcp_ping "$ip" "$port" "$tout") - if [ -n "$ms" ]; then _PING_METHOD="TCP:${port}"; echo "$ms"; return 0; fi + if [ -n "$ms" ]; then echo "TCP:${port}|$ms"; return 0; fi return 1 } @@ -784,8 +783,9 @@ ping_live() { [ -n "$_port" ] && _mode="ICMP/TCP:${_port}" while [ "$running" -eq 1 ]; do - local ms - ms=$(smart_ping "$ip" 3 "${_port:-}") + local raw method="" ms="" + raw=$(smart_ping "$ip" 3 "${_port:-}") + if [ -n "$raw" ]; then method="${raw%%|*}"; ms="${raw#*|}"; fi ((count++)) clear @@ -797,7 +797,7 @@ ping_live() { results+=("$ms") local bar bar=$(make_ping_bar "$ms") - printf " ${GREEN}#%-4d %7sms${NC} ${CYAN}[%s]${NC} %b\n" "$count" "$ms" "$_PING_METHOD" "$bar" + printf " ${GREEN}#%-4d %7sms${NC} ${CYAN}[%s]${NC} %b\n" "$count" "$ms" "$method" "$bar" else ((lost++)) printf " ${RED}#%-4d ------${NC} " "$count" @@ -839,12 +839,13 @@ ping_live() { echo "" if [ ${#results[@]} -eq 0 ] && [ "$count" -gt 0 ]; then echo -e "${YELLOW}━━━ Сервер не ответил ни разу ━━━${NC}" - echo -e "${WHITE}ICMP заблокирован, а TCP-соединение не удалось.${NC}" + echo -e "${WHITE}ICMP заблокирован$([ -n "$_port" ] && echo " и TCP:$_port не удался").${NC}" echo "" echo -e "${CYAN}Чтобы включить ping на удалённом сервере:${NC}" echo -e " ${WHITE}ssh root@${ip}${NC}" echo -e " ${GREEN}sysctl -w net.ipv4.icmp_echo_ignore_all=0${NC}" echo -e " ${GREEN}echo 'net.ipv4.icmp_echo_ignore_all=0' >> /etc/sysctl.conf${NC}" + echo -e " ${GREEN}iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT${NC}" echo "" fi read -p "Нажмите Enter для возврата в меню..." @@ -962,8 +963,9 @@ monitor_daemon() { [ -f "$ckf" ] && lc=$(cat "$ckf") if (( now - lc >= MON_INTERVAL )); then echo "$now" > "$ckf" - local pr; pr=$(smart_ping "$MON_IP" 3) - if [ -z "$pr" ]; then + local pr_raw; pr_raw=$(smart_ping "$MON_IP" 3) + local pr="${pr_raw#*|}" + if [ -z "$pr" ] || [ -z "$pr_raw" ]; then monitor_alert "$MON_IP" "TIMEOUT" "$MON_THRESHOLD" "$MON_COOLDOWN" else local pi; pi=$(awk "BEGIN {printf \"%d\", $pr + 0.5}") @@ -1281,24 +1283,29 @@ bot_handle_callback() { ps:*) local ip="${data#ps:}"; local lb; lb=$(fmt_ip_short "$ip") tg_edit "$chat_id" "$msg_id" "🏓 $lb\nРежим:" "$(kbd_ping_opts "$ip")" ;; po:*) local ip="${data#po:}"; local lb; lb=$(fmt_ip_short "$ip") - ( local ms; ms=$(smart_ping "$ip" 3) - if [ -n "$ms" ]; then - tg_send "$chat_id" "🏓 $lb\n${ms} ms" "$(kbd_back)" > /dev/null + ( local raw; raw=$(smart_ping "$ip" 3) + if [ -n "$raw" ]; then + local mtd="${raw%%|*}" pms="${raw#*|}" + tg_send "$chat_id" "🏓 $lb\n${pms} ms [$mtd]" "$(kbd_back)" > /dev/null else - tg_send "$chat_id" "🏓 $lb\ntimeout\n\nICMP заблокирован на сервере.\nВключить:\nsysctl -w net.ipv4.icmp_echo_ignore_all=0" "$(kbd_back)" > /dev/null + tg_send "$chat_id" "🏓 $lb\ntimeout\n\nСервер не ответил.\nВключите ping:\nsysctl -w net.ipv4.icmp_echo_ignore_all=0\niptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT" "$(kbd_back)" > /dev/null fi ) & ;; p10:*) local ip="${data#p10:}"; local lb; lb=$(fmt_ip_short "$ip") ( local resp; resp=$(tg_send "$chat_id" "🏓 $lb (10x)..." "") local mid; mid=$(echo "$resp" | jq -r '.result.message_id // empty') local -a res=(); local lost=0 txt="" for n in $(seq 1 10); do - local ms; ms=$(smart_ping "$ip" 3) - [ -n "$ms" ] && res+=("$ms") && txt+="#$n: ${ms}ms\n" || { ((lost++)); txt+="#$n: timeout\n"; } + local raw; raw=$(smart_ping "$ip" 3) + if [ -n "$raw" ]; then + local mtd="${raw%%|*}" pms="${raw#*|}" + res+=("$pms"); txt+="#$n: ${pms}ms [$mtd]\n" + else ((lost++)); txt+="#$n: timeout\n"; fi sleep 1 done local sm="🏓 $lb (10x)\n${txt}" [ ${#res[@]} -gt 0 ] && { local av; av=$(printf '%s\n' "${res[@]}" | awk '{s+=$1} END {printf "%.2f",s/NR}'); sm+="\nСреднее: ${av}ms"; } sm+="\nПотеряно: $lost/10" + if [ ${#res[@]} -eq 0 ]; then sm+="\n\nВключите ping:\nsysctl -w net.ipv4.icmp_echo_ignore_all=0"; fi [ -n "$mid" ] && tg_edit "$chat_id" "$mid" "$sm" "$(kbd_back)" > /dev/null || tg_send "$chat_id" "$sm" "$(kbd_back)" > /dev/null ) & ;; p60:*) local ip="${data#p60:}"; local lb; lb=$(fmt_ip_short "$ip") @@ -1306,8 +1313,10 @@ bot_handle_callback() { local mid; mid=$(echo "$resp" | jq -r '.result.message_id // empty') local -a res=(); local lost=0 for n in $(seq 1 60); do - local ms; ms=$(smart_ping "$ip" 3) - [ -n "$ms" ] && res+=("$ms") || ((lost++)) + local raw; raw=$(smart_ping "$ip" 3) + if [ -n "$raw" ]; then + local pms="${raw#*|}"; res+=("$pms") + else ((lost++)); fi if (( n % 10 == 0 )) && [ -n "$mid" ]; then local p="🏓 $lb: ${n}/60с\nОК: ${#res[@]} | Lost: $lost" [ ${#res[@]} -gt 0 ] && { local pa; pa=$(printf '%s\n' "${res[@]}" | awk '{s+=$1} END {printf "%.2f",s/NR}'); p+="\nСред: ${pa}ms"; } @@ -1319,6 +1328,7 @@ bot_handle_callback() { local st; st=$(printf '%s\n' "${res[@]}" | awk 'BEGIN{mn=999999;mx=0;s=0}{s+=$1;if($1mx)mx=$1}END{printf "%.2f|%.2f|%.2f",mn,mx,s/NR}') IFS='|' read -r sn sx sa <<< "$st"; sm+="Мин: ${sn}ms\nМакс: ${sx}ms\nСред: ${sa}ms\n" fi; sm+="Потеряно: $lost/60" + if [ ${#res[@]} -eq 0 ]; then sm+="\n\nВключите ping:\nsysctl -w net.ipv4.icmp_echo_ignore_all=0"; fi [ -n "$mid" ] && tg_edit "$chat_id" "$mid" "$sm" "$(kbd_back)" > /dev/null || tg_send "$chat_id" "$sm" "$(kbd_back)" > /dev/null ) & ;;