Function to return a list with status of tcp connections at linux systems To get pid of all network process running on system, you must run this script as superuser
(typ='tcp')
| 52 | return host_out,int(port,16) |
| 53 | |
| 54 | def netstat(typ='tcp'): |
| 55 | ''' |
| 56 | Function to return a list with status of tcp connections at linux systems |
| 57 | To get pid of all network process running on system, you must run this script |
| 58 | as superuser |
| 59 | ''' |
| 60 | with open('/proc/net/'+typ,'r',encoding='utf8') as f: |
| 61 | content = f.readlines() |
| 62 | content.pop(0) |
| 63 | result = [] |
| 64 | for line in content: |
| 65 | line_array = _remove_empty(line.split(' ')) # Split lines and remove empty spaces. |
| 66 | tcp_id = line_array[0] |
| 67 | l_addr = _convert_ip_port(line_array[1]) |
| 68 | r_addr = _convert_ip_port(line_array[2]) |
| 69 | state = line_array[3] |
| 70 | inode = int(line_array[9]) # Need the inode to match with process pid. |
| 71 | nline = [tcp_id, l_addr, r_addr, state, inode] |
| 72 | result.append(nline) |
| 73 | return result |
| 74 | |
| 75 | def get_bind_addrs(pid): |
| 76 | ''' |
no test coverage detected