Script Refactoring

pull/2148/head
skiffer-git 2 years ago
parent 66a676fdf3
commit 4920b20d5a

@ -2832,42 +2832,44 @@ function openim::util::find_ports_for_all_services() {
check_binary_ports() { check_binary_ports() {
binary_path="$1" binary_path="$1"
binary_name=$(basename "$binary_path") binary_name=$(basename "$binary_path")
# Check if the binary is running # Check if the binary is running
if pgrep -f "$binary_path" > /dev/null; then if pgrep -f "$binary_path" > /dev/null; then
echo "$binary_name is running." echo "$binary_name is running."
# Find the PID(s) of the running binary # Find the PID(s) of the running binary
pids=$(pgrep -f "$binary_path") pids=$(pgrep -f "$binary_path")
# Initialize an empty string to store ports # Initialize an empty string to store ports
ports="" ports=""
# Loop through each PID # Loop through each PID
for pid in $pids; do for pid in $pids; do
# Check for listening ports using lsof # Check for listening ports using lsof
if [[ "$OSTYPE" == "linux-gnu"* ]]; then if [[ "$OSTYPE" == "linux-gnu"* ]]; then
# Linux # Linux
ports+=$(lsof -i -n | grep LISTEN | grep "$pid" | awk '{print $9}' | cut -d':' -f2 | uniq) # Append found ports to the ports variable, followed by a space
elif [[ "$OSTYPE" == "darwin"* ]]; then ports+=$(lsof -i -n | grep LISTEN | grep "$pid" | awk '{print $9}' | cut -d':' -f2 | uniq | tr '\n' ' ')
# macOS elif [[ "$OSTYPE" == "darwin"* ]]; then
ports+=$(lsof -i -n | grep LISTEN | grep "$pid" | awk '{print $9}' | awk -F'[:\.]+' '{print $(NF-1)}' | uniq) # macOS
fi # Append found ports to the ports variable, followed by a space
done ports+=$(lsof -i -n | grep LISTEN | grep "$pid" | awk '{print $9}' | awk -F'[:\.]+' '{print $(NF-1)}' | uniq | tr '\n' ' ')
fi
done
# Remove duplicate ports if any # Remove trailing spaces and duplicate ports if any
ports=$(echo "$ports" | uniq) ports=$(echo "$ports" | xargs | tr ' ' '\n' | uniq | tr '\n' ' ')
if [ -z "$ports" ]; then if [ -z "$ports" ]; then
echo "$binary_name is not listening on any ports." echo "$binary_name is not listening on any ports."
else else
echo "$binary_name is listening on the following ports: $ports" echo "$binary_name is listening on the following ports: $ports"
fi fi
else else
echo "$binary_name is not running." echo "$binary_name is not running."
fi fi
} }

Loading…
Cancel
Save