Created
August 20, 2020 00:03
-
-
Save parrotmac/3a372b7e2bba819885bdf0ba1cd61957 to your computer and use it in GitHub Desktop.
List Processes with open HCI Sockets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import os | |
| hci_sockets = [] | |
| with open('/proc/net/hci') as hcif: | |
| for line in hcif: | |
| if 'Inodes' in line: | |
| continue | |
| hci_sockets.append(line.split()[5]) | |
| pids = [ | |
| pid for pid in os.listdir('/proc') if pid.isdigit() | |
| ] | |
| pid_socket_map = {} | |
| for pid in pids: | |
| proc_fd_path = '/proc/{}/fd'.format(pid) | |
| proc_links = [] | |
| try: | |
| for fd in os.listdir(proc_fd_path): | |
| link_name = os.readlink("{}/{}".format(proc_fd_path, fd)) | |
| if 'socket:' in link_name: | |
| proc_links.append(link_name) | |
| if len(proc_links) > 0: | |
| pid_socket_map[pid] = proc_links | |
| except: | |
| pass | |
| hci_pids = [] | |
| for pid, sockets in pid_socket_map.items(): | |
| for socket in sockets: | |
| for hci_socket in hci_sockets: | |
| if hci_socket in socket: | |
| if pid not in hci_pids: | |
| hci_pids.append(pid) | |
| print("\n".join(hci_pids)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment