Browse Source

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

sigi 2 months ago
parent
commit
751591c4e5
2 changed files with 46 additions and 38 deletions
  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>
 __Improvements to the original netstat command:__ <br>
  \- *explore IPv4 in IPv6 listening/established sockets (IPv4-mapped IPv6 addresses)*<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>
  \- *shows UID and PID of the command*<br>
  \- *shorten output by hiding repeated entries on reused (SO_REUSEPORT) ports*<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`
 Direct Download: `wget https://git.zweiernet.ch/sigi/sinetstat/raw/master/sinetstat`
   
   
-
+Recent Version is 1.4.3
 
 
 Then simply try:  
 Then simply try:  
 <small>
 <small>
@@ -30,8 +30,8 @@ Then simply try:
 # sinetstat -h
 # sinetstat -h
 usage: sinetstat [-h] [-l] [-e] [-s] [-r] [-w] [-W] [-t] [-u] [-4] [-6]
 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:
 optional arguments:
   -h, --help  show this help message and exit
   -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_TCP4 = "/proc/net/tcp"
 PROC_UDP4 = "/proc/net/udp"
 PROC_UDP4 = "/proc/net/udp"
@@ -57,7 +57,8 @@ opt_l = True
 
 
 
 
 def grep_b(list, search):
 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):
 def get_ip_address(ifname):
     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@@ -112,8 +113,8 @@ def _ip6q(s):
 
 
 def _conv_v6(s):
 def _conv_v6(s):
     return s
     return s
-	
-	
+    
+    
 
 
 def _remove_empty(array):
 def _remove_empty(array):
     return [x for x in array if x !='']
     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)
         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 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:
         else:
-        	tcpresult.append(nline)
-        	# update v4inv6check list
-        	v4ports.append(l_port)
-        	
+            tcpresult.append(nline)
+            # update v4inv6check list
+            v4ports.append(l_port)
+            
     return tcpresult
     return tcpresult
 
 
 def netstat_tcp6():
 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)
         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 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:
         else:
-        	tcpresult.append(nline)
-        		
+            tcpresult.append(nline)
+                
     return tcpresult
     return tcpresult
 
 
 
 
@@ -299,7 +300,7 @@ def netstat_tcp4in6():
     for line in tcpcontent:
     for line in tcpcontent:
         line_array = _remove_empty(line.split(' '))
         line_array = _remove_empty(line.split(' '))
         #if TCP_STATE[line_array[3]] != 'LISTEN':
         #if TCP_STATE[line_array[3]] != 'LISTEN':
-        #	continue
+        #   continue
         
         
         l_host,l_port = _convert_ipv6_port(line_array[1])
         l_host,l_port = _convert_ipv6_port(line_array[1])
         r_host,r_port = _convert_ipv6_port(line_array[2])
         r_host,r_port = _convert_ipv6_port(line_array[2])
@@ -335,13 +336,13 @@ def netstat_tcp4in6():
             exe = '-'
             exe = '-'
             
             
         if l_host == '00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01':
         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 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:
         #else:
         #    for a in MYIFS.split():
         #    for a in MYIFS.split():
         #       _check_v4inv6_port(get_ip_address(a),l_port) 
         #       _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)
         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 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:
         else:
-        	udpresult.append(nline)
-        	
+            udpresult.append(nline)
+            
     return udpresult
     return udpresult
 
 
 def netstat_udp6():
 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)
         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 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:
         else:
-        	udpresult.append(nline)
-        	
+            udpresult.append(nline)
+            
     return udpresult
     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
     To retrieve the process pid, check every running process and look for one using
     the given inode.
     the given inode.
     '''
     '''
+    inode="\[" + iinode + "\]"
     for item in glob.glob('/proc/[0-9]*/fd/[0-9]*'):
     for item in glob.glob('/proc/[0-9]*/fd/[0-9]*'):
         try:
         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]
                 return item.split('/')[2]
         except:
         except:
             pass
             pass
@@ -511,7 +519,7 @@ if __name__ == '__main__':
     o_v6 = True
     o_v6 = True
     o_v4 = True
     o_v4 = True
     o_reused = False
     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 )
                                    formatter_class=argparse.RawDescriptionHelpFormatter )
     parser.add_argument('-l', help="Only listening sockets", action="store_true")
     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('-e', help="Only established sockets", action="store_true")