|
@@ -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")
|