Created
November 18, 2020 07:12
-
-
Save mohno007/7a564631cbbc82ab6fb1f4e169f7362a to your computer and use it in GitHub Desktop.
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
#!/bin/sh | |
. ~/.bin/mylib.sh | |
require_commands netstat | |
IPV4ADDR_REGEX='((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' | |
IPV6ADDR_REGEX='((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){1}(((:[0-9a-f]{1,4}){1,6})|((:[0-9a-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9a-f]{1,4}){1,7})|((:[0-9a-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*' | |
print_line_help() { | |
cat <<-EOF | |
Usage: `basename "$0"` [OPTIONS] | |
Linux's ss like command implemeted with BSD netstat. | |
EOF | |
} | |
print_long_help() { | |
print_line_help | |
cat <<-EOF | |
EOF | |
} | |
with_lsof() { | |
echo "Proto Recv-Q Send-Q Local Address Foreign Address (state))" | |
if [ "$lsof" == "true" ]; then | |
# remove header lines | |
awk 'NR>2 { print }' | | |
while read line; do | |
proto="` | |
echo "$line" | | |
awk '{ print $1 }' | | |
sed -E 's/(tcp|udp)4?6?/\1/' | |
`" | |
version="` | |
echo "$line" | | |
awk '{ print $1 }' | | |
sed -E 's/(tcp|udp)(4?6?)/\2/' | | |
head -c1 | |
`" | |
addr="` | |
echo "$line" | | |
awk '{ print $4 }' | | |
# surround with []. eg. ::1 -> [::1] | |
sed -E 's/('"${IPV6ADDR_REGEX}"')\./@[\1]:/; s/('"${IPV4ADDR_REGEX}"')\./@\1:/; s/^\*\./:/; s/(.*)\.([0-9]+)$/@\1:\2/;' | |
`" | |
if echo "$proto" | grep -s "tcp|udp"; then | |
echo "$line" | |
continue; | |
fi | |
result="`lsof -i "$version$proto$addr" +c0 | awk 'NR>1{ print $1"("$2")" }'`" | |
echo "$line\t$result" | |
done | |
else | |
cat | |
fi | |
} | |
lsof="false" | |
options="" | |
sed="p;" | |
while getopts "46alnptu" opt; do | |
case "$opt" in | |
"4") | |
options="${options} -f inet" | |
;; | |
"6") | |
options="${options} -f inet6" | |
;; | |
"a") | |
options="${options} -a" | |
;; | |
"l") | |
options="${options} -a" | |
sed="${sed#"p;"} 1,2p; /LISTEN/p;" | |
;; | |
"n") | |
options="${options} -n" | |
;; | |
"p") | |
lsof="true" | |
;; | |
"t") | |
options="${options} -p tcp" | |
;; | |
"u") | |
options="${options} -p udp" | |
;; | |
*) | |
eecho "not supported; $opt" | |
print_line_help | |
exit 2 | |
;; | |
esac | |
done | |
netstat ${options} | sed -n "${sed}" | | |
with_lsof |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment