/
sauce.py
361 lines (287 loc) · 11.8 KB
/
sauce.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import sys
import time
import subprocess
from tkinter import Tk as tink
from tkinter import ttk as tinky
from tkinter import Frame, Menu, Entry, IntVar, Checkbutton, Button, Label, StringVar, END
# Here, we are creating our class, Window, and inheriting from the Frame
# class. Frame is a class from the tkinter module. (see Lib/tkinter/__init__)
class Window(Frame):
# Define settings upon initialization. Here you can specify
def __init__(self, master=None):
# parameters that you want to send through the Frame class.
Frame.__init__(self, master)
#reference to the master widget, which is the tk window
self.master = master
#with that, we want to then run init_window, which doesn't yet exist
self.init_window()
#Creation of init_window
def init_window(self):
# changing the title of our master widget
self.master.title('SAUCE')
# allowing the widget to take the full space of the root window
self.grid()
# creating a menu instance
menu = Menu(self.master)
self.master.config(menu=menu)
#-------- create the file object
file = Menu(menu)
# adds a command to the menu option, calling it exit, and the
# command it runs on event is client_exit
file.add_command(label="Import")
file.add_command(label="Save")
file.add_command(label="Save As")
file.add_command(label="Exit", command=self.client_exit)
#added "File" to our menu
menu.add_cascade(label="File", menu=file)
#------- create the edit object
edit = Menu(menu)
# adds a command to the menu option, calling it exit, and the
# command it runs on event is client_exit
edit.add_command(label="Clear All")
edit.add_command(label="Delete Row", command=delete_row)
#added "Edit" to our menu
menu.add_cascade(label="Edit", menu=edit)
#-------- create the tools object
tools = Menu(menu)
# adds a command to the menu option, calling it exit, and the
# command it runs on event is client_exit
tools.add_command(label="Add Toolbox")
#added "Tools" to our menu
menu.add_cascade(label="Tools", menu=tools)
#-------- create the help(ayuda) object
ayuda = Menu(menu)
# adds a command to the menu option, calling it exit, and the
# command it runs on event is client_exit
ayuda.add_command(label="About", command=self.about_popup)
#added "About" to our menu
menu.add_cascade(label="Help", menu=ayuda)
def client_exit(self):
exit()
def about_popup(self):
popupmsg("Version: 3.21")
class PowerShell:
def _init_(self):
return True
#Functions
def RunCommand(self, Command):
process = subprocess.Popen(["powershell", Command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
output = process.stdout.readlines()
errout = process.stderr.readlines()
if errout:
return errout
else:
return output
def get_computerName(self):
computerName = self.RunCommand("$env:computername")
computerName = computerName[0].strip('\n')
return computerName
def get_Serial(self):
return "wmic bios get serialnumber"
def get_Model(self):
return "wmic csproduct get name"
def get_Brand(self):
return "wmic csproduct get vendor"
def get_Status(self, computerName):
isOnline = self.RunCommand("Test-Connection -BufferSize 32 -Count 1 -ComputerName '" + computerName + "' -Quiet")
isOnline = isOnline[0].strip('\n') #[i.strip('\n') for i in isOnline]
if isOnline == "True":
return "Online"
else:
return "Offline"
def get_NETA(self):
d_status = "" # Device Status
macAddr = "" # MAC Address
ethernet = 'Get-NetAdapter -Name "Ethernet" | Select-Object '
e_line = ""
e_status = "I did not receive a new value"
wi_fi = 'Get-NetAdapter -Name "Wi-Fi" | Select-Object '
w_line = ""
w_status = "I did not receive a new value"
w_line = self.RunCommand(str(wi_fi) + "Status")
for line in w_line:
if "Up" in line:
w_status = "Online"
break
elif "Disconnected" in line:
w_status = "Offline"
break
else:
continue
e_line = self.RunCommand(str(ethernet) + "Status")
for line in e_line:
if "Up" in line:
e_status = "Online"
break
elif "Disconnected" in line:
e_status = "Offline"
break
else:
e_status = "Not Present"
continue
if ((e_status == "Offline") and (w_status == "Offline")):
d_status = "Offline"
macAddr = (ethernet + "MacAddress")
elif ((e_status == "Online") and (w_status == "Online")):
d_status = "'Online'"
macAddr = (ethernet + "MacAddress")
elif (e_status == "Online"):
d_status = "'Online'"
macAddr = (ethernet + "MacAddress")
elif (w_status == "Online"):
d_status = "'Online'"
macAddr = (wi_fi + "MacAddress")
elif (e_status == "Offline"):
d_status = "'Offline'"
macAddr = (ethernet + "MacAddress")
elif (w_status == "Offline"):
d_status = "'Offline'"
macAddr = (wi_fi + "MacAddress")
else:
d_status = "'Offline'"
macAddr = "'No Adapter'"
return [d_status, macAddr]
def get_computerInfo(self, computerName):
result=[]
commands = [self.get_Serial(), self.get_Model(), self.get_Brand(), self.get_NETA()[0], self.get_NETA()[1]]
if computerName == "this":
for command in commands:
result.append(self.RunCommand(command))
else:
for command in commands:
result.append(self.RunCommand("Invoke-command -ComputerName '" + str(computerName) + "' -ScriptBlock {" + command + "}"))
return result
# MAIN, variables
shell = PowerShell()
i=2
rows = []
# Data Grid functions here
# Create one row and add in to the rows list
def add_row(name,serial, model, brand, status, mac):
computer = [name,serial,model,brand,status,mac]
global i
i=i+1
items = []
var = IntVar()
# Create and append the checkbox to the item list
c = Checkbutton(center, variable = var)
c.val = var
items.append(c)
c.grid(row = i, column = 0)
# Next create and append the entry boxes to the items list
for j in range(1,7): #Columns
e = Entry(center)
items.append(e)
e.grid(row=i, column=j)
data = items[j] # Locate entry
data.delete(0, END) # Clear any text in entry
data.insert(0, computer[j-1]) # Add a text value to entry
data.config(state='readonly',justify='center') # set to read only
# Append the new entry fields to the rows list
rows.append(items)
# Remove selected row
def delete_row():
for rowno, row in reversed(list(enumerate(rows))):
if row[0].val.get() == 1:
for i in row:
i.destroy()
rows.pop(rowno)
# This is a function which increases the progress bar value by the given increment amount
def startProgress():
progessBarOne['value']=progessBarOne['value'] + 1
window.update_idletasks()
# This is a function which stops the progress bar
def stopProgress():
progessBarOne.stop()
# This is a function which shows a popup message.
def popupmsg(msg):
popup = tink()
popup.geometry('{}x{}'.format(155, 75)) # Width x Height
popup.wm_title("!")
label = tinky.Label(popup,text=msg)
label.configure(anchor="center")
label.pack(side="top",fill="x",pady=10)
B1 = tinky.Button(popup,text="Ok",command=popup.destroy)
B1.pack()
popup.mainloop()
# This is the function that gets the computer information
def show_ComputerInfo():
startProgress()
name = cnEntry.get().lower()
if name == "this":
results = shell.get_computerInfo(name)
print(results)
add_row(shell.get_computerName(), str(results[0][2]), str(results[1][2]), str(results[2][2]), str(results[3][0]), str(results[4][3]))
elif shell.get_Status(name) == "Online":
results = shell.get_computerInfo(name)
add_row(name.upper(), str(results[0][2]), str(results[1][2]), str(results[2][2]), "Online", "GHT:456")
else:
popupmsg("This device may be offline.")
stopProgress()
window = tink()
# window.iconphoto(True, PhotoImage(file="SAS.png"))
window.geometry('{}x{}'.format(873, 350)) # Width x Height
window.resizable(False, False)
window.config(bg='#fff')
# create all of the main containers
top_frame = Frame(window, bg='#fff', width=450, height=80, pady=3)
center = Frame(window, bg='#fff', width=50, height=40, padx=3, pady=3)
btm_frame = Frame(window, bg='#fff', width=450, height=20, pady=3)
btm_frame2 = Frame(window, bg='#fff', width=450, height=30, pady=3)
# layout all of the main containers
window.grid_rowconfigure(1, weight=1)
window.grid_columnconfigure(0, weight=1)
top_frame.grid(row=0, sticky="ew", pady=10)
center.grid(row=1, sticky="nsew")
btm_frame.grid(row=3, sticky="ew")
btm_frame2.grid(row=4, sticky="ew", pady=1)
# create the widgets for the top frame
cName_label = Label(top_frame, background='#fff',text='Computer Name:')
cnEntry = Entry(top_frame, width=30, background="#fff")
cnButton = Button(top_frame, width=10, text='FIND', bg='#4EC5F1', font=('arial', 8, 'normal'), command=show_ComputerInfo)
# layout the widgets in the top frame
cName_label.grid(row=1, column=0)
cnEntry.grid(row=1, column=1)
cnButton.grid(row=1, column=2)
# create the center widgets, datagrid
v0 = StringVar()
e0 = Entry(center, textvariable = v0, state = 'readonly', justify = 'center')
v0.set('Select')
e0.grid(row = 1, column = 0 )
v1 = StringVar()
e1 = Entry(center, textvariable = v1, state = 'readonly', justify= 'center')
v1.set('Name')
e1.grid(row = 1, column = 1 )
v2 = StringVar()
e2 = Entry(center, textvariable = v2, state = 'readonly', justify= 'center')
v2.set('Serial#')
e2.grid(row = 1, column = 2 )
v3 = StringVar()
e3 = Entry(center, textvariable = v3, state = 'readonly', justify= 'center')
v3.set('Model#')
e3.grid(row = 1, column = 3)
v4 = StringVar()
e4 = Entry(center, textvariable = v4, state = 'readonly', justify= 'center')
v4.set('Brand')
e4.grid(row = 1, column = 4 )
v5 = StringVar()
e5 = Entry(center, textvariable = v5, state = 'readonly', justify= 'center')
v5.set('Status')
e5.grid(row = 1, column = 5 )
v6 = StringVar()
e6 = Entry(center, textvariable = v6, state = 'readonly', justify= 'center')
v6.set('MAC')
e6.grid(row = 1, column = 6 )
# Vertical (y) Scroll Bar
#scrolly=Scrollbar(window, command=center.yview)
#scrolly.pack(side=LEFT, fill=Y, pady=65)
# create the widgets for the bottom frames
progessBarOne=tinky.Progressbar(btm_frame2, style='progessBarOne.Horizontal.TProgressbar', orient='horizontal', length=873, mode='indeterminate', maximum=100, value=0)
# layout the widgets in the bottom frames
progessBarOne.grid(row=1, column=0)
#creation of an instance for window menu
app = Window(window)
#creation of an instance for data grid
# data = DataGrid(center)
# END, Open Window
window.mainloop()