forked from HarshCasper/Rotten-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ports_kill.py
37 lines (33 loc) · 1020 Bytes
/
ports_kill.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
import psutil
import sys
# To get the PID according to the Port Number
def get_pid():
connections = psutil.net_connections()
port = int(sys.argv[1])
# Using psutil functionality
for con in connections:
if con.raddr != tuple():
if con.raddr.port == port:
return con.pid, con.status
if con.laddr != tuple():
if con.laddr.port == port:
return con.pid, con.status
return -1
# CLI Input
if __name__ == '__main__':
if len(sys.argv) > 1:
pid = get_pid()
if pid == -1:
print(":: Not Found :<")
else:
print(f"Found service on Port {sys.argv[1]}")
print(f"[+] PID: {pid[0]}")
print(f"[+] Status: {pid[1]}")
ch = input("Close the Port?: (y/n) ")
# Takes Keyboard Input
if ch.lower() == 'y':
p = psutil.Process(pid[0])
p.terminate()
"""
Sample Input -
python3 ports_kill.py <port number>"""