From b732b7468e07efd6bcd84cd32c81ad44726b6fed Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Thu, 21 Mar 2024 17:46:02 +0800 Subject: [PATCH] Optimizing scripts --- scripts/lib/util.sh | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index 75fe06cd0..22e5ef4a0 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -2846,22 +2846,58 @@ function openim::util::check_process_names_for_stop() { } -function openim::util::find_process_ports() { +function openim::util::find_process_ports2() { local process_path="$1" if [[ -z "$process_path" ]]; then echo "Usage: find_process_ports /path/to/process" return 1 fi - + local protocol_ports="" lsof -nP -iTCP -iUDP | grep LISTEN | grep "$(pgrep -f $process_path)" | awk '{print $9, $8}' | while read line; do local port_protocol=($line) local port=${port_protocol[0]##*:} local protocol=${port_protocol[1]} + protocol_ports= protocol_ports + $protocol + " "+$ports echo "Process $process_path is listening on port $port with protocol $protocol" done + + echo echo "Process $process_path is listening on protocol & port $protocol_ports " + } +function openim::util::find_process_ports() { + local process_path="$1" + if [[ -z "$process_path" ]]; then + echo "Usage: find_process_ports /path/to/process" + return 1 + fi + + local protocol_ports="" + local pids=$(pgrep -f "$process_path") + if [[ -z "$pids" ]]; then + echo "No process found for $process_path" + return 1 + fi + + lsof -nP -iTCP -iUDP | grep LISTEN | grep -E "$(echo $pids | sed 's/ /|/g')" | awk '{print $9, $8}' | while read -r port protocol; do + protocol_ports+="$protocol $port, " + echo "Process $process_path is listening on port ${port##*:} with protocol $protocol" + done + + + protocol_ports=${protocol_ports%, } + + if [[ -n "$protocol_ports" ]]; then + echo "Process $process_path is listening on protocol & port: $protocol_ports" + else + echo "Process $process_path is not listening on any port" + fi +} + + + + function openim::util::find_ports_for_all_services() {