def netstat_udp6(): """ List IPv6 UDP connections. Return list of processes utilizing IPv6 UDP connections. All localhost connections are filtered out. :return: List of connections """ udp6_socket_table = load_socket_table(PROC_ENTRY_UDP6) result = [] for line in udp6_socket_table: socket_data = line.split() if not socket_data: continue # Convert IPv6 address and port from hex to human readable format raw_address = socket_data[ProcFileSystem.NET_LOCAL_ADDRESS.value] local_address, local_port = parse_ipv6_address_port_pair(raw_address) if local_address.is_loopback: # Skip all loopback connections continue # Extract inode to get PIDs inode = socket_data[ProcFileSystem.NET_INODE.value] # Get PIDs from inode pids = get_pids_from_inode(inode) for pid in pids: exe_name = get_exe_name(pid) my_connection = connection.Connection("udp6", local_address, local_port, pid, exe_name) result.append(my_connection) return result
def netstat_raw(): """ List raw sockets connections. Return list of processes utilizing packet sockets. All localhost connections are filtered out. :return: List of connections """ raw_socket_table = load_socket_table(PROC_ENTRY_PACKET) result = [] for line in raw_socket_table: socket_data = line.split() if not socket_data: continue # Extract inode to get PIDs inode = socket_data[ProcFileSystem.NET_PACKET_INODE.value] # Get PIDs from inode pids = get_pids_from_inode(inode) for pid in pids: exe_name = get_exe_name(pid) my_connection = connection.Connection("raw", None, None, pid, exe_name) result.append(my_connection) return result
def netstat_tcp4(): """ List IPv4 TCP connections. Return list of processes utilizing IPv4 TCP listening connections. All localhost connections are filtered out. :return: List of connections """ tcp4_socket_table = load_socket_table(PROC_ENTRY_TCP4) result = [] for line in tcp4_socket_table: socket_data = line.split() if not socket_data: continue # Convert IPv4 address and port from hex to human readable format raw_address = socket_data[ProcFileSystem.NET_LOCAL_ADDRESS.value] local_address, local_port = parse_ipv4_address_port_pair(raw_address) if local_address.is_loopback: # Skip all loopback connections continue tcp_state_hex = socket_data[ProcFileSystem.NET_TCP_STATE.value] tcp_state = TCP_STATES[tcp_state_hex] if tcp_state != "LISTEN": # Skip all non-listenning connections continue # Extract inode to get PIDs inode = socket_data[ProcFileSystem.NET_INODE.value] # Get PIDs from inode pids = get_pids_from_inode(inode) for pid in pids: exe_name = get_exe_name(pid) my_connection = connection.Connection("tcp4", local_address, local_port, pid, exe_name) result.append(my_connection) return result