Skip to content

Instantly share code, notes, and snippets.

@mohd-akram
Created May 10, 2024 16:56
Show Gist options
  • Save mohd-akram/ecf9b752e155c06d142312bbcad5f49d to your computer and use it in GitHub Desktop.
Save mohd-akram/ecf9b752e155c06d142312bbcad5f49d to your computer and use it in GitHub Desktop.
List open TCP and UDP ports
#!/bin/sh
# usage: lstcp [options] [port]
lstcp() (
while getopts : o; do opts="$opts -$OPTARG"; done; shift $((OPTIND-1))
lsof -i tcp"${1+:}$1" -s tcp:listen $opts
)
# usage: lsudp [options] [port]
lsudp() (
while getopts : o; do opts="$opts -$OPTARG"; done; shift $((OPTIND-1))
lsof -i udp"${1+:}$1" $opts | grep -vF -e '*:*' -e '->'
)
# usage: lsport [options] [port]
lsport() (
while getopts : o; do opts="$opts -$OPTARG"; done; shift $((OPTIND-1))
lsof -i"${1+:}$1" -s tcp:listen $opts | grep -vF -e '*:*' -e '->'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment