Jump to content

Help creating an interesting script...

Hi guys!

 

I work at a small IT department at my college. There are about 100 printers all around campus that all need to be checked twice daily for toner levels, and written on a sheet for documentation. All of this information is obtained by visiting the printer's IP address. It is then displayed on that page.

 

What I want to try to do is script this process. Basically, I'd want it to:

 

Visit each IP address in a list

Find the words " Imaging Unit Life Remaining" and that type of thing, take the digits following, and place those numbers in a corresponding Excel spreadsheet

 

I do not have any kind of scripting background, unfortunately, so I'm not sure of any way to do this. But it sounds very possible and, likely, very easy.

 

Anyone know how I can go about doing this, have any advice on how, or can offer any help?

 

Thanks guys!

Link to comment
https://linustechtips.com/topic/607208-help-creating-an-interesting-script/
Share on other sites

Link to post
Share on other sites

you can do this in JavaScript. 

 

var myInnerHtml = document.getElementById("tag ID here").innerHTML;

this will let you load the data inside the tag, basically get the toner level.

I am currently looking for a get function so that you can go to each url and get that ellement from the each page. Then you just need to create an array of addresses and print the returning data out :)

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

1 hour ago, GFC_ said:

Awesome! Thank you for the help.

had to stop as I had to set up a friends computer. I haven't forgotten I will get a simple one script together tomorrow.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

43 minutes ago, DevBlox said:

You could analyze what rest calls the web page is doing, if any. That would be even more simple and robust than crawling over the whole page :)

How would that be 'more simple' than using something like a dictionary of regex to acquire the data? Presumable there's going to be a multitude of different makes and therefore a multitude of different endpoints/APIs.

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

So i have a script that will pull the title of the pages. for me these are my rounter, printer and NAS. I had to use this plugin to get my script to work. Cross domain security who knew? :P

 

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script> 
    var links = ["http://192.168.1.1","http://192.168.1.9/PRESENTATION/HTML/TOP/INDEX.HTML","http://192.168.1.2"];
    function load(){
        for(var i=0; i < links.length; i++)
        {
            jQuery.ajax({
                url: links[i],
                success: function(result) {
                    var html = jQuery('<title>').html(result);
                    document.getElementById('output').innerHTML += "<p>"+links[i]+" Title is " + html.find("title").html() +"</p>";
                    
                },
            });
        }

        count++;
    }
</script>
</head>
<body>
    <button onclick="load()">Get</button>
    <div ID="output"></div>
</body>
</html>

Outputs

undefined Title is undefined

undefined Title is Super Router

undefined Title is WF-2650 Series

First one being my Nas no idea why it will not get it or why the 3rd item is first... I also have no idea why it doesn't get the links while in the function so if anyone can fix that would be good. I'm sure you could do this in vbs script? this seems like a very hacky way to get the data TBH it's not very elegant at all but works.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

So I've been searching everywhere and I really cannot figure out how to do it for my own implementation. Basically:

 

This is what the web pages look like. The text I am interested in is "Black Cartridge ~33%" and "Imaging Unit Life Remaining: 27%". What I would like to do is have these values entered into a premade Excel sheet, in order.

 

I have tried various web extensions, and they work for the most part, but they usually use xpaths, and since some printers have slightly different web pages, those aren't a universal fix. I would much rather use something that actually pulls the entire website HTML as a string, removes the HTML tags, and then iterates through that string to grab what I need.

 

Any thoughts of where to look? VBScript seems to be able to do this, but I do not know nearly enough about it. I am not trying to say "Write this for me!" here, I am truly trying to understand how to do this.

printerinfo.PNG

Link to post
Share on other sites

32 minutes ago, GFC_ said:

So I've been searching everywhere and I really cannot figure out how to do it for my own implementation.

32 minutes ago, GFC_ said:

Any thoughts of where to look?

Try your own thread perhaps...

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

1 minute ago, Nuluvius said:

yfX2KDp.png

I was editing my post. I misunderstood you, thinking you were confused as to who the OP is. But, good one. Teehee.

 

Anyways, I did check my own thread, and many others around the internet, and I have not really gotten anywhere. Hence why I posted a little more information on my specific implementation.

 

I'm not a coder. I'm trying to understand advice as best I can but you're going to have to be a little specific in advice.

Link to post
Share on other sites

14 minutes ago, GFC_ said:

I'm not a coder. I'm trying to understand advice as best I can but you're going to have to be a little specific in advice.

Ok lets think about this then. You have a number of web interfaces on a network which you would like to visit and collect some data from.

 

