Repository navigation
Expand file tree
/
Copy pathnewPrinters.py
More file actions
186 lines (159 loc) · 6.09 KB
/
Copy pathnewPrinters.py
File metadata and controls
186 lines (159 loc) · 6.09 KB
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
from multiprocessing import Pool
from win10toast import ToastNotifier
import multiprocessing as mp
import pandas as pd
import paramiko
import time
import os
import re
brands = []
cmds = []
counter = 0
toaster = ToastNotifier()
df = pd.read_excel('printerIP.xlsx', sheet_name = 'Sheet1')
def ricohToners(cmd, ip, location):
#Reads Black toner levels
black = os.popen(cmd + " 1.3.6.1.2.1.43.11.1.1.9.1.1").read()
bLevel = re.sub("[^0-9]", "", black[-4:-1])
if str(black):
#Checks if printer is a color printer
cyan = os.popen(cmd + " 1.3.6.1.2.1.43.11.1.1.9.1.3").read()
cLevel = re.sub("[^0-9]", "", cyan[-4:-1])
if str(cyan):
#Read remaining toner levels if it is a color printer
magenta = os.popen(cmd + " 1.3.6.1.2.1.43.11.1.1.9.1.4").read()
mLevel = re.sub("[^0-9]", "", magenta[-4:-1])
yellow = os.popen(cmd + " 1.3.6.1.2.1.43.11.1.1.9.1.5").read()
yLevel = re.sub("[^0-9]", "", yellow[-4:-1])
#Creates a warning notification if the toner levels are below 10%
if int(bLevel) < 10:
text = "Black is low/empty at " + ip + " " + location
toaster.show_toast("WARNING", text, duration=15, threaded=True)
while toaster.notification_active():
time.sleep(0.1)
if int(cLevel) < 10:
text = "Cyan is low/empty at " + ip + " " + location
toaster.show_toast("WARNING", text, duration=15, threaded=True)
while toaster.notification_active():
time.sleep(0.1)
if int(mLevel) < 10:
text = "Magenta is low/empty at " + ip + " " + location
toaster.show_toast("WARNING", text, duration=15, threaded=True)
while toaster.notification_active():
time.sleep(0.1)
if int(yLevel) < 10:
text = "Yellow is low/empty at " + ip + " " + location
toaster.show_toast("WARNING", text, duration=15, threaded=True)
while toaster.notification_active():
time.sleep(0.1)
else:
if int(bLevel) < 10:
text = "Black is low/empty at " + ip + " " + location
toaster.show_toast("WARNING", text, duration=15, threaded=True)
while toaster.notification_active():
time.sleep(0.1)
else:
#Provides a timeout error notification if the printer cannot be reached
text = "Check " + ip + " " + location
toaster.show_toast("Timeout Error", text, duration = 15, threaded = True)
while toaster.notification_active():
time.sleep(0.1)
def hpToners(cmd, ip, location):
#Reads Black toner levels and toner type
black = os.popen(cmd + " 1.3.6.1.2.1.43.11.1.1.9.1.1").read()
bLevel = re.sub("[^0-9]", "", black[-4:-1])
order = os.popen(cmd + " 1.3.6.1.2.1.43.11.1.1.6.1.1").read()
order = order[-6:-2]
if str(black):
#Checks if printer is a color printer
cyan = os.popen(cmd + " 1.3.6.1.2.1.43.11.1.1.9.1.2").read()
cLevel = re.sub("[^0-9]", "", cyan[-4:-1])
if str(cyan):
#Read remaining toner levels if it is a color printer
magenta = os.popen(cmd + " 1.3.6.1.2.1.43.11.1.1.9.1.3").read()
mLevel = re.sub("[^0-9]", "", magenta[-4:-1])
yellow = os.popen(cmd + " 1.3.6.1.2.1.43.11.1.1.9.1.4").read()
yLevel = re.sub("[^0-9]", "", yellow[-4:-1])
#Creates a warning notification with toner type if the toner levels are below 10%
if int(bLevel) < 10:
text = "Black is low/empty at " + ip + " " + location + "\n" + order
toaster.show_toast("WARNING", text, duration=15, threaded=True)
while toaster.notification_active():
time.sleep(0.1)
if int(cLevel) < 10:
text = "Cyan is low/empty at " + ip + " " + location + "\n" + order
toaster.show_toast("WARNING", text, duration=15, threaded=True)
while toaster.notification_active():
time.sleep(0.1)
if int(mLevel) < 10:
text = "Magenta is low/empty at " + ip + " " + location + "\n" + order
toaster.show_toast("WARNING", text, duration=15, threaded=True)
while toaster.notification_active():
time.sleep(0.1)
if int(yLevel) < 10:
text = "Yellow is low/empty at " + ip + " " + location + "\n" + order
toaster.show_toast("WARNING", text, duration=15, threaded=True)
while toaster.notification_active():
time.sleep(0.1)
else:
if int(bLevel) < 10:
text = "Black is low/empty at " + ip + " " + location + "\n" + order
toaster.show_toast("WARNING", text, duration=15, threaded=True)
while toaster.notification_active():
time.sleep(0.1)
else:
#Provides a timeout error notification if the printer cannot be reached
text = "Check " + ip + " " + location
toaster.show_toast("Timeout Error", text, duration = 15, threaded = True)
while toaster.notification_active():
time.sleep(0.1)
def main(cmds):
cmd = cmds[0]
i = cmds[1]
brands = cmds[2]
#Checks to see if printer is a Ricoh or an HP printer
if brands == 'Ricoh':
ricohToners(cmd, str(df['IP'][i]), str(df['Location'][i]))
elif brands == 'HP':
hpToners(cmd, str(df['IP'][i]), str(df['Location'][i]))
def ssh():
COMP = 'ip of remote computer'
#Setup for SSH connection via paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(COMP, 22, username = remote computer user, password = remote computer pwd , allow_agent = False)
stdin = ssh.exec_command("")
conn = ssh.invoke_shell()
#Command to run the printer script on the remote computer
output = conn.recv(1000)
conn.send("\n")
conn.send("python3 yardPrinters.py\n")
time.sleep(15)
rawData = conn.recv(1000000)
rawData = rawData.decode("ascii").strip("\n")
dataTemp = rawData.splitlines()
data = dataTemp[4:-1]
#Parses the output for toner levels below 10%
for i in data:
text = str(i[:-16] + "\n" + i[-16:])
print(text)
toaster.show_toast("WARNING", text, duration = 15, threaded = True)
while toaster.notification_active():
time.sleep(0.1)
if __name__ == "__main__":
for i in df.index:
brands.append(df['Brand'][i])
ip = df['IP'][i]
if brands[i] == 'Ricoh':
cmd = "snmpwalk -v1 -c public " + ip
cmds.append([cmd, counter, str(brands[i])])
elif brands[i] == 'HP':
cmd = "snmpwalk -v1 -c public " + ip
cmds.append([cmd, counter, str(brands[i])])
counter += 1
with Pool(mp.cpu_count()) as p:
records = p.map(main, cmds)
p.terminate()
p.join()
#Only run this command if accessing printers on a different network
ssh()