-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportAPI.py
44 lines (42 loc) · 1.4 KB
/
portAPI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
import subprocess
import re
class Ports:
"""
Module `Port`
Shows all running port on the system
`self.portDic={}` Dictionary which stores all process
`self.portList=[]` List that contains cluster of dictionaries
"""
def __init__(self):
self.portDic={}
self.portList=[]
def display_port(self):
"""
Displays all process along with Ports and Process IDS
"""
ports=os.popen("sudo netstat -ntlp").read().strip().splitlines()[2:]
for port in ports:
split=re.split('[\s]+',port)
self.portDic["Protcol"]=split[0]
self.portDic["Receive Q"]=split[1]
self.portDic["Send Q"]=split[2]
split_port=split[3].split(":")
if split_port[1]=="":
self.portDic["port"]="No Port"
else:
self.portDic["port"]=split_port[1]
self.portDic["Foreign Address"]=split[4]
self.portDic["State"]=split[5]
split_ID=split[6].split("/")
self.portDic["PID"]=split_ID[0]
self.portDic["Programme Name"]=split_ID[1]
self.portList.append(self.portDic.copy())
return self.portList
def kill_process(self,PID):
"""
Kill Process with given process ID
`PID` Process ID
"""
os.system("sudo kill {}".format(PID))
return True