How many services are listening on the target system on all interfaces? (Not on localhost and IPv4 only)

From man netstat:

This program is mostly obsolete. Replacement for netstat is ss.

At this point, I think this will be the best option:

ss -l -4 | grep -v "127\.0\.0" | grep "LISTEN" | wc -l

Where:

  • -l: show only listening services
  • -4: show only ipv4
  • -grep -v "127.0.0": exclude all localhost results
  • -grep "LISTEN": better filtering only listening services
  • wc -l: count results