📜 ⬆️ ⬇️

How I created a printer installation system at work

image

Hello, dear% user%. Today I will talk about how I wrote a user system to enable users to install the printers required for them. I will tell you a little about the place where I work, so that it is clear why I did it. I will say in advance that I knew about the existence of a role component in Windows Server 2012 R2, which makes it possible to install a printer from a browser. But I wanted to invent my bike, and the restrictions here will probably be in fantasy. To whom it became interesting welcome under kat. For those who are not patient, I’ll just say that the screenshots are at the end of the article.

Little about work


I work in helpdesk in some big organization that has many branches. There are more than a thousand employees working in front of computers. One can guess that there is a large zoo of network printers, there are individual employees responsible for the operability of printers. We carry out installation of printers to computers - helpdesk. Very often, employees of different departments move from one workplace to another, come or leave, or even change their position and profession, and so on. A lot of interesting things. Accordingly, because of this, very often calls are made to our helpdesk asking to install a printer for them. You see, asking the user to tell the printer’s IP address is like asking a monkey to cook soup. I will give an example of a dialogue (real case):

U - user, I - I
U- Hello, this is a helpdesk I got there?
I- Yes. Right. You got there
U- Help me please install the printer on my computer
I-Okay, please tell me the name of your computer.
U-My computer name is comp-01 (fictitious)
I-What printer do you need? (Connecting to computer)
U-HP LaserJet 600, it is in% place%
I - the location of the printer does not give me anything (and the truth will change it with another printer since the first one went bad, or it will change its place). Well, can you tell me the ip address of this printer?
U- ...
Pause at the end of the tube. I already heard the user's brain begin to move, blood began to flow out of his ears, and finally he asked
U- Sorry what? Amy? What do you say? Imy address?
I - ip, ip address.
U-ipi? And what is it? How can you look at it?
I –Okay tell me, is there someone else using this printer?
U-Well, it uses% another_user%
I-Please ask him for the name of the computer and get permission for me to connect to it for a moment
U- Ok, now (and I hear the muffled voice of how he asks% another_user% computer name and permission to connect)
I- ... .. (thinking to myself: aimi aimi aimi, sticks of a tree what rhyme on ayimi can come up with)
U-Ale
I- Yes, I'm listening to you
U- Computer name comp-02, you can connect
I- Ask% another_user% to show me the printer that he constantly uses (I connect to the computer)
U- Ok, now (again a muffled voice) ... ... - This one
Okay, okay, I get it. I’ll not keep you on the line. Now I’ll put everything in and set it up.
U- Ok, thanks. However, you did not tell me what is aymi.
I- You probably meant ip. Well, it will be difficult to explain now, as there are too many calls, but in general this is the address of the device on the network.
Have you ever heard how the human brain breaks down? I heard this sound, you know such a deaf sound with splashes of a large amount of information with the content of only “water”.
U - .... (long pause) ... I see. I'll go away on business and you put the printer
I- Yeah, well, goodbye

Now imagine that every third caller in the helpdesk, with a request to install the printer in the same condition. How to be? This kind of dialogue is very tiring, and to explain to the user how to print the configuration of the device to look at the printer’s IP, this is something extra heavy. So some kind of automation is needed. And what if you collect centrally all the printers on one separate printer server, write a simple web site with a friendly interface. A couple of clicks and the printer is installed. Challenge accepted.
')

Everything is not so difficult as it seems


I asked system administrators to allocate a separate server for Windows Server 2012 R2.

On the printer server


With printers:


We start to program


The website is built on PHP by writing a samopny MVC engine, using only Notepad ++ editor, Google and a browser (yes - only on hardcore). I don’t have knowledge of any framework, study laziness, use javascript frameworks in 2k17?, And isn't it too much for printers?

We set the goal that we need from this system:


Define the structure of the project:


In the root folder, create the index.php file and add the code:

<?php //  session_start(); //   spl_autoload_register(function ($class){ include './app/controllers/' . $class . '.php'; }); //  .        $controller = new Controller($_GET, $_POST); ?> 

In the configs folder, create the database.php file:

 <?php define("HOST", "localhost"); define("USER", "root"); define("PASSWORD", "OUR_PASSWORD"); define("DATABASE", "printer"); ?> 

Go to the browser in phpmyadmin, create a printer database using the utf8_general_ci encoding. Create 3 tables with the names branches, printers, users.

Branch structure:
NameType ofAdditionally
Idint (6)AUTO_INCREMENT, PRIMARY, UNIQUE
branch_namevarchar (255)
imagevarchar (255)

Printers structure:
NameType ofAdditionally
Idint (6)AUTO_INCREMENT, PRIMARY, UNIQUE
Namevarchar (255)
branchidint (6)Default value: 1
descriptiontext
ipaddressvarchar (255)
imagevarchar (255)
File1varchar (255)
File2varchar (255)
File3varchar (255)

Users structure:
NameType ofAdditionally
Idint (6)AUTO_INCREMENT, PRIMARY, UNIQUE
Loginvarchar (128)
Tokenvarchar (128)
passwordvarchar (128)
langvarchar (10)
logindatevarchar (255)

As can be seen from the tables branchid determines to which branch the printer belongs (having received the value of its id).

Create a record with id equal to 1, the branch name “none” and image with the value “none”. This entry is needed to set the printer is not set branch. The first entry is hard-coded and is not displayed in the lists of branches, as well as for users and in the admin. It can not be deleted.

I found the Database class on the Internet. It has a function for connecting to our database, using the values ​​for the connection from the database.php file located in the configs folder and the function for querying the database.

The İnfo class is used to get, change, delete data from the database. It is with this class that all operations in the system are performed.

One of the important classes is the class Controller.php . As the name suggests, this class is a controller that accepts GET and POST requests in __construct ($ get, $ post) from index.php. After receiving a specific request, it collects pieces of the page into a single corresponding request and performs the required functions from Info.php .

The Views class is a class that loads pieces of a page. Each page has its own name corresponding to the php file in the Views folder. So for example, to display the main page, you should write:

 $views = new Views; $views->addView('header', 'header.php'); $views->addView('menu', 'menu.php'); $views->addView('dashboard', 'dashboard.php'); $views->addView('footer', 'footer.php'); 

In the same class, the current language of the user is determined, the localization is loaded from the ini file, and the variable $ lang is created, which is already used in the piece of the page itself. Sample code for the main page ( dashboard.php ):

 <div class="dashboard"> <div class="container"> <h1><?php echo $lang['DASHBOARD_HEADER']; ?></h1> <p><?php echo $lang['DASHBOARD_TEXT']; ?></p> <br> <br> <p><a class="btn btn-primary btn-lg" href="index.php?branches" role="button"><?php echo $lang['DASHBOARD_INSTALL']; ?> »</a></p> <img class="dashboard-img" src="app/views/img/printer_icon.png"> </div> </div> 

In the views folder there are also css, javascript files. I used Bootstrap as the basis for CSS styles.

The Lang class defines the user's current language. First, it tries to get cookies cookie_lang. If it does not detect it, then it defaults to the Russian language. The getLangArray () function loads the required ini file and returns an array of the form “key” -> “value”. It is this function that is used in the views class.

What we got


The user enters the main page. There is a description of where he got and the button that opens the list of all branches. After the user selects a branch, a list of printers opens. Printers are displayed in a grid, where their photos and their names. The user selects a printer, a page with the selected printer opens, where there is a large “Install” button (also there is information about the printer, its IP address). When you click on this button, the bat file is downloaded, which in turn opens the vbs script shared in the scripts $ folder on the printer server. The problem is that if you upload a vbs file, then instead of downloading it, the browser opens this file in its new tab. Therefore it was necessary to be so perverted. Vbs files on the ball folder are also sorted along with their bat file into folders. For example, below is the folder structure:

Printer # id

Where #id is the serial number of the printer.

When adding a new printer through the admin panel, fill in the required fields and select the desired bat file from the balloons. The installation file, the printer photo is uploaded to the uploads folder. In the admin it is done in such a way that we can download up to three different installation files. How many files downloaded so many buttons "Install". In this case, there were no alternative methods for installing printers. VBS script was enough.

Vbs script


Vbs script setting the printer looked like this:

 printerName = "\\prnserver01\HP LaserJet 600 printer1 branch1" Set WshNetwork = CreateObject("WScript.Network") WshNetwork.AddWindowsPrinterConnection printerName WSHNetwork.SetDefaultPrinter printerName 

This script runs in invisible mode, a command window is displayed for a while, then it disappears. This script has a minus of what is not clear what is happening. Therefore, thinking a little changed it to this form:

 '   printerName = "\\prnserver01\HP LaserJet 600 printer1" '  Internet Explorer Set objExplorer = CreateObject("InternetExplorer.Application") '   - , ,    objExplorer.Navigate "about:blank" objExplorer.ToolBar = 0 objExplorer.StatusBar = 0 objExplorer.Left = 500 objExplorer.Top = 250 objExplorer.Width = 550 objExplorer.Height = 170 objExplorer.Visible = 1 '  objExplorer.Document.Title = "Ustanovka printera" ' html  ,     objExplorer.Document.Body.InnerHTML = "<table style=""width:100%""><tr><td id=""progress"" style=""font-family:Segoe UI;text-align: center;font-size:48px;border-bottom:1px solid black;""> Ustanovka printera: 0%</td></tr><tr><td style=""font-family:Segoe UI;text-align: center;font-size:22px;"">" & printerName & "</td></tr></table>" ' 500 ,      id progress    .    ,    0 Wscript.Sleep 500 objExplorer.document.getElementById("progress").innerText = " Ustanovka printera: 10%" '    ,       .            Set WshNetwork = CreateObject("WScript.Network") WshNetwork.AddWindowsPrinterConnection printerName WSHNetwork.SetDefaultPrinter printerName '   200 ,        Wscript.Sleep 200 objExplorer.document.getElementById("progress").innerText = " Ustanovka printera: 20%" '     200  Wscript.Sleep 200 objExplorer.document.getElementById("progress").innerText = " Ustanovka printera: 40%" ' ,         Wscript.Sleep 200 objExplorer.document.getElementById("progress").innerText = " Ustanovka printera: 60%" '      Wscript.Sleep 200 objExplorer.document.getElementById("progress").innerText = " Ustanovka printera: 80%" '  , ! Wscript.Sleep 100 objExplorer.document.getElementById("progress").innerText = " Printer ustanovlen!" '  3     ,      Wscript.Sleep 3000 objExplorer.Quit 

Screenshots


Screenshots
image

image

image

image

image

image

image

image

image

image

image

image

image

image

image

image

The source code of all this is posted on github

Now there are fewer calls to install printers.
Thanks for attention!

Source: https://habr.com/ru/post/333056/


All Articles