Created
February 18, 2026 18:28
-
-
Save toby-bro/0d724b1f25988edf6b9c157fc19de861 to your computer and use it in GitHub Desktop.
Get all the listening ports of a linux device in a nice interface
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 | |
| # Print the table header | |
| echo -e '╭─────────────────┬───────────────────────────┬─────────────────────┬──────┬──────┬──────╮' | |
| echo -e '│ Process │ Local Address │ Foreign Address │ Prot │ RcvQ │ SndQ │' | |
| echo -e '├─────────────────┼───────────────────────────┼─────────────────────┼──────┼──────┼──────┤' | |
| # Get the output of ss and process it | |
| sudo ss -lntup | awk ' | |
| function truncate(str, max_len) { | |
| if (length(str) > max_len) { | |
| return substr(str, 1, max_len-3) "..."; | |
| } | |
| return str; | |
| } | |
| NR==1 {next} # Skip the header line | |
| { | |
| # Extract process name | |
| match($0, /users:\(([^)]+)\)/, a); | |
| if (a[1]) { | |
| split(a[1], proc_array, ","); | |
| proc_name = proc_array[1]; | |
| gsub(/[()]/, "", proc_name); | |
| split(proc_name, name, "\""); | |
| process = name[2]; | |
| } else { | |
| process = "N/A"; | |
| } | |
| # Get the values and truncate if needed | |
| process = truncate(process, 15); | |
| local_addr = truncate($5, 25); | |
| foreign_addr = truncate($6, 19); | |
| protocol = truncate($1, 4); | |
| rcvq = truncate($3, 4); | |
| sndq = truncate($4, 4); | |
| printf "│ %-15s │ %-25s │ %-19s │ %-4s │ %-4s │ %-4s │\n", | |
| process, local_addr, foreign_addr, protocol, rcvq, sndq; | |
| } | |
| ' | grep -v 'firefox-esr' | sort -k1,1 | |
| # Print the table footer | |
| echo -e '╰─────────────────┴───────────────────────────┴─────────────────────┴──────┴──────┴──────╯' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment