-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathnetstat_cmdline
More file actions
executable file
·45 lines (40 loc) · 1.37 KB
/
netstat_cmdline
File metadata and controls
executable file
·45 lines (40 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash
#
# based on https://superuser.com/a/1079629
# consult that URL for a list of limitation
# that this script inherits
#
# I'm at home at https://github.com/tpo/little_shell_scripts
help() {
echo 'usage: netstat_cmdline [netstat_options]*'
echo ' netstat_cmdline --help'
echo
echo ' netstat_cmdline will call netstat and replace'
echo ' the program name with the full command line'
echo
echo ' See `netstat --help` for all possible options'
echo
echo ' Note: netstat_cmdline works, but is slow and is'
echo " a hack (see its source code). It'd be"
echo ' better to add a respective option to'
echo ' netstat itself. Or to use `ss`, whose'
echo ' output seems to be more robust to parse.'
echo
exit 1
}
[ "$1" == "--help" ] && help
set -e # stop on error
set -u # stop on undefined variable
set -o pipefail # stop part of pipeline failing
netstat "$@" -p | while IPS= read -r line;
do
pid="$( sed -En 's/^.*( [0-9]+)\/[^ ]*.*/\1/gp' <<< "$line" )"
if [ "$pid" != "" ]; then
cmdline="$(ps -p $pid -o cmd=)"
line_start="$( sed -En 's/^(.* [0-9]+)\/[^ ]*.*/\1/gp' <<< "$line" )"
line_end="$( sed -En 's/^.* [0-9]+\/[^ ]*(.*)/\1/gp' <<< "$line" )"
echo "$line_start#$cmdline $line_end"
else
echo "$line"
fi
done