Fix: replace all grep -oP/-P (PCRE) with portable sed — fixes ping TIMEOUT on servers without libpcre

Made-with: Cursor
This commit is contained in:
anten-ka
2026-03-07 15:00:02 +03:00
parent 3dd10c679b
commit ee13a9e512

View File

@@ -228,7 +228,7 @@ probe_server_cli() {
local plost=0 local plost=0
for n in 1 2 3; do for n in 1 2 3; do
local ms local ms
ms=$(ping -c 1 -W 3 "$ip" 2>/dev/null | grep -oP 'time=\K[\d.]+') ms=$(ping -c 1 -W 3 "$ip" 2>/dev/null | sed -n 's/.*time=\([0-9.]*\).*/\1/p')
if [ -n "$ms" ]; then if [ -n "$ms" ]; then
pings+=("$ms") pings+=("$ms")
echo -e " #$n: ${GREEN}${ms}ms${NC}" echo -e " #$n: ${GREEN}${ms}ms${NC}"
@@ -290,7 +290,7 @@ probe_server_tg() {
local plost=0 local plost=0
for n in 1 2 3; do for n in 1 2 3; do
local ms local ms
ms=$(ping -c 1 -W 3 "$ip" 2>/dev/null | grep -oP 'time=\K[\d.]+') ms=$(ping -c 1 -W 3 "$ip" 2>/dev/null | sed -n 's/.*time=\([0-9.]*\).*/\1/p')
if [ -n "$ms" ]; then if [ -n "$ms" ]; then
pings+=("$ms") pings+=("$ms")
result+=" #$n: ${ms}ms\n" result+=" #$n: ${ms}ms\n"
@@ -392,9 +392,9 @@ get_system_stats() {
get_rules_list() { get_rules_list() {
iptables -t nat -S PREROUTING 2>/dev/null | grep "DNAT" | while read -r line; do iptables -t nat -S PREROUTING 2>/dev/null | grep "DNAT" | while read -r line; do
local port proto dest local port proto dest
port=$(echo "$line" | grep -oP '(?<=--dport )\d+') port=$(echo "$line" | sed -n 's/.*--dport \([0-9]*\).*/\1/p')
proto=$(echo "$line" | grep -oP '(?<=-p )\w+') proto=$(echo "$line" | sed -n 's/.*-p \([a-z]*\).*/\1/p')
dest=$(echo "$line" | grep -oP '(?<=--to-destination )[\d.:]+') dest=$(echo "$line" | sed -n 's/.*--to-destination \([0-9.:]*\).*/\1/p')
[ -n "$port" ] && echo "${proto}|${port}|${dest}" [ -n "$port" ] && echo "${proto}|${port}|${dest}"
done done
} }
@@ -405,15 +405,15 @@ get_target_ips() {
remove_rules_for_port() { remove_rules_for_port() {
local proto="$1" in_port="$2" local proto="$1" in_port="$2"
iptables -t nat -S PREROUTING 2>/dev/null | grep "DNAT" | grep -P "\b--dport ${in_port}\b" | grep -P "\b-p ${proto}\b" | while read -r rule; do iptables -t nat -S PREROUTING 2>/dev/null | grep "DNAT" | grep -- "--dport ${in_port} " | grep -- "-p ${proto} " | while read -r rule; do
eval "iptables -t nat -D ${rule#-A }" 2>/dev/null eval "iptables -t nat -D ${rule#-A }" 2>/dev/null
done done
iptables -S INPUT 2>/dev/null | grep "kaskad" | grep -P "\b--dport ${in_port}\b" | grep -P "\b-p ${proto}\b" | while read -r rule; do iptables -S INPUT 2>/dev/null | grep "kaskad" | grep -- "--dport ${in_port} " | grep -- "-p ${proto} " | while read -r rule; do
eval "iptables -D ${rule#-A }" 2>/dev/null eval "iptables -D ${rule#-A }" 2>/dev/null
done done
iptables -S FORWARD 2>/dev/null | grep "kaskad" | grep -P "\b-p ${proto}\b" | while read -r rule; do iptables -S FORWARD 2>/dev/null | grep "kaskad" | grep -- "-p ${proto} " | while read -r rule; do
local rd=$(echo "$rule" | grep -oP '(?<=--dport )\d+') local rd; rd=$(echo "$rule" | sed -n 's/.*--dport \([0-9]*\).*/\1/p')
local rs=$(echo "$rule" | grep -oP '(?<=--sport )\d+') local rs; rs=$(echo "$rule" | sed -n 's/.*--sport \([0-9]*\).*/\1/p')
[[ "$rd" == "$in_port" || "$rs" == "$in_port" ]] && eval "iptables -D ${rule#-A }" 2>/dev/null [[ "$rd" == "$in_port" || "$rs" == "$in_port" ]] && eval "iptables -D ${rule#-A }" 2>/dev/null
done done
} }
@@ -535,9 +535,10 @@ flush_rules() {
read -p "Уверены? (y/n): " confirm read -p "Уверены? (y/n): " confirm
if [[ "$confirm" == "y" ]]; then if [[ "$confirm" == "y" ]]; then
if command -v ufw &>/dev/null && ufw status 2>/dev/null | grep -q "Status: active"; then if command -v ufw &>/dev/null && ufw status 2>/dev/null | grep -q "Status: active"; then
iptables -S INPUT 2>/dev/null | grep "kaskad" | grep -oP '(?<=--dport )\d+' | while read -r p; do iptables -S INPUT 2>/dev/null | grep "kaskad" | while read -r ul; do
local pr; pr=$(iptables -S INPUT 2>/dev/null | grep "kaskad" | grep "\b${p}\b" | grep -oP '(?<=-p )\w+' | head -1) local up; up=$(echo "$ul" | sed -n 's/.*--dport \([0-9]*\).*/\1/p')
[ -n "$pr" ] && ufw delete allow "$p/$pr" > /dev/null 2>&1 local upr; upr=$(echo "$ul" | sed -n 's/.*-p \([a-z]*\).*/\1/p')
[ -n "$up" ] && [ -n "$upr" ] && ufw delete allow "$up/$upr" > /dev/null 2>&1
done done
fi fi
while iptables -t nat -S PREROUTING 2>/dev/null | grep -q "DNAT"; do while iptables -t nat -S PREROUTING 2>/dev/null | grep -q "DNAT"; do
@@ -602,9 +603,10 @@ full_uninstall() {
systemctl daemon-reload 2>/dev/null systemctl daemon-reload 2>/dev/null
echo -e " ${GREEN}${NC} Мониторинг остановлен" echo -e " ${GREEN}${NC} Мониторинг остановлен"
if command -v ufw &>/dev/null && ufw status 2>/dev/null | grep -q "Status: active"; then if command -v ufw &>/dev/null && ufw status 2>/dev/null | grep -q "Status: active"; then
iptables -S INPUT 2>/dev/null | grep "kaskad" | grep -oP '(?<=--dport )\d+' | while read -r p; do iptables -S INPUT 2>/dev/null | grep "kaskad" | while read -r ul; do
local pr; pr=$(iptables -S INPUT 2>/dev/null | grep "kaskad" | grep "\b${p}\b" | grep -oP '(?<=-p )\w+' | head -1) local up; up=$(echo "$ul" | sed -n 's/.*--dport \([0-9]*\).*/\1/p')
[ -n "$pr" ] && ufw delete allow "$p/$pr" > /dev/null 2>&1 local upr; upr=$(echo "$ul" | sed -n 's/.*-p \([a-z]*\).*/\1/p')
[ -n "$up" ] && [ -n "$upr" ] && ufw delete allow "$up/$upr" > /dev/null 2>&1
done done
echo -e " ${GREEN}${NC} Правила UFW очищены" echo -e " ${GREEN}${NC} Правила UFW очищены"
fi fi
@@ -717,7 +719,7 @@ ping_live() {
while [ "$running" -eq 1 ]; do while [ "$running" -eq 1 ]; do
local ms local ms
ms=$(ping -c 1 -W 2 "$ip" 2>/dev/null | grep -oP 'time=\K[\d.]+') ms=$(ping -c 1 -W 3 "$ip" 2>/dev/null | sed -n 's/.*time=\([0-9.]*\).*/\1/p')
((count++)) ((count++))
clear clear
@@ -883,7 +885,7 @@ monitor_daemon() {
[ -f "$ckf" ] && lc=$(cat "$ckf") [ -f "$ckf" ] && lc=$(cat "$ckf")
if (( now - lc >= MON_INTERVAL )); then if (( now - lc >= MON_INTERVAL )); then
echo "$now" > "$ckf" echo "$now" > "$ckf"
local pr; pr=$(ping -c 1 -W 3 "$MON_IP" 2>/dev/null | grep -oP 'time=\K[\d.]+') local pr; pr=$(ping -c 1 -W 3 "$MON_IP" 2>/dev/null | sed -n 's/.*time=\([0-9.]*\).*/\1/p')
if [ -z "$pr" ]; then if [ -z "$pr" ]; then
monitor_alert "$MON_IP" "TIMEOUT" "$MON_THRESHOLD" "$MON_COOLDOWN" monitor_alert "$MON_IP" "TIMEOUT" "$MON_THRESHOLD" "$MON_COOLDOWN"
else else
@@ -1202,7 +1204,7 @@ bot_handle_callback() {
ps:*) local ip="${data#ps:}"; local lb; lb=$(fmt_ip_short "$ip") ps:*) local ip="${data#ps:}"; local lb; lb=$(fmt_ip_short "$ip")
tg_edit "$chat_id" "$msg_id" "🏓 <b>$lb</b>\nРежим:" "$(kbd_ping_opts "$ip")" ;; tg_edit "$chat_id" "$msg_id" "🏓 <b>$lb</b>\nРежим:" "$(kbd_ping_opts "$ip")" ;;
po:*) local ip="${data#po:}"; local lb; lb=$(fmt_ip_short "$ip") po:*) local ip="${data#po:}"; local lb; lb=$(fmt_ip_short "$ip")
( local ms; ms=$(ping -c 1 -W 3 "$ip" 2>/dev/null | grep -oP 'time=\K[\d.]+') ( local ms; ms=$(ping -c 1 -W 3 "$ip" 2>/dev/null | sed -n 's/.*time=\([0-9.]*\).*/\1/p')
[ -n "$ms" ] && tg_send "$chat_id" "🏓 <b>$lb</b>\n<code>${ms} ms</code>" "$(kbd_back)" > /dev/null \ [ -n "$ms" ] && tg_send "$chat_id" "🏓 <b>$lb</b>\n<code>${ms} ms</code>" "$(kbd_back)" > /dev/null \
|| tg_send "$chat_id" "🏓 <b>$lb</b>\n<code>timeout</code>" "$(kbd_back)" > /dev/null ) & ;; || tg_send "$chat_id" "🏓 <b>$lb</b>\n<code>timeout</code>" "$(kbd_back)" > /dev/null ) & ;;
p10:*) local ip="${data#p10:}"; local lb; lb=$(fmt_ip_short "$ip") p10:*) local ip="${data#p10:}"; local lb; lb=$(fmt_ip_short "$ip")
@@ -1210,7 +1212,7 @@ bot_handle_callback() {
local mid; mid=$(echo "$resp" | jq -r '.result.message_id // empty') local mid; mid=$(echo "$resp" | jq -r '.result.message_id // empty')
local -a res=(); local lost=0 txt="" local -a res=(); local lost=0 txt=""
for n in $(seq 1 10); do for n in $(seq 1 10); do
local ms; ms=$(ping -c 1 -W 2 "$ip" 2>/dev/null | grep -oP 'time=\K[\d.]+') local ms; ms=$(ping -c 1 -W 3 "$ip" 2>/dev/null | sed -n 's/.*time=\([0-9.]*\).*/\1/p')
[ -n "$ms" ] && res+=("$ms") && txt+="#$n: ${ms}ms\n" || { ((lost++)); txt+="#$n: timeout\n"; } [ -n "$ms" ] && res+=("$ms") && txt+="#$n: ${ms}ms\n" || { ((lost++)); txt+="#$n: timeout\n"; }
sleep 1 sleep 1
done done
@@ -1224,7 +1226,7 @@ bot_handle_callback() {
local mid; mid=$(echo "$resp" | jq -r '.result.message_id // empty') local mid; mid=$(echo "$resp" | jq -r '.result.message_id // empty')
local -a res=(); local lost=0 local -a res=(); local lost=0
for n in $(seq 1 60); do for n in $(seq 1 60); do
local ms; ms=$(ping -c 1 -W 2 "$ip" 2>/dev/null | grep -oP 'time=\K[\d.]+') local ms; ms=$(ping -c 1 -W 3 "$ip" 2>/dev/null | sed -n 's/.*time=\([0-9.]*\).*/\1/p')
[ -n "$ms" ] && res+=("$ms") || ((lost++)) [ -n "$ms" ] && res+=("$ms") || ((lost++))
if (( n % 10 == 0 )) && [ -n "$mid" ]; then if (( n % 10 == 0 )) && [ -n "$mid" ]; then
local p="🏓 <b>$lb</b>: ${n}/60с\nОК: ${#res[@]} | Lost: $lost" local p="🏓 <b>$lb</b>: ${n}/60с\nОК: ${#res[@]} | Lost: $lost"