Linux

有多少服務在所有介面上監聽目標系統?(僅在 localhost 和 IPv4 上除外)

  • May 7, 2021

我需要找出有多少服務正在監聽我的介面(僅限 ipv4,而不是 localhost)

$ ifconfig

ens192: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
       inet 10.129.56.137  netmask 255.255.0.0  broadcast 10.129.255.255
       inet6 dead:beef::250:56ff:feb9:8c07  prefixlen 64  scopeid 0x0<global>
       inet6 fe80::250:56ff:feb9:8c07  prefixlen 64  scopeid 0x20<link>
       ether 00:50:56:b9:8c:07  txqueuelen 1000  (Ethernet)
       RX packets 3644  bytes 330312 (330.3 KB)
       RX errors 0  dropped 0  overruns 0  frame 0
       TX packets 3198  bytes 679711 (679.7 KB)
       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
       inet 127.0.0.1  netmask 255.0.0.0
       inet6 ::1  prefixlen 128  scopeid 0x10<host>
       loop  txqueuelen 1000  (Local Loopback)
       RX packets 15304  bytes 895847 (895.8 KB)
       RX errors 0  dropped 0  overruns 0  frame 0
       TX packets 15304  bytes 895847 (895.8 KB)
       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

$ nmap 10.129.56.137

Starting Nmap 7.60 ( https://nmap.org ) at 2020-12-05 05:23 UTC
Nmap scan report for 10.129.56.137
Host is up (0.000086s latency).
Not shown: 991 closed ports
PORT    STATE SERVICE
21/tcp  open  ftp
22/tcp  open  ssh
80/tcp  open  http
110/tcp open  pop3
139/tcp open  netbios-ssn
143/tcp open  imap
445/tcp open  microsoft-ds
993/tcp open  imaps
995/tcp open  pop3s

Nmap done: 1 IP address (1 host up) scanned in 10.57 seconds

我以為答案是 9,但必須有辦法找到正確答案。提前乾杯!

netstat -tunleep4 | grep -v "127\.0\.0"

來自 man netstat:

該程序大多已過時。netstat 的替代品是 ss。

在這一點上,我認為這將是最好的選擇:

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

在哪裡:

  • -l : 只顯示監聽服務
  • -4 : 只顯示 ipv4
  • -grep -v “127.0.0”:排除所有本地主機結果
  • -grep “LISTEN” : 更好地過濾監聽服務
  • wc -l : 統計結果

引用自:https://unix.stackexchange.com/questions/623001