Add netstat-tulp.sh
This commit is contained in:
@@ -0,0 +1,98 @@
|
|||||||
|
#!/bin/ksh
|
||||||
|
|
||||||
|
# OpenBSD equivalent-ish to:
|
||||||
|
#
|
||||||
|
# Linux: netstat -tulpn
|
||||||
|
#
|
||||||
|
# Shows listening TCP sockets and UDP sockets together with
|
||||||
|
# the process using the socket when fstat can identify it.
|
||||||
|
#
|
||||||
|
# Best run as root for complete process information.
|
||||||
|
|
||||||
|
printf "%-5s %-7s %-8s %-22s %-22s %s\n" \
|
||||||
|
"PROTO" "PID" "USER" "LOCAL ADDRESS" "FOREIGN ADDRESS" "PROCESS"
|
||||||
|
|
||||||
|
printf "%-5s %-7s %-8s %-22s %-22s %s\n" \
|
||||||
|
"-----" "-------" "--------" "----------------------" \
|
||||||
|
"----------------------" "-------"
|
||||||
|
|
||||||
|
# ------------------------------------------------------------
|
||||||
|
# TCP listening sockets
|
||||||
|
# ------------------------------------------------------------
|
||||||
|
|
||||||
|
netstat -an -f inet 2>/dev/null |
|
||||||
|
awk '$1 ~ /^tcp/ && $6 == "LISTEN" {
|
||||||
|
print $1, $4, $5
|
||||||
|
}' |
|
||||||
|
while read proto local foreign
|
||||||
|
do
|
||||||
|
# Extract port from local address.
|
||||||
|
port="${local##*.}"
|
||||||
|
|
||||||
|
# Try to find the process owning this socket.
|
||||||
|
owner="$(
|
||||||
|
fstat 2>/dev/null |
|
||||||
|
awk -v port="$port" '
|
||||||
|
$0 ~ ("\\*:" port "([^0-9]|$)") ||
|
||||||
|
$0 ~ ("\\." port "([^0-9]|$)") {
|
||||||
|
print $1, $2, $3
|
||||||
|
exit
|
||||||
|
}
|
||||||
|
'
|
||||||
|
)"
|
||||||
|
|
||||||
|
if [ -n "$owner" ]; then
|
||||||
|
set -- $owner
|
||||||
|
|
||||||
|
user="$1"
|
||||||
|
process="$2"
|
||||||
|
pid="$3"
|
||||||
|
else
|
||||||
|
user="-"
|
||||||
|
process="-"
|
||||||
|
pid="-"
|
||||||
|
fi
|
||||||
|
|
||||||
|
printf "%-5s %-7s %-8s %-22s %-22s %s\n" \
|
||||||
|
"$proto" "$pid" "$user" "$local" "$foreign" "$process"
|
||||||
|
done
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------------------------------------------------
|
||||||
|
# UDP sockets
|
||||||
|
# ------------------------------------------------------------
|
||||||
|
|
||||||
|
netstat -an -f inet 2>/dev/null |
|
||||||
|
awk '$1 ~ /^udp/ {
|
||||||
|
print $1, $4, $5
|
||||||
|
}' |
|
||||||
|
while read proto local foreign
|
||||||
|
do
|
||||||
|
port="${local##*.}"
|
||||||
|
|
||||||
|
owner="$(
|
||||||
|
fstat 2>/dev/null |
|
||||||
|
awk -v port="$port" '
|
||||||
|
$0 ~ ("\\*:" port "([^0-9]|$)") ||
|
||||||
|
$0 ~ ("\\." port "([^0-9]|$)") {
|
||||||
|
print $1, $2, $3
|
||||||
|
exit
|
||||||
|
}
|
||||||
|
'
|
||||||
|
)"
|
||||||
|
|
||||||
|
if [ -n "$owner" ]; then
|
||||||
|
set -- $owner
|
||||||
|
|
||||||
|
user="$1"
|
||||||
|
process="$2"
|
||||||
|
pid="$3"
|
||||||
|
else
|
||||||
|
user="-"
|
||||||
|
process="-"
|
||||||
|
pid="-"
|
||||||
|
fi
|
||||||
|
|
||||||
|
printf "%-5s %-7s %-8s %-22s %-22s %s\n" \
|
||||||
|
"$proto" "$pid" "$user" "$local" "$foreign" "$process"
|
||||||
|
done
|
||||||
Reference in New Issue
Block a user