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
for n in 1 2 3; do
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
pings+=("$ms")
echo -e " #$n: ${GREEN}${ms}ms${NC}"
@@ -290,7 +290,7 @@ probe_server_tg() {
local plost=0
for n in 1 2 3; do
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
pings+=("$ms")
result+=" #$n: ${ms}ms\n"
@@ -392,9 +392,9 @@ get_system_stats() {
get_rules_list() {
iptables -t nat -S PREROUTING 2>/dev/null | grep "DNAT" | while read -r line; do
local port proto dest
port=$(echo "$line" | grep -oP '(?<=--dport )\d+')
proto=$(echo "$line" | grep -oP '(?<=-p )\w+')
dest=$(echo "$line" | grep -oP '(?<=--to-destination )[\d.:]+')
port=$(echo "$line" | sed -n 's/.*--dport \([0-9]*\).*/\1/p')
proto=$(echo "$line" | sed -n 's/.*-p \([a-z]*\).*/\1/p')
dest=$(echo "$line" | sed -n 's/.*--to-destination \([0-9.:]*\).*/\1/p')
[ -n "$port" ] && echo "${proto}|${port}|${dest}"
done
}
@@ -405,15 +405,15 @@ get_target_ips() {
remove_rules_for_port() {
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
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
done
iptables -S FORWARD 2>/dev/null | grep "kaskad" | grep -P "\b-p ${proto}\b" | while read -r rule; do
local rd=$(echo "$rule" | grep -oP '(?<=--dport )\d+')
local rs=$(echo "$rule" | grep -oP '(?<=--sport )\d+')
iptables -S FORWARD 2>/dev/null | grep "kaskad" | grep -- "-p ${proto} " | while read -r rule; do
local rd; rd=$(echo "$rule" | sed -n 's/.*--dport \([0-9]*\).*/\1/p')
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
done
}
@@ -535,9 +535,10 @@ flush_rules() {
read -p "Уверены? (y/n): " confirm
if [[ "$confirm" == "y" ]]; 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
local pr; pr=$(iptables -S INPUT 2>/dev/null | grep "kaskad" | grep "\b${p}\b" | grep -oP '(?<=-p )\w+' | head -1)
[ -n "$pr" ] && ufw delete allow "$p/$pr" > /dev/null 2>&1
iptables -S INPUT 2>/dev/null | grep "kaskad" | while read -r ul; do
local up; up=$(echo "$ul" | sed -n 's/.*--dport \([0-9]*\).*/\1/p')
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
fi
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
echo -e " ${GREEN}${NC} Мониторинг остановлен"
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
local pr; pr=$(iptables -S INPUT 2>/dev/null | grep "kaskad" | grep "\b${p}\b" | grep -oP '(?<=-p )\w+' | head -1)
[ -n "$pr" ] && ufw delete allow "$p/$pr" > /dev/null 2>&1
iptables -S INPUT 2>/dev/null | grep "kaskad" | while read -r ul; do
local up; up=$(echo "$ul" | sed -n 's/.*--dport \([0-9]*\).*/\1/p')
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
echo -e " ${GREEN}${NC} Правила UFW очищены"
fi
@@ -717,7 +719,7 @@ ping_live() {
while [ "$running" -eq 1 ]; do
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++))
clear
@@ -883,7 +885,7 @@ monitor_daemon() {
[ -f "$ckf" ] && lc=$(cat "$ckf")
if (( now - lc >= MON_INTERVAL )); then
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
monitor_alert "$MON_IP" "TIMEOUT" "$MON_THRESHOLD" "$MON_COOLDOWN"
else
@@ -1202,7 +1204,7 @@ bot_handle_callback() {
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")" ;;
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 \
|| 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")
@@ -1210,7 +1212,7 @@ bot_handle_callback() {
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=$(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"; }
sleep 1
done
@@ -1224,7 +1226,7 @@ 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=$(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++))
if (( n % 10 == 0 )) && [ -n "$mid" ]; then
local p="🏓 <b>$lb</b>: ${n}/60с\nОК: ${#res[@]} | Lost: $lost"