-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINFSelectionPrint.py
165 lines (143 loc) · 5.83 KB
/
INFSelectionPrint.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
import subprocess
import time
import os
import socket
from tkinter import Tk, filedialog
import hashlib
import getpass
import sys
def validate_ipv4(address):
try:
socket.inet_pton(socket.AF_INET, address)
return True
except (socket.error, AttributeError):
try:
socket.inet_aton(address)
return True
except socket.error:
return False
def clear_console():
os.system('cls') # For Windows
def browse_file():
root = Tk()
root.withdraw()
file_path = filedialog.askopenfilename(
title="Select INF File",
filetypes=(("INF Files", "*.inf"), ("All Files", "*.*"))
)
root.destroy()
return file_path
def hash_password(password):
# Use SHA-256 for hashing
sha256 = hashlib.sha256()
sha256.update(password.encode())
return sha256.hexdigest()
def get_masked_password():
return getpass.getpass("Enter password to proceed: ")
def handle_subprocess(command):
try:
subprocess.run(command, check=True)
except subprocess.CalledProcessError as e:
print(f"Error executing subprocess: {e}")
# Handle the error gracefully, possibly logging it or prompting the user for appropriate action
sys.exit(1)
def choose_printer():
while True:
clear_console()
print("Network Printer Installation [Version 1.00]")
print("(c) Printer Batch Installation. All rights reserved.")
# Prompt user for password
password = get_masked_password()
# Hash the entered password for comparison
hashed_password = hash_password(password)
# Replace the stored hashed password with the actual hashed password
stored_hashed_password = hash_password("P@55w0rd_")
# Compare the hashed passwords
if hashed_password == stored_hashed_password:
print("Password accepted. Please continue.")
else:
print("Incorrect password. Try again.")
time.sleep(2)
continue # Restart the loop if the password is incorrect
print("")
print("Existing Printer:")
print("")
print("1. Canon PCL6 Driver")
print("2. HP Universal Printing PCL 6")
print("")
var_choice = input("Select your Printer Driver: ")
if var_choice == "1":
var_brand = "Canon Printer"
while True:
var_ip = input(f"IP address for {var_brand}: ")
if var_ip.strip() and validate_ipv4(var_ip):
break
else:
print("Invalid IPv4 format. Please enter a valid IP Address.")
clear_console()
var_driver = "Canon PCL6 Driver"
var_inf_path = browse_file()
confirm_installation(var_choice, var_brand, var_driver, var_ip, var_inf_path)
break
elif var_choice == "2":
var_brand = "HP Printer"
while True:
var_ip = input(f"IP address for {var_brand}: ")
if var_ip.strip() and validate_ipv4(var_ip):
break
else:
print("Invalid IPv4 format. Please enter a valid IP Address.")
clear_console()
var_driver = "HP Universal Printing PCL 6 Driver"
var_inf_path = browse_file()
confirm_installation(var_choice, var_brand, var_driver, var_ip, var_inf_path)
break
else:
print("Invalid Selection. Please try again.")
time.sleep(2)
def confirm_installation(var_choice, var_brand, var_driver, var_ip, var_inf_path):
clear_console()
print(f"Printer selected : {var_brand}")
print(f"Printer Driver : {var_driver}")
print(f"IP address : {var_ip}")
print(f"Driver Path Selected (INF Files) : {var_inf_path}")
print("")
while True:
print("Proceed with this setup? (Yes/No/Restart)")
print("Enter y/n or r for restart")
var_confirm = input().upper()
if var_confirm in ["YES", "NO", "RESTART", "Y", "N", "R"]:
if var_confirm == "YES" or var_confirm == "Y":
print("Installation starting in 5 seconds...")
time.sleep(5)
rundll32_path = os.path.join(os.environ['SYSTEMROOT'], 'System32', 'rundll32.exe')
if os.path.exists(rundll32_path):
handle_subprocess([rundll32_path, "printui.dll,PrintUIEntry", "/ia", "/f", var_inf_path, "/h", "x64"])
handle_subprocess([rundll32_path, "printui.dll,PrintUIEntry", "/if", "/b", "Network Printer", "/f", var_inf_path, "/r", var_ip, "/m", var_driver])
print("Installation Finished. Goodbye!")
time.sleep(3)
sys.exit()
else:
print("Error: rundll32 not found. Installation cannot proceed.")
sys.exit(1)
elif var_confirm == "NO" or var_confirm == "N":
print("Exiting the script. Goodbye!")
sys.exit()
elif var_confirm == "RESTART" or var_confirm == "R":
print("Restarting printer selection...")
time.sleep(2)
choose_printer()
break
else:
clear_console()
print("Invalid Input Option!")
time.sleep(3)
clear_console()
print("Your setting is :")
print(f"Printer selected : {var_brand}")
print(f"Printer Driver : {var_driver}")
print(f"IP address : {var_ip}")
print(f"INF file path : {var_inf_path}")
print("")
if __name__ == "__main__":
choose_printer()