mirror of
https://github.com/ivuorinen/dotfiles.git
synced 2026-02-03 16:48:40 +00:00
40 lines
854 B
Bash
Executable File
40 lines
854 B
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# List open (listened) ports, without the crud that
|
|
# usually comes with `lsof -i`
|
|
#
|
|
# Modified by: Ismo Vuorinen <https://github.com/ivuorinen> 2020
|
|
# Originally from: https://www.commandlinefu.com/commands/view/8951
|
|
# Original author: https://www.commandlinefu.com/commands/by/wickedcpj
|
|
|
|
set -euo pipefail
|
|
|
|
# Function to print the header
|
|
print_header()
|
|
{
|
|
echo 'User: Command: PID: Port:'
|
|
echo '========================================================='
|
|
}
|
|
|
|
# Function to list open ports
|
|
list_open_ports()
|
|
{
|
|
lsof -i 4 -P -n +c 0 \
|
|
| grep -i 'listen' \
|
|
| awk '{print $3, $1, $2, $9}' \
|
|
| sed 's/ [a-z0-9\.\*]*:/ /' \
|
|
| sort -k 3 -n \
|
|
| xargs printf '%-15s %-25s %-8s %-5s\n' \
|
|
| uniq
|
|
}
|
|
|
|
# Main function
|
|
main()
|
|
{
|
|
print_header
|
|
list_open_ports
|
|
echo ""
|
|
}
|
|
|
|
main "$@"
|