-
Notifications
You must be signed in to change notification settings - Fork 0
/
hitman.py
81 lines (62 loc) · 2.6 KB
/
hitman.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
######################################################
# hitman.py #
# Torrent Glenn #
# 11/13/2013 #
# CheeseWhiz revamped #
######################################################
""" The hitman is exactly what the cheesewhiz family has been lacking all this time. Now the hitman
(essentially the opposite of cheesewhiz) simply kills all the processes you want dead. Another addition, listAdd.py
is the new interface for managing the cheesewhiz family"""
import os
def rmSpace(spaceList):
#Later in the code when the information returned by ps-ef
#needs to be parsed this function removed the empty strings
#in the list in order to make it easier to retrieve
#information via indexes
#rmSpace() recursively removes the first empty string in the
#given list until there are no more empty strings
if '' in spaceList:
spaceList.remove('')
rmSpace(spaceList)
else:
return spaceList
#Open the hitlist, get all the lines, and put them in a list
with open('hitlist.txt') as w:
content = w.readlines()
# make a new list wherein all the newline characters have been stripped
programs = [elem.strip("\n") for elem in content]
# runs the unix script to output the results of ps-ef into ps.txt
#os.system("top -n 1 -b > top.txt")
os.system("ps -ef > psh.txt")
# create an empty list to be populated with the results of parsing ps.txt
tst = []
# from ps.txt, parse each line to get the name of the process and append it to tst
with open('psh.txt', 'r') as input:
for line in input:
newline = line.split(' ')
rmSpace(newline)
tst.append(newline[1].strip())
# create an empty string to be populated by the subset of all processes which are running
#that we want to kill
running = ""
# get the PIDs of the programs to kill and append them to IDs.txt
for item in programs:
os.system("ps -ef | grep "+ item +" >> IDs.txt")
# the list of programs we want dead
toKill = []
#here we put all the PIDs of programs we want to kill in the toKill list
# and remove trailing whitespace
with open ("IDs.txt","r") as input:
for line in input:
newline = line.split(" ")
rmSpace(newline)
toKill.append(newline[1].strip())
#get the intersection of processes that are running and processes we want to kill
for item in toKill:
if item in tst:
running += " " + item
# kill the programs in the aforementioned intersection: programs we want to kill that
# are running
os.system("kill -9 " + running)
#remove IDs.txt
os.system("rm IDs.txt psh.txt")