You therefore want an simple application that can take a dynamic list of addresses and also a a list of data shapes (probably regular expressions) to look for. You could implement that in whatever language you like. You can decide whether you want to iterate the regex list on each visit or try to match them up with the addresses; you'd trade repeated data for repeated cycles. Perhaps consider if your web interfaces are all the same shape or not.

 

What specifically are you have trouble with? Design or implementation?

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

On 6/7/2016 at 11:10 AM, Nuluvius said:

How would that be 'more simple' than using something like a dictionary of regex to acquire the data? Presumable there's going to be a multitude of different makes and therefore a multitude of different endpoints/APIs.

I personally find it way easier to call an API. Though my statement is based on some assumptions, mainly that universities/colleges may very well buy printers in bulk, so they may be the same model. But even though they may be of different makes and models, having tailored a ton of regex'es or listing a ton of endpoints for all models is not that different anyway. IMO when using an API the script is more likely to survive a firmware update of the printers than a regex check, it could also protect against a more horrible administering problem - when only part of printers have their firmware updated, one part may have an updated GUI, another part may not. It becomes a less brittle solution, I bet @GFC_ would like to spend as less time as possible maintaining that thing.

 

@GFC_ you need to look at how the html/js works for those pages and figure out what's the better solution for them, this is a simpler, but more annoying solution in the long run. Another possible solution would be to use CUPS to maintain a list of those printers and do the interfacing with all those brands and makes for you, then base your script on it's API (I bet you can retrieve all those metrics from CUPS), this would minimize your script to just calling a single API and making the report, no multiple model handling necessary.

Link to post
Share on other sites

well if you want to input the values into an excel sheet you could try the VB scripting included in any of the MS Office products. (I know, Visual Basic, kill me now).

 

The theory would be simple, you need to make an http request to the ip address of the printer, as a response, you will get an html document, you don't need to display it, you just need to parse the document for the value you want.

The best way to measure the quality of a piece of code is "Oh F*** "s per line

Link to post
Share on other sites

3 hours ago, DevBlox said:

I personally find it way easier to call an API. Though my statement is based on some assumptions, mainly that universities/colleges may very well buy printers in bulk, so they may be the same model. But even though they may be of different makes and models, having tailored a ton of regex'es or listing a ton of endpoints for all models is not that different anyway. IMO when using an API the script is more likely to survive a firmware update of the printers than a regex check, it could also protect against a more horrible administering problem - when only part of printers have their firmware updated, one part may have an updated GUI, another part may not. It becomes a less brittle solution, I bet @GFC_ would like to spend as less time as possible maintaining that thing.

Consider having to write a range of very different interface code around those APIs (never make assumptions in the Software Industry - especially when dealing with hardware interfacing); on top of completely different APIs one may also have to deal with different hand shaking and authentication as well as what to do if the API is simply not meant to be accessible (obfuscated/encrypted call tokenization/sessions) or if it is but it's as low level as talking in bits over UDP/TCP - where will you get the documentation for that from!

 

Moverover how would you extend/maintain it once it had been written? Unless you made it a pluggable architecture you'd be left with having to recompile it every time you extended or updated it. Whereas with the other solution all you'd have to do would be to add or update a bit of regex in a text file...

3 hours ago, DevBlox said:

@GFC_ you need to look at how the html/js works for those pages and figure out what's the better solution for them, this is a simpler, but more annoying solution in the long run.

Based on the points above I'd consider the API binding approach to be the least flexible, most brittle and generally quite unwieldy to be honest. To much engineering for a relatively simple problem that could be banged out in about 10 to 20 minutes if that.

3 hours ago, DevBlox said:

Another possible solution would be to use CUPS to maintain a list of those printers and do the interfacing with all those brands and makes for you, then base your script on it's API (I bet you can retrieve all those metrics from CUPS), this would minimize your script to just calling a single API and making the report, no multiple model handling necessary.

This would be a better and cleaner approach however I'd still argue that once again it's over engineering the solution.

1 hour ago, espurritado said:

well if you want to input the values into an excel sheet you could try the VB scripting included in any of the MS Office products. (I know, Visual Basic, kill me now).

 

The theory would be simple, you need to make an http request to the ip address of the printer, as a response, you will get an html document, you don't need to display it, you just need to parse the document for the value you want.

You could but it would also be just as effective to have something spit out a CSV file. That way you are not so tightly coupled to Excel and the output format can also be abstracted.

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

no one like my javascript idea then :(

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

18 minutes ago, vorticalbox said:

no one like my javascript idea then :(

The principle behind it is sound although I'd personally choose to implement it in something other than JavaScript. Python perhaps.

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

13 minutes ago, Nuluvius said:

The principle behind it is sound although I'd personally choose to implement it in something other than JavaScript. Python perhaps.

I have been using JavaScript quite a bit recently got it on the brain :P python is next for me to learn.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×