Browse Source

v1.4: - hide multiple socket listing on reused Ports (SO_REUSEPORT)
- new Argument -s

boson 1 year ago
parent
commit
6c968c4174
2 changed files with 44 additions and 17 deletions
  1. 9 5
      README.md
  2. 35 12
      sinetstat

+ 9 - 5
README.md

@@ -8,10 +8,12 @@ Improvements to the original netstat command:
 * *explore IPv4 in IPv6 listening sockets (IPv4-mapped IPv6 addresses)*
 * *shows real command and arguments*
 * *shows the UID of the command*
-
+* *hide multiple entries on reused (SO_REUSEPORT) ports*
   
   
 This is a Python 2.x/3.x script.  
+Maybe the shebang line needs to be adjusted, depending on the Python installation of your system.
+
 For a full featured output the script needs root-privileges.  
 
 
@@ -19,14 +21,16 @@ For a full featured output the script needs root-privileges.
 Simply try:  
 <small><pre>
  # sinetstat -h
-usage: sinetstat [-h] [-l] [-e] [-r] [-w] [-W] [-t] [-u] [-4] [-6]
-netstat utility V1.2
-2017-2019 by sigi <https://wiki.zweiernet.ch/wiki/sinetstat>
-.
+usage: sinetstat [-h] [-l] [-e] [-s] [-r] [-w] [-W] [-t] [-u] [-4] [-6]
+
+netstat utility V1.4
+2017-2022 by sigi <https://wiki.zweiernet.ch/wiki/sinetstat>
+
 optional arguments:
   -h, --help  show this help message and exit
   -l          Only listening sockets
   -e          Only established sockets
+  -s          Show all sockets on reused ports
   -r          Resolve IP-Addresses
   -w          Wide (show cmd)
   -W          Wider (show cmd with arguments)

+ 35 - 12
sinetstat

@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 
-# (c) 2016-2019 by Siegrist(SystemLoesungen) <PSS @ ZweierNet.ch> 
+# (c) 2016-2022 by Siegrist(SystemLoesungen) <PSS @ ZweierNet.ch> 
 # Website: [https://wiki.zweiernet.ch/wiki/Sinetstat]
 # 
 # This program is free software under the terms of the GNU General Public License. 
@@ -28,7 +28,7 @@ import argparse
 
 
 
-VERSION = '1.3.1'
+VERSION = '1.4'
 
 PROC_TCP4 = "/proc/net/tcp"
 PROC_UDP4 = "/proc/net/udp"
@@ -226,11 +226,16 @@ def netstat_tcp4():
         
         nline = '%-7s %-24s  %-24s  %-11s  %-8s  %-6s  %-s' % ('TCP4', l_host+': '+l_port, r_host+': '+r_port, state, uid, pid, exe)
         
-        tcpresult.append(nline)
-        
-        # update v4inv6check list
-        v4ports.append(l_port)
-        
+        if o_reused == False:
+        	if nline not in tcpresult:				# Hide multi binds on same Socket (SO_REUSEPORT)
+        		tcpresult.append(nline)
+        		# update v4inv6check list
+        		v4ports.append(l_port)
+        else:
+        	tcpresult.append(nline)
+        	# update v4inv6check list
+        	v4ports.append(l_port)
+        	
     return tcpresult
 
 def netstat_tcp6():
@@ -274,8 +279,12 @@ def netstat_tcp6():
 
         nline = '%-7s %-24s  %-24s  %-11s  %-8s  %-6s  %-s' % ('TCP6', _compress_v6(_convert_ipv6(line_array[1]))+': '+l_port, _compress_v6(_convert_ipv6(line_array[2]))+': '+r_port, state, uid, pid, exe)
         
-        tcpresult.append(nline)
-        	
+        if o_reused == False:
+        	if nline not in tcpresult:				# Hide multi binds on same Socket (SO_REUSEPORT)
+        		tcpresult.append(nline)
+        else:
+        	tcpresult.append(nline)
+        		
     return tcpresult
 
 
@@ -381,7 +390,12 @@ def netstat_udp4():
 
         nline = '%-7s %-24s  %-24s  %-11s  %-8s  %-6s  %-s' % ('UDP4', l_host+': '+l_port, r_host+': '+r_port, udp_state, uid, pid, exe)
         
-        udpresult.append(nline)
+        if o_reused == False:
+        	if nline not in udpresult:				# Hide multi binds on same Socket (SO_REUSEPORT)
+        		udpresult.append(nline)
+        else:
+        	udpresult.append(nline)
+        	
     return udpresult
 
 def netstat_udp6():
@@ -425,7 +439,12 @@ def netstat_udp6():
 
         nline = '%-7s %-24s  %-24s  %-11s  %-8s  %-6s  %-s' % ('UDP6', _compress_v6(_convert_ipv6(line_array[1]))+': '+l_port, _compress_v6(_convert_ipv6(line_array[2]))+': '+r_port, udp_state, uid, pid, exe)
         
-        udpresult.append(nline)
+        if o_reused == False:
+        	if nline not in udpresult:				# Hide multi binds on same Socket (SO_REUSEPORT)
+        		udpresult.append(nline)
+        else:
+        	udpresult.append(nline)
+        	
     return udpresult
 
 
@@ -491,10 +510,12 @@ if __name__ == '__main__':
     o_tcp = True
     o_v6 = True
     o_v4 = True
-    parser = argparse.ArgumentParser(description='netstat utility V'+VERSION+"\n2017-2019 by sigi <https://wiki.zweiernet.ch/wiki/sinetstat>",
+    o_reused = False
+    parser = argparse.ArgumentParser(description='netstat utility V'+VERSION+"\n2017-2022 by sigi <https://wiki.zweiernet.ch/wiki/sinetstat>",
                                    formatter_class=argparse.RawDescriptionHelpFormatter )
     parser.add_argument('-l', help="Only listening sockets", action="store_true")
     parser.add_argument('-e', help="Only established sockets", action="store_true")
+    parser.add_argument('-s', help="Show all sockets on reused ports", action="store_true")
     parser.add_argument('-r', help="Resolve IP-Addresses", action="store_true")
     parser.add_argument('-w', help="Wide (show cmd)", action="store_true")
     parser.add_argument('-W', help="Wider (show cmd with arguments)", action="store_true")
@@ -523,6 +544,8 @@ if __name__ == '__main__':
         o_v6 = False
     if args.v6:
         o_v4 = False
+    if args.s:
+        o_reused = True
     
     
     # Output