Parcourir la source

v1.6.1 new param -D: show DNS payload
fastening Inode lookup

sigi il y a 3 jours
Parent
commit
751591c4e5
2 fichiers modifiés avec 46 ajouts et 38 suppressions
  1. 4 4
      README.md
  2. 42 34
      sinetstat

+ 4 - 4
README.md

@@ -8,7 +8,7 @@ It can display all TCP/UDP network sockets including (hidden) IPv4-mapped addres
 
 __Improvements to the original netstat command:__ <br>
  \- *explore IPv4 in IPv6 listening/established sockets (IPv4-mapped IPv6 addresses)*<br>
- \- *prints full output of command including its arguments*<br>
+ \- *complete output of initial command including its arguments*<br>
  \- *shows UID and PID of the command*<br>
  \- *shorten output by hiding repeated entries on reused (SO_REUSEPORT) ports*<br> 
  
@@ -22,7 +22,7 @@ Maybe the shebang line needs to be adjusted, depending on the Python installatio
 
 Direct Download: `wget https://git.zweiernet.ch/sigi/sinetstat/raw/master/sinetstat`
   
-
+Recent Version is 1.4.3
 
 Then simply try:  
 <small>
@@ -30,8 +30,8 @@ Then simply try:
 # sinetstat -h
 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>
+netstat utility V1.4.3
+2017-2025 by sigi <https://wiki.zweiernet.ch/wiki/sinetstat>
 
 optional arguments:
   -h, --help  show this help message and exit

+ 42 - 34
sinetstat

@@ -28,7 +28,7 @@ import argparse
 
 
 
-VERSION = '1.4.2'
+VERSION = '1.4.3'
 
 PROC_TCP4 = "/proc/net/tcp"
 PROC_UDP4 = "/proc/net/udp"
@@ -57,7 +57,8 @@ opt_l = True
 
 
 def grep_b(list, search):
-    return [True for i in list if search in i]
+    if (search in list):
+        return True
     
 def get_ip_address(ifname):
     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@@ -112,8 +113,8 @@ def _ip6q(s):
 
 def _conv_v6(s):
     return s
-	
-	
+    
+    
 
 def _remove_empty(array):
     return [x for x in array if x !='']
@@ -227,15 +228,15 @@ def netstat_tcp4():
         nline = '%-7s %-24s  %-24s  %-11s  %-8s  %-6s  %-s' % ('TCP4', l_host+': '+l_port, r_host+': '+r_port, state, uid, pid, exe)
         
         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)
+            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)
-        	
+            tcpresult.append(nline)
+            # update v4inv6check list
+            v4ports.append(l_port)
+            
     return tcpresult
 
 def netstat_tcp6():
@@ -280,11 +281,11 @@ 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)
         
         if o_reused == False:
-        	if nline not in tcpresult:				# Hide multi binds on same Socket (SO_REUSEPORT)
-        		tcpresult.append(nline)
+            if nline not in tcpresult:              # Hide multi binds on same Socket (SO_REUSEPORT)
+                tcpresult.append(nline)
         else:
-        	tcpresult.append(nline)
-        		
+            tcpresult.append(nline)
+                
     return tcpresult
 
 
@@ -299,7 +300,7 @@ def netstat_tcp4in6():
     for line in tcpcontent:
         line_array = _remove_empty(line.split(' '))
         #if TCP_STATE[line_array[3]] != 'LISTEN':
-        #	continue
+        #   continue
         
         l_host,l_port = _convert_ipv6_port(line_array[1])
         r_host,r_port = _convert_ipv6_port(line_array[2])
@@ -335,13 +336,13 @@ def netstat_tcp4in6():
             exe = '-'
             
         if l_host == '00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01':
-        	if _check_v4inv6_port("127.0.0.1",l_port):
-        	    nline = '%-7s %-24s  %-24s  %-11s  %-8s  %-6s  %-s' % ('TCP4in6', '127.0.0.1: '+l_port, _compress_v6(_convert_ipv6(line_array[2]))+': '+r_port, state, uid, pid, exe)
-        	    tcpresult.append(nline)
+            if _check_v4inv6_port("127.0.0.1",l_port):
+                nline = '%-7s %-24s  %-24s  %-11s  %-8s  %-6s  %-s' % ('TCP4in6', '127.0.0.1: '+l_port, _compress_v6(_convert_ipv6(line_array[2]))+': '+r_port, state, uid, pid, exe)
+                tcpresult.append(nline)
         if l_host == "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00":
-        	if _check_v4inv6_port("0.0.0.0",l_port):
-        	    nline = '%-7s %-24s  %-24s  %-11s  %-8s  %-6s  %-s' % ('TCP4in6', '0.0.0.0: '+l_port, _compress_v6(_convert_ipv6(line_array[2]))+': '+r_port, state, uid, pid, exe)
-        	    tcpresult.append(nline)
+            if _check_v4inv6_port("0.0.0.0",l_port):
+                nline = '%-7s %-24s  %-24s  %-11s  %-8s  %-6s  %-s' % ('TCP4in6', '0.0.0.0: '+l_port, _compress_v6(_convert_ipv6(line_array[2]))+': '+r_port, state, uid, pid, exe)
+                tcpresult.append(nline)
         #else:
         #    for a in MYIFS.split():
         #       _check_v4inv6_port(get_ip_address(a),l_port) 
@@ -391,11 +392,11 @@ 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)
         
         if o_reused == False:
-        	if nline not in udpresult:				# Hide multi binds on same Socket (SO_REUSEPORT)
-        		udpresult.append(nline)
+            if nline not in udpresult:              # Hide multi binds on same Socket (SO_REUSEPORT)
+                udpresult.append(nline)
         else:
-        	udpresult.append(nline)
-        	
+            udpresult.append(nline)
+            
     return udpresult
 
 def netstat_udp6():
@@ -440,22 +441,29 @@ 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)
         
         if o_reused == False:
-        	if nline not in udpresult:				# Hide multi binds on same Socket (SO_REUSEPORT)
-        		udpresult.append(nline)
+            if nline not in udpresult:              # Hide multi binds on same Socket (SO_REUSEPORT)
+                udpresult.append(nline)
         else:
-        	udpresult.append(nline)
-        	
+            udpresult.append(nline)
+            
     return udpresult
 
 
-def _get_pid_of_inode(inode):
+def _get_pid_of_inode(iinode):
     '''
     To retrieve the process pid, check every running process and look for one using
     the given inode.
     '''
+    inode="\[" + iinode + "\]"
     for item in glob.glob('/proc/[0-9]*/fd/[0-9]*'):
         try:
-            if re.search(inode,os.readlink(item)):
+            searchlnk = os.readlink(item)
+        except:
+            continue
+        if not searchlnk.startswith("socket"):
+            continue
+        try:
+            if re.search(inode,searchlnk):
                 return item.split('/')[2]
         except:
             pass
@@ -511,7 +519,7 @@ if __name__ == '__main__':
     o_v6 = True
     o_v4 = True
     o_reused = False
-    parser = argparse.ArgumentParser(description='netstat utility V'+VERSION+"\n2017-2022 by sigi <https://wiki.zweiernet.ch/wiki/sinetstat>",
+    parser = argparse.ArgumentParser(description='netstat utility V'+VERSION+"\n2017-2025 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")