check_port 370 B

1234567891011121314151617
  1. #!/bin/bash
  2. ##############
  3. # Returns the name of the service
  4. # running on a given port.
  5. ##############
  6. if [ ! "$UID" = 0 ]; then
  7. echo "Must be ran as root"
  8. exit 1
  9. fi
  10. if [ ! "$1" -gt 0 ] || [ ! "$1" -lt 65536 ]; then
  11. echo "Port number must be in range 1 - 65535"
  12. exit 2
  13. fi
  14. ss -tulpn | grep LISTEN | grep -E ":$1\b" | grep -Po "(?<=\(\(\").+?(?=\")" | uniq