Created
August 19, 2023 08:42
-
-
Save LouDnl/64a812283eb47cf91a8613ff8666eda8 to your computer and use it in GitHub Desktop.
Script for displaying listening and established connections using ss and their command lines using ps
This file contains 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
#!/usr/bin/env bash | |
## | |
# Script for displaying listening and established connections using ss | |
# Prints information and complete command lines using ps in table form output | |
# NOTE: Can get messy output with long command lines | |
# | |
# LouD 2023-09-2023 | |
## Example output: | |
# STATE LOCAL PEER TYPE PID COMMAND | |
# LISTEN 0.0.0.0:2022 0.0.0.0:* tcp 601 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups | |
# ESTAB 192.168.200.160:3333 192.168.200.150:52711 tcp 1586 sshd: loud@pts/0 | |
# LISTEN [::]:2022 [::]:* tcp 601 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups | |
# start ss command with sudo | |
sudo ss -anptu state established state connected state listening |\ | |
# remove unneeded and error causing states | |
egrep -v "TIME-WAIT|SYN-RECV|SYN-SENT|FIN-WAIT-1|FIN-WAIT-2|%lo" |\ | |
# take what we need and reformat it, add the command lines | |
awk 'NR>1 { split($7,pids,",") ; split(pids[2], pid, "=") ; printf $2 "^" $5 "^" $6 "^" $1 "^" pid[2] "^" ; system("ps -o args p " pid[2] "| sed -n '2p'"); }' |\ | |
# sort in reverse so that listening is on top | |
sort -r |\ | |
# pretty print in columns | |
column --separator "^" --table --table-columns STATE,LOCAL,PEER,TYPE,PID,COMMAND ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment