Last active
June 20, 2023 00:02
-
-
Save habibiefaried/3b3807b1d9849f93f8ca9ad1f6fab572 to your computer and use it in GitHub Desktop.
Check outgoing port
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
is_someprocess_connected() { | |
# Run netstat command and store the output | |
is_someprocess_connected_somewhere=0 | |
output=$(stdbuf -oL netstat -nputw 2>/dev/null) | |
port=$1 | |
# Process the output line by line | |
while IFS= read -r line; do | |
# Check if the line contains an active connection | |
if [[ $line =~ (tcp|udp)[[:space:]] ]]; then | |
# Extract the process name, PID, and port from the line | |
process_name=$(echo "$line" | awk '{print $7}') | |
dest=$(echo "$line" | awk '{print $5}') | |
ip_dest=$(echo "$dest" | awk -F':' '{print $1}') | |
port_dest=$(echo "$dest" | awk -F':' '{print $NF}') | |
# Print the process name, PID, and port | |
if [ "$port_dest" -eq "$port" ]; then | |
echo "Process $process_name is connected to IP $ip_dest port $port!" | |
is_someprocess_connected_somewhere=1 | |
fi | |
fi | |
done <<<"$output" | |
return $is_someprocess_connected_somewhere | |
} | |
is_someprocess_connected "443" | |
if (($? == 1)); then | |
exit 3 | |
else | |
echo "OK" | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment