Link to home
Start Free TrialLog in
Avatar of tsukraw
tsukraw

asked on

Script to collect page counts?

Hey experts.
I am trying to create some kind of script that would collect page counts from HP network printers using snmp.  I can use the (snmpget host .1.3.6.1.2.1.43.10.2.1.4.1.1) and get a page counts but what i would really like is to make a txt file with IP addresses of the multiple printers.  The script would access the txt of IPs, communicate with those devices pulling there page counts, then it would create a results txt file showing the IP address along with its page count.  

Is this possible?
Avatar of Bill Prew
Bill Prew

I might be able to help, but not sure.  Did you have a technique in mind for "communicate with those devices pulling there page counts"?

~bp
Avatar of tsukraw

ASKER

SNMP.
I am no expert but i could open a SNMP connection to the devices and see the page count.
So are you thinking some form of SNMPGET, or something else.  (sorry, not an SNMP expert).

~bp
Avatar of tsukraw

ASKER

Yea like i said above.  I was told i can use (snmpget host .1.3.6.1.2.1.43.10.2.1.4.1.1) i used GetIF and found that   .1.3.6.1.2.1.43.10.2.1.4.1.1 and that is the PageCount number.  How do i go about writing a script that would use a txt file with a list of IPs. and have it write a Txt file with the IPs page counts.
Do you have a working copy of SNMPGET for windows, and if so can you share the output of a single command?

~bp
Avatar of tsukraw

ASKER

(command i ran 10.0.0.6 is the IP of printer) snmpget  -v 1 -c public 10.0.0.6 .1.3.6.1.2.1.43.10.2.1.4.1.1

Cannot find module (IP-MIB): At line 0 in (none)
Cannot find module (IF-MIB): At line 0 in (none)
Cannot find module (TCP-MIB): At line 0 in (none)
Cannot find module (UDP-MIB): At line 0 in (none)
Cannot find module (HOST-RESOURCES-MIB): At line 0 in (none)
Cannot find module (SNMPv2-MIB): At line 0 in (none)
Cannot find module (SNMPv2-SMI): At line 0 in (none)
Cannot find module (NOTIFICATION-LOG-MIB): At line 0 in (none)
Cannot find module (UCD-SNMP-MIB): At line 0 in (none)
Cannot find module (UCD-DEMO-MIB): At line 0 in (none)
Cannot find module (SNMP-TARGET-MIB): At line 0 in (none)
Cannot find module (NET-SNMP-AGENT-MIB): At line 0 in (none)
Cannot find module (DISMAN-EVENT-MIB): At line 0 in (none)
Cannot find module (SNMP-VIEW-BASED-ACM-MIB): At line 0 in (none)
Cannot find module (SNMP-COMMUNITY-MIB): At line 0 in (none)
Cannot find module (UCD-DLMOD-MIB): At line 0 in (none)
Cannot find module (SNMP-FRAMEWORK-MIB): At line 0 in (none)
Cannot find module (SNMP-MPD-MIB): At line 0 in (none)
Cannot find module (SNMP-USER-BASED-SM-MIB): At line 0 in (none)
Cannot find module (SNMP-NOTIFICATION-MIB): At line 0 in (none)
Cannot find module (SNMPv2-TM): At line 0 in (none)
iso.3.6.1.2.1.43.10.2.1.4.1.1 = Counter32: 306135

The current page count is 306135 that is correct!!
Ah, so those "Cannot find module" are "normal".  I can reproduce that with a printer here, so I can create a batch file to do what you want.  For the output file, do you just want the IP address, a few spaces, and then the numeric count value?

~bp
Avatar of tsukraw

ASKER

That would be awesome.  I have no idea what the cannot find module means.  Are you thinking a script that would access a txt file with the IPs and substitute them in one at a time and run the command?  

If you got something in mind could you email me so i can explain situation more directly?  Your help so far has been fantastic
tsukraw@gmail.com
Yes, we can have a text file of IPs, one per line, lets call it printers.txt.  I'll create a batch file for you, lets call it getpages.bat.  Then you can run the script like this:

getpages printers.txt report.txt

And it will do a snmpget to each printer, extract the count from the result, and write a line to report.txt for each IP with the count next to it.

We could even make the OID another parm to the batch file if that made sense, but lets see if we can get it working this way first.

~bp
Okay, here's version 1 of a solution, hope it's close.  I still have to work out suppressing all those warning messages to the console screen, but it isn't affecting the script, and it's getting late here, so wanted to share this much.

Run it as I mentioned above and see what you think.  I think snmpget will need to be in your path.  I'll add comments tomorrow once I get some feedback on how close we are.

~bp
@echo off
setlocal EnableDelayedExpansion
setlocal EnableExtensions
 
if exist "%~f2" del "%~f2"
echo Printer              Page Count>>"%~f2"
echo -----------------  ------------>>"%~f2"
 
for /F "tokens=1" %%a in ('type "%~f1"') do (
  for /F "tokens=4" %%b in ('snmpget  -v 1 -c public %%a .1.3.6.1.2.1.43.10.2.1.4.1.1 ^| find "Counter32"') do (
    set IP=%%a            PAD
    set Count=PAD            %%b
    echo !IP:~0,18! !Count:~-12!>>"%~f2"
  )
)

Open in new window

Avatar of tsukraw

ASKER

There are a few OIDs i would like to get also...Maybe they can be added to the same line just add another  snmpget?  I would like to add the OID host name and Serial number.

Serial = .1.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0
HostName= .1.3.6.1.2.1.1.5.0
Avatar of tsukraw

ASKER

BillPrew
I would like to keep this form going so others can see our end result.  Can you please contact me via email as there is company information i would not like to post on here but would like to add to the script.  

tsukraw@gmail.com

Okay, here is a version with all three data elements gathered, see what you think.

I may rewrite it a bit to use a subroutine or two, but I figured I'd start with this approach and see if this looked right.

~bp
@echo off
setlocal EnableDelayedExpansion
setlocal EnableExtensions
 
REM For reference, here are the MIBS involved, and the resulting output from snmpget
REM Pages .1.3.6.1.2.1.43.10.2.1.4.1.1
REM   iso.3.6.1.2.1.43.10.2.1.4.1.1 = Counter32: 134888
REM Serial .1.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0
REM   iso.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0 = Hex-STRING: 01 15 55 53 52 47 59 31 34 31 39 32
REM Host .1.3.6.1.2.1.1.5.0
REM   iso.3.6.1.2.1.1.5.0 = STRING: "NPIB2CC9B"
 
REM Set up varialble with 40 spaces in it for formatting
set Spaces="                                        "
set Spaces=%Spaces:"=%
 
REM Remove output file if it exists, and display column headers one time
if exist "%~f2" del "%~f2"
echo Printer IP         Host                 Serial                           Page Count>>"%~f2"
echo ------------------ -------------------- ------------------------------ ------------>>"%~f2"
 
REM Loop through all printer IPs in the input file
for /F "tokens=1" %%a in ('type "%~f1"') do (
  REM Format the IP address for display (pad right to 18 characters, left justify)
  set IP=%%a%Spaces%
  set IP=!IP:~0,18!
 
  REM Get Host
  for /F "tokens=3*" %%b in ('snmpget  -v 1 -c public %%a .1.3.6.1.2.1.1.5.0 2^>nul ^| find "STRING:"') do (
    REM Format the Host name for display (remove quotes, pad right to 20 characters, left justify)
    set Host=%%c
    set Host=!Host:"=!%Spaces%
    set Host=!Host:~0,20!
  )
 
  REM Get Serial
  for /F "tokens=3*" %%b in ('snmpget  -v 1 -c public %%a .1.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0 2^>nul ^| find "Hex-STRING:"') do (
    REM Format the Serial for display (remove spaces, pad right to 30 characters, left justify)
    set Serial=%%c
    set Serial=!Serial: =!%Spaces%
    set Serial=!Serial:~0,30!
  )
 
  REM Get Pages
  for /F "tokens=3*" %%b in ('snmpget  -v 1 -c public %%a .1.3.6.1.2.1.43.10.2.1.4.1.1 2^>nul ^| find "Counter32:"') do (
    REM Format the Pages for display (pad left to 12 characters, right justify)
    set Pages=%Spaces%%%c
    set Pages=!Pages:~-12!
  )
 
  REM Output the line with all data for this printer
  echo !IP! !Host! !Serial! !Pages!>>"%~f2"
)

Open in new window

Avatar of tsukraw

ASKER

cool. Is it possible instead of a txt file have a comma delimited file created that I could import into excel or access? Also can a date/time  that shows when the script ran.  How about a line that shows IPs that are offline and could not report?
See what you think of this.

~bp
@echo off
setlocal EnableDelayedExpansion
setlocal EnableExtensions
 
REM For reference, here are the MIBS involved, and the resulting output from snmpget
REM Pages .1.3.6.1.2.1.43.10.2.1.4.1.1
REM   iso.3.6.1.2.1.43.10.2.1.4.1.1 = Counter32: 134888
REM Serial .1.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0
REM   iso.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0 = Hex-STRING: 01 15 55 53 52 47 59 31 34 31 39 32
REM Host .1.3.6.1.2.1.1.5.0
REM   iso.3.6.1.2.1.1.5.0 = STRING: "NPIB2CC9B"
REM Timeout: No Response from 192.168.59.232.
 
REM Remove output file if it exists, and display column headers one time
if exist "%~f2" del "%~f2"
echo Printer_IP,Host,Serial,Page_Count>>"%~f2"
 
REM Loop through all printer IPs in the input file
for /F "tokens=1" %%a in ('type "%~f1"') do (
  REM Get Host
  call :DoSnmpGet "%%a" ".1.3.6.1.2.1.1.5.0" "Host"
  set Host=!Host:"=!
 
  REM Get Serial
  call :DoSnmpGet "%%a" ".1.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0" "Serial"
  set Serial=!Serial: =!
 
  REM Get Pages
  call :DoSnmpGet "%%a" ".1.3.6.1.2.1.43.10.2.1.4.1.1" "Pages"
 
  REM Output the line with all data for this printer
  echo %%a,!Host!,!Serial!,!Pages!>>"%~f2"
)
goto :EOF
 
:DoSnmpGet
  set %~3=N/A
  for /F "tokens=1-3*" %%b in ('%cd%\snmpget -v 1 -c public %~1 %~2 2^>nul') do (
    set %~3=%%e
  )
  goto :EOF

Open in new window

Avatar of tsukraw

ASKER

Very nice. I did chance one thing. O added -O T to the snmpget.  The serial is showing up with a hex string and the actual serial?  Is there anyway to remove the HexString?

  REM Get Serial
  for /F "tokens=3*" %%b in ('snmpget -O T -v 1 -c public %%a .1.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0 2^>nul ^| find "Hex-STRING:"') do (
    REM Format the Serial for display (remove spaces, pad right to 30 characters, left justify)
    set Serial=%%c
    set Serial=!Serial: =!%Spaces%
    set Serial=!Serial:~0,40!

Example:
192.168.2.230      NPI92CE18            0115434E434336444D323359[..CNCA0DM00Y]          32170
Avatar of tsukraw

ASKER

When i run the last file you posted i get
IP ADDRESS,NA,NA,NA

I have tested it with more then one IP
>> When i run the last file you posted i get
>> IP ADDRESS,NA,NA,NA

That's odd, I run it here with 3 good ip's and one bad, and the first three have data, and the last only shows the N/As, meaning the data couldn't be accessed.  

Oh wait though, I didn't add the options you said you added yet, that may be related.  Where are those options documented, is there a web reference someplace for SNMPGET?

~bp
Avatar of tsukraw

ASKER

I looked at the snmpget -h it showed the commands.

I got another entire part to this for the script it you would be interested in taking a look?
This is getting  to be quite a bit of work on your part i hope you dont mind.  
Sure, pass it along.  Can't promise anything but happy to take a look.

~bp
Avatar of tsukraw

ASKER

The end goal of this script is to schedule it to run on a computer.  the computer generates txt comma delimited file.  that file is then sent via FTP to my server.

In the Printers.txt file is it possible to add a entry like this?  The machine ID would be a name/number i associate to the machine.  Then in the comma delimited file would it be possible to have a fiield added for the machineID?
Example: MachineID,IP Address,Data/Time,HostName,Serial,PageCount,

10.0.0.6 , MachineID
10.0.0.7 , MachineID

I also added a file called config.txt in this file i would like to have it look like this or something of the such.

FTPaddress:ftp.domain.com
FTPuser:BOB
FTPpass:bobpass
ClientID:123456

I would like it so that once it collects the info the script access the config.txt and pulls the ftp address,  username, pass and is able to upload the result file.  Is is possible to have it create a file name for the results file something like (clintID)_Data.txt Pullling the client ID from the config.txt file?

Avatar of tsukraw

ASKER

could the ClientID almost be added to a filed in the comma delimited file?

Example: ClientID,MachineID,IP Address,Data/Time,HostName,Serial,PageCount,
Have you tried adding the -O T option to my more recent version, using the DoSnmpGet routine?  It seems to work here.

~bp
Avatar of tsukraw

ASKER

I have tested the last code provided and it is still failing with all NA
If i have multiple IPs it only shows the first one in the list.
Avatar of tsukraw

ASKER

Hmm i ran it on another computer and it worked so scratch that not working.
Avatar of tsukraw

ASKER

Here is the result file.  Is there any way to remove the HexString from it so i only get the Serial? The JPEL00N076 part?

192.168.0.210,NPI7E1FF0,01154A50454C36384E303035[..JPEL00N076],104905
This takes care of the MachineID (add it after a comma in the IP file), and the serial number in text.

I'll think about the ftp part, let me know if this looks okay for the report.

It is possible to do basic FTP xfers right from batch.  Would you want this all in one longer batch file?

~bp
Avatar of tsukraw

ASKER

Yea that is what i was hoping to do is just use the simple FTP from batch file....It is up to you on if it is one big. The goal is to have one batch file that i can run that will access the printers.txt list, create the report, and ftp it to the server.
Avatar of tsukraw

ASKER

Were you going to upload a new copy?
Okay, I'll see what I can work up.  Let me know how the last version looks.

~bp
Oops, yes, in all the typing forgot to add the script...

~bp
@echo off
setlocal EnableDelayedExpansion
setlocal EnableExtensions
 
REM For reference, here are the MIBS involved, and the resulting output from snmpget
REM Pages .1.3.6.1.2.1.43.10.2.1.4.1.1
REM   iso.3.6.1.2.1.43.10.2.1.4.1.1 = Counter32: 134888
REM Serial .1.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0
REM   iso.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0 = Hex-STRING: 01 15 55 53 52 47 59 31 34 31 39 32
REM Host .1.3.6.1.2.1.1.5.0
REM   iso.3.6.1.2.1.1.5.0 = STRING: "NPIB2CC9B"
REM Timeout: No Response from 192.168.59.232.
 
REM Remove output file if it exists, and display column headers one time
if exist "%~f2" del "%~f2"
echo Machine_ID,Printer_IP,Host,Serial,Page_Count>>"%~f2"
 
REM Loop through all printer IPs in the input file
for /F "tokens=1,2 delims=," %%a in ('type "%~f1"') do (
  REM Get Host
  call :DoSnmpGet "%%a" ".1.3.6.1.2.1.1.5.0" "Host"
  set Host=!Host:"=!
 
  REM Get Serial
  call :DoSnmpGet "%%a" ".1.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0" "Serial"
  set Serial=!Serial: =!
  for /F "tokens=2 delims=[.]" %%c in ("!Serial!") do set Serial=%%c
 
  REM Get Pages
  call :DoSnmpGet "%%a" ".1.3.6.1.2.1.43.10.2.1.4.1.1" "Pages"
 
  REM Output the line with all data for this printer
  echo %%b,%%a,!Host!,!Serial!,!Pages!>>"%~f2"
)
goto :EOF
 
:DoSnmpGet
  set %~3=N/A
  REM echo snmpget -O T -v 1 -c public %~1 %~2 2^>nul
  for /F "tokens=1-3*" %%t in ('%cd%\snmpget -O T -v 1 -c public %~1 %~2 2^>nul') do (
    set %~3=%%w
  )
  goto :EOF

Open in new window

One small change you need to make.  Remove the following from before the smnpget command, I keep the program in the same folder as the script so I add this in to find it in the current directory.

%cd%\

~bp
Avatar of tsukraw

ASKER

You are amazing!
Is it possible to add if there is no MachineID  to say NA like the other fields?
Avatar of tsukraw

ASKER

I as well have the snmpget.exe in the same directory as the scripts should i remove the
%cd%\ still or should i leave it in place?
Yes on NA for machine id.

I'd say take the %cd%\ out since it has been running for you without it.

~bp
With N/A for MachineID.

~bp
@echo off
setlocal EnableDelayedExpansion
setlocal EnableExtensions
 
REM For reference, here are the MIBS involved, and the resulting output from snmpget
REM Pages .1.3.6.1.2.1.43.10.2.1.4.1.1
REM   iso.3.6.1.2.1.43.10.2.1.4.1.1 = Counter32: 134888
REM Serial .1.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0
REM   iso.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0 = Hex-STRING: 01 15 55 53 52 47 59 31 34 31 39 32
REM Host .1.3.6.1.2.1.1.5.0
REM   iso.3.6.1.2.1.1.5.0 = STRING: "NPIB2CC9B"
REM Timeout: No Response from 192.168.59.232.
 
REM Remove output file if it exists, and display column headers one time
if exist "%~f2" del "%~f2"
echo Machine_ID,Printer_IP,Host,Serial,Page_Count>>"%~f2"
 
REM Loop through all printer IPs in the input file
for /F "tokens=1,2 delims=," %%a in ('type "%~f1"') do (
  REM Get Machine ID
  if "%%b"=="" (set MachineID=N/A) else (set MachineID=%%b)
 
  REM Get Host
  call :DoSnmpGet "%%a" ".1.3.6.1.2.1.1.5.0" "Host"
  set Host=!Host:"=!
 
  REM Get Serial
  call :DoSnmpGet "%%a" ".1.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0" "Serial"
  set Serial=!Serial: =!
  for /F "tokens=2 delims=[.]" %%c in ("!Serial!") do set Serial=%%c
 
  REM Get Pages
  call :DoSnmpGet "%%a" ".1.3.6.1.2.1.43.10.2.1.4.1.1" "Pages"
 
  REM Output the line with all data for this printer
  echo !MachineID!,%%a,!Host!,!Serial!,!Pages!>>"%~f2"
)
goto :EOF
 
:DoSnmpGet
  set %~3=N/A
  for /F "tokens=1-3*" %%t in ('snmpget -O T -v 1 -c public %~1 %~2 2^>nul') do (
    set %~3=%%w
  )
  goto :EOF

Open in new window

Avatar of tsukraw

ASKER

Can you add 2 more OIDs?


.1.3.6.1.4.1.11.2.3.9.4.2.1.4.1.2.7.0  - Color Page Count
.1.3.6.1.2.1.25.3.2.1.3.1 - Device Model
Okay, last one for tonight...

~bp
@echo off
setlocal EnableDelayedExpansion
setlocal EnableExtensions
 
REM For reference, here are the MIBS involved, and the resulting output from snmpget
REM Pages .1.3.6.1.2.1.43.10.2.1.4.1.1
REM   iso.3.6.1.2.1.43.10.2.1.4.1.1 = Counter32: 134888
REM Serial .1.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0
REM   iso.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0 = Hex-STRING: 01 15 55 53 52 47 59 31 34 31 39 32
REM Host .1.3.6.1.2.1.1.5.0
REM   iso.3.6.1.2.1.1.5.0 = STRING: "NPIB2CC9B"
REM Timeout: No Response from 192.168.59.232.
REM Color Page Count .1.3.6.1.4.1.11.2.3.9.4.2.1.4.1.2.7.0
REM Device Model .1.3.6.1.2.1.25.3.2.1.3.1
 
REM Remove output file if it exists, and display column headers one time
if exist "%~f2" del "%~f2"
echo Machine_ID,Printer_IP,Host,Serial,Page_Count,Color_Pages,Device_Model>>"%~f2"
 
REM Loop through all printer IPs in the input file
for /F "tokens=1,2 delims=," %%a in ('type "%~f1"') do (
  REM Get Machine ID
  if "%%b"=="" (set MachineID=N/A) else (set MachineID=%%b)
 
  REM Get Host
  call :DoSnmpGet "%%a" ".1.3.6.1.2.1.1.5.0" "Host"
  set Host=!Host:"=!
 
  REM Get Serial
  call :DoSnmpGet "%%a" ".1.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0" "Serial"
  set Serial=!Serial: =!
  for /F "tokens=2 delims=[.]" %%c in ("!Serial!") do set Serial=%%c
 
  REM Get Pages
  call :DoSnmpGet "%%a" ".1.3.6.1.2.1.43.10.2.1.4.1.1" "Pages"
 
  REM Get Color Pages
  call :DoSnmpGet "%%a" ".1.3.6.1.4.1.11.2.3.9.4.2.1.4.1.2.7.0" "ColorPages"
 
  REM Get Device Model
  call :DoSnmpGet "%%a" ".1.3.6.1.2.1.25.3.2.1.3.1" "DeviceModel"
 
  REM Output the line with all data for this printer
  echo !MachineID!,%%a,!Host!,!Serial!,!Pages!,!ColorPages!,!DeviceModel!>>"%~f2"
)
goto :EOF
 
:DoSnmpGet
  set %~3=N/A
  for /F "tokens=1-3*" %%t in ('snmpget -O T -v 1 -c public %~1 %~2 2^>nul') do (
    set %~3=%%w
  )
  goto :EOF

Open in new window

Avatar of tsukraw

ASKER

I really never knew so much was possible with scripts this is amazing! This is exactly what i am looking for, now if it is able to FTP and use a config.txt file for the FTP parameters it will be complete!!

THANK YOU!
I will say this is the best experts-exchange experience i have ever had.
Avatar of tsukraw

ASKER

What do you think about adding this as well?
Max Toner Level = 1.3.6.1.2.1.43.11.1.1.8.1.1
Current Toner Level = 1.3.6.1.2.1.43.11.1.1.9.1.1

toner level percentage is determined by dividing the result of current toner level by the max tuner level.  Is there a way to make it so it would look up the value of max and current.  Divide the 2 numbers and put the result in the output file as a %

That might be stretching the abilities of s script or maybe you will be able to impress me farther?
Avatar of tsukraw

ASKER

I made one change.  I decided that the HostName wasnt so much needed so i changed it to Installed Toner.
 .1.3.6.1.2.1.43.11.1.1.6.1.1
Been away from the computer today (training) but will try and poke a bit tonight.  

I haven't thought this through yet, but maybe it makes sense to pull the MIBS and item name from a file also, to allow easy future changes or additions?  Not sure how hard that would be, the basic idea is easy, the only problem comes in with some of the items that need special treatment of the results SNMPGET returns, like the serial number where we then went on to strip hex representation out.  So maybe that won't work well.

I'll also need to set up an FTP server on my desktop at home so that I can test out the ftp put logic, put I think that's fairly doable.

~bp
Avatar of tsukraw

ASKER

Yea what ever you think is best.  Once this is finished i dont think there will be much change needed to it. Was it going to be possible to add a date field that woudl show the time the script was ran?

Also was it going to be possible to use that config.txt file and have it name the file CLIENTID_date.txt?

Thank you for all your work!
>> The end goal of this script is to schedule it to run on a computer.  >> the computer generates txt comma delimited file.  that file is then >> sent via FTP to my server.

I think I understand this concept.

>> In the Printers.txt file is it possible to add a entry like this?  >> The machine ID would be a name/number i associate to the machine.  >> Then in the comma delimited file would it be possible to have a >> fiield added for the machineID?>> Example: MachineID,IP Address,Data/Time,HostName,Serial,PageCount,>> 10.0.0.6 , MachineID>> 10.0.0.7 , MachineID

This change is in the last version.

>> I also added a file called config.txt in this file i would like to >> have it look like this or something of the such.>> FTPaddress:ftp.domain.com>> FTPuser:BOB>> FTPpass:bobpass>> ClientID:123456

This can work fine as long as the format stays like this.  It looks like
none of the field names (FPuser, etc) would ever have spaces in them, as
well as any of the values.  It's a little easier to code if we assume we
don't have to handle embedded spaces in those, but ant to see if you
agree with that assumption.

>> I would like it so that once it collects the info the script access >> the config.txt and pulls the ftp address,  username, pass and is able >> to upload the result file.  Is is possible to have it create a file >> name for the results file something like (clintID)_Data.txt Pullling >> the client ID from the config.txt file?

A couple of typos in your text, but I think you are asking for the output
file from the SNMPGET querries to go to a file with the ClientID from
the config.txt file, as well as the current date.  That's all doable.
What format would you want the date in for the filename?  Could be anything
but I often like to go with YYYYMMDD or YYYY-MM-DD for this sort of
thing so that the files can easily be sorted chronologically.  Let me
know your thoughts on this.

~bp
Avatar of tsukraw

ASKER

Correct if it makes it easier for you there will be NO spaces in the config.txt file

How ever you would like to do the date is fine with me the YYYMMDD is good for me!!
On a couple of pronters here, I'm seeing some negative values for some of the recent values you gave me, does this make sense to you?

~bp
Host
iso.3.6.1.2.1.1.5.0 = STRING: "NPIB2CC9B"
iso.3.6.1.2.1.1.5.0 = STRING: "RCH03-LZR-02"
iso.3.6.1.2.1.1.5.0 = STRING: "NPI281B7C"
Serial
iso.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0 = Hex-STRING: 01 15 55 53 52 47 59 31 34 31 39 32   [..USRGY14192]
iso.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0 = Hex-STRING: 01 15 4A 50 48 41 44 31 30 33 34 34   [..JPHAD10344]
iso.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0 = Hex-STRING: 01 15 55 53 43 31 30 33 36 35 39 37   [..USC1036597]
Pages
iso.3.6.1.2.1.43.10.2.1.4.1.1 = Counter32: 135012
iso.3.6.1.2.1.43.10.2.1.4.1.1 = Counter32: 55481
iso.3.6.1.2.1.43.10.2.1.4.1.1 = Counter32: 421710
Color Pages
iso.3.6.1.2.1.1.5.0 = STRING: "NPIB2CC9B"
iso.3.6.1.2.1.1.5.0 = STRING: "RCH03-LZR-02"
iso.3.6.1.2.1.1.5.0 = STRING: "NPI281B7C"
Device Model
iso.3.6.1.2.1.25.3.2.1.3.1 = STRING: "HP LaserJet 4100 MFP"
iso.3.6.1.2.1.25.3.2.1.3.1 = STRING: "HP Color LaserJet 4500"
iso.3.6.1.2.1.25.3.2.1.3.1 = STRING: "HP LaserJet 5000 Series"
Installed Toner
iso.3.6.1.2.1.43.11.1.1.6.1.1 = STRING: "Toner Cartridge HP C8061A"
iso.3.6.1.2.1.43.11.1.1.6.1.1 = STRING: "Black Toner Cartridge HP C4191A"
iso.3.6.1.2.1.43.11.1.1.6.1.1 = STRING: "Toner Cartridge HP C4129X"
Max Toner Level
iso.3.6.1.2.1.43.11.1.1.8.1.1 = INTEGER: 2700
iso.3.6.1.2.1.43.11.1.1.8.1.1 = INTEGER: -2
iso.3.6.1.2.1.43.11.1.1.8.1.1 = INTEGER: -2
Current Toner Level
iso.3.6.1.2.1.43.11.1.1.9.1.1 = INTEGER: 2295
iso.3.6.1.2.1.43.11.1.1.9.1.1 = INTEGER: -3
iso.3.6.1.2.1.43.11.1.1.9.1.1 = INTEGER: -3

Open in new window

Avatar of tsukraw

ASKER

Hmm color pages does not look right at all!  I am guessing i got the wrong MIB.I dont have any color devices available to me right now to test the MIB myself.
I believe the correct one is 1.3.6.1.4.1.11.2.3.9.4.2.1.4.1.2.7 for collor page count?  Are you able to check it with your 4500?


With the toner level are you able to have the current devided by the max to get a % remaining?
You can take host name out of there if you like i dont think there is a need for that anymore now since you added the MachineID.

The Toner was to be able to see the HP toner number that is installed.
Yes, we can do division to calc percentage once we have the right MIBS.

My bad on the color pages count, a cut and past error.  Using .1.3.6.1.4.1.11.2.3.9.4.2.1.4.1.2.7.0 did give:

Color Pages
iso.3.6.1.4.1.11.2.3.9.4.2.1.4.1.2.7.0 = INTEGER: 41481

For the toner level numbers, the -2 and -3 make sense?

~bp
Okay, here's the next version, it has everything except the FTP xfer logic in it yet.

You run it a little differently now, the batch file takes 2 parms when run, the first is the name of the printers.txt file, the second is the name of the new config.txt file (same format as you specified).

The output goes into a file named ClientID_YYYYMMDD.txt

I'll start working the FTP stuff tonight and tomorrow but wanted to get you this much for checking.

~bp
Sorry, forgot the code...

~bp
@echo off
setlocal EnableDelayedExpansion
setlocal EnableExtensions
 
REM Grap parms from command line and store them
set PrinterFile=%~f1
set ConfigFile=%~f2
 
REM Load config file information
call :DoConfig "%ConfigFile%"
echo FTPhost=[%FTPhost%]
echo FTPuser=[%FTPuser%]
echo FTPpass=[%FTPpass%]
echo ClientID=[%ClientID%]
 
REM Remove output file if it exists, and display column headers one time
set OutputFile=%ClientID%_%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%.txt
if exist "%OutputFile%" del "%OutputFile%"
echo MachineID,PrinterIP,Host,Serial,PageCount,ColorPages,DeviceModel,InstalledToner,Max_TonerLevel,CurrentTonerLevel,TonerLevelPercent>>"%OutputFile%"
 
REM Loop through all printer IPs in the input file
for /F "tokens=1,2 delims=," %%a in ('type "%PrinterFile%"') do (
  REM Get Machine ID
  if "%%b"=="" (set MachineID=N/A) else (set MachineID=%%b)
 
  REM Get Host
  call :DoSnmpGet "%%a" ".1.3.6.1.2.1.1.5.0" "Host"
  set Host=!Host:"=!
 
  REM Get Serial
  call :DoSnmpGet "%%a" ".1.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0" "Serial"
  set Serial=!Serial: =!
  for /F "tokens=2 delims=[.]" %%c in ("!Serial!") do set Serial=%%c
 
  REM Get Pages
  call :DoSnmpGet "%%a" ".1.3.6.1.2.1.43.10.2.1.4.1.1" "Pages"
 
  REM Get Color Pages
  call :DoSnmpGet "%%a" ".1.3.6.1.4.1.11.2.3.9.4.2.1.4.1.2.7.0" "ColorPages"
 
  REM Get Device Model
  call :DoSnmpGet "%%a" ".1.3.6.1.2.1.25.3.2.1.3.1" "DeviceModel"
 
  REM Get Installed Toner
  call :DoSnmpGet "%%a" ".1.3.6.1.2.1.43.11.1.1.6.1.1" "InstalledToner"
 
  REM Get Max Toner Level
  call :DoSnmpGet "%%a" ".1.3.6.1.2.1.43.11.1.1.8.1.1" "MaxTonerLevel"
 
  REM Get Current Toner Level
  call :DoSnmpGet "%%a" ".1.3.6.1.2.1.43.11.1.1.9.1.1" "CurrentTonerLevel"
 
  REM Calculate Toner Level Percent (to 2 decimal places)
  set TonerLevelPercent=N/A
  if NOT "!CurrentTonerLevel!"=="N/A" (
    if NOT "!MaxTonerLevel!"=="N/A" (
      set /A TonerLevelPercent = CurrentTonerLevel * 10000
      set /A TonerLevelPercent /= MaxTonerLevel
      set TonerLevelPercent=!TonerLevelPercent:~0,-2!.!TonerLevelPercent:~-2!
    )
  )
 
  REM Output the line with all data for this printer
  echo !MachineID!,%%a,!Host!,!Serial!,!Pages!,!ColorPages!,!DeviceModel!,!InstalledToner!,!MaxTonerLevel!,!CurrentTonerLevel!,!TonerLevelPercent!>>"%OutputFile%"
)
goto :EOF
 
:DoSnmpGet
  REM Use SNMPGET to fetch value for IP and MIB passed in, return in variable passed in
  set %~3=N/A
  for /F "tokens=1-3*" %%t in ('snmpget -O T -v 1 -c public %~1 %~2 2^>nul') do (
    set %~3=%%w
  )
  goto :EOF
 
:DoConfig
  REM Load settings from config file into variables with the setting name
  for /F "tokens=1* delims=:" %%c in ('type %1') do (
    set %%c=%%d
  )
  goto :EOF
 
:DoFtp
  REM cd \path\where\to\put\file
  REM (
  REM echo open host passwd
  REM echo cd /path/to/the/file
  REM echo get the_file
  REM echo bye
  REM )|ftp
  goto :EOF

Open in new window

Avatar of tsukraw

ASKER

Hmm i just tested it on my network.  My older devices are reporting like your -2 -3 is it possible to have it if there is a (-) value to have it just show NA.  That way the newer devices that can report toner level will and the older devices that report -2 and -3 will just show NA?
Yes, we could do that.

~bp
Avatar of tsukraw

ASKER

Actually if it can just report the current toner level and that max that is just fine!! I was thinking about it and i will just used a formula in excel to calculate it!.  I might actually prefer doing that way.
Avatar of tsukraw

ASKER

Or if the percent is easy for you to do that is cool with me either way works!
Avatar of tsukraw

ASKER

What do i need to run in order for it to access the config.txt file?

Currently i am running.
getpages printers.txt ClientName.txt
Do i need to add config.txt to that?
Just run it as:

getpages printer.txt config.txt

The output file is now named in the code as mentioned above.

~bp
Avatar of tsukraw

ASKER

When i run the latest script.  The filelds are showing up blank. like it is not accessing the config.txt file?
THe file name is (__20091120.txt)
Avatar of tsukraw

ASKER

Found my problem.
I had in the config.txt

FTPaddress it looks like you changed it to FTPhost. as soon as i changed my config.txt to FTPhost it pulled all the info correctly.
Avatar of tsukraw

ASKER

One thing i did notice with the last code.
If i have a device offline it seems to get hungup on it and just sits there.
Yes, sorry, should have mentioned that on the config file change to the FTPhost parm, glad you found that.

I think I'm pretty close on the FTP part.

On the offline printer, I have an invalid IP in my printers.txt for testing, and it always comes back with N/A for the data.  It does take it 30 or 60 seconds to timeout though, and that could be for each MIB so it can slow down the script.  Have you waited a fair amount of time?  Also, have you tried doing the SNMPGET from the command line for that printer and MIB and does it come back, or hang?

~bp
Avatar of tsukraw

ASKER

You are correct i was just not waiting long enough for it to time out.
Okay, this seems to handle the FTP now.  I made some slight code changes too.

Near the bottom I have left the delete of the FTP temporary file needed for the xfer commented out.  Once you know this all works fine you can remove that REM.

Hopefully this will all make sense, I tested it here and it it worked okay.

~bp
EE24909017.bat.txt
Avatar of tsukraw

ASKER

Is it posible to add a simple log.txt file that would be engerated one all the previous commands went through...Something that woudl show like

192.168.10.1-OK
192.168.10.2-NA
192.168.10.9-OK
FTP Connection-OK
FileSent-OK

Just showing which IPs it was able to connect to and if the FTP was successful?
Okay, added a little more error checking and the summary logging you mentioned.

Same command line to run it, but now it will write the info you asked for above to the screen.  If you want to capture it to a file just use redirection to a file, such as:

EE24909017 printers.txt config.txt >log.txt

It should recover from non responsive printers quicker now too.  If I do the first SNMPGET to a printer and don't get a reply I skip the other gets for that printer and set all data to N/A.

~bp
EE24909017.bat.txt
Avatar of tsukraw

ASKER

I added the >log.txt and i am receiving the following...

192.168.0.210 - OK
192.168.0.202 - OK
FTP logon - ERROR
FTP transfer - OK

But the FTP is working fine any idea why it would be showing a ERROR on the FTP logon?
Could be a difference in your client or server (FTP).  Can you REM out the line near the bottom that is

del "%~f1"

And then after you run it send me the ftp.log file.

~bp
Avatar of tsukraw

ASKER

you are correct it was different clients.  I changed the following 2 lines.  After changing them it worked correctly.  Is it supposed to delete the 123456_20091122.txt after it is FTPed?  If not is it possible to have this happens so we dont get a large collection of logs over time?



  find /I "230 Login OK" "%~f2">NUL
to
  find /I "230" "%~f2">NUL
---------------------------------------------------------------------------
  find /I "226 Transfer complete" "%~f2">NUL
to
  find /I "226" "%~f2">NUL

Avatar of tsukraw

ASKER

I am not sure if this is possible.  I have a Xerox machine that when it gets the printer name it returns the following.
Xerox Phaser 7500DT;OS8.80,PS3.2.0,Eng40.12.1,Net40.38.03.15

For some reason it has a strange name and it has a (,) comma in the name which is throwing the file off (comma delimited).  Is it possible to have it not allow commas to be written in any of the results to prevent it from messing up the comma delimited file?
Glad you found the FTP error code difference.

On the comma issue I think we have a few choices.

(1) We could change to a different delimiter in the report file, like ^ or something.

(2) We can not allow commas in the data fields and replace them with something else, like semicolon, etc.

(3) We can truncate the data field at the comma.

(4) We can just enclose all non numeric type fields in double quotes, which most programs like Excel will then allow commas inside the quotes.

Thoughts?

~bp
Avatar of tsukraw

ASKER

I am liking option 2 the most if we could just repace the comma with a semicolon that would work for me. Thoughs on the file deleting after successful ftp upload?
Just curious, why not #4, preserving the data fields as they are actually?  We can do #2, but curious on #4.  Are you not intending to use the data with a program that supports quoted fields in the CSV.

Yes, we can delete the "report" file at the end of the job in all cases.

~bp
Avatar of tsukraw

ASKER

I honestly dont know to much about comma delimited files.
So you are saying the line above would come through like....

,N/A,""Xerox Phaser 7500DT;OS8.80,PS3.2.0,Eng40.12.1,Net40.38.03.15"",N/A,
Avatar of tsukraw

ASKER

I just tried adding the "" to my results file and it imported just fine so yes solution 4 works for me.
Avatar of tsukraw

ASKER

Well i thought it was working.  
The Xerox returns the following.  I double quoted it where the actual data is supposed to be separated and it imported it as one entire line.  It really doesn't matter to me we can replace the (,) with a semicolon that wroks just as well for me.

""Phaser 7500DT,N/A,5679,N/A,"Xerox Phaser 7500DT;OS8.80,PS3.2.0,Eng40.12.1,Net40.38.03.15"",""Cyan Standard Capacity Toner Cartridge, Phaser 7500""
Try changing the utput line to the following and see how that goes.

~bp
echo "!MachineID!","%%a","!Host!","!Serial!",!Pages!,!ColorPages!,!DeviceModel!,!InstalledToner!,!MaxTonerLevel!,!CurrentTonerLevel!,!TonerLevelPercent!>>"%~f2"

Open in new window

Avatar of tsukraw

ASKER

Perfect worked great!

I know i had asked this before but can we add a Data field to the comma delimited so when it is imported into a database i can see the data on the record?

Avatar of tsukraw

ASKER

Date*
Yes on date, sorry if that slipped through the cracks.  If you do the following command at your command line, what are the results, does it look the same as my results below?

ECHO [%DATE%]

[Mon 11/23/2009]

If so, would you just want the MM/DD/YYYY part I assume?

~bp
Avatar of tsukraw

ASKER

Correct it shows just like yours.  Is it possible to have it in the format we have in the file name?

20091123
Okay, I've gotten that taken care of.

In adding a few more printers to my test here I noticed the following.  When I did the following command:

snmpget -O T -v 1 -c public 123.123.123.123 .1.3.6.1.2.1.43.11.1.1.6.1.1
I got a TWO line response from SNMPGET.  The way my script is written now it assumed only a one line output from SNMPGET.  Here's an example from the above command.

iso.3.6.1.2.1.43.11.1.1.6.1.1 = Hex-STRING: 42 6C 61 63 6B 20 43 61 72 74 72 69 64 67 65 20   [Black Cartridge ]48 50 20 43 42 33 39 30 41 00   [HP CB390A.]

My question is do we want just the first line, just the second line, or some combination of the two?  Right now the script will only pick up the last line if more than one line is returned.

I wonder if this MIB is the only place this might occur...

~bp
Avatar of tsukraw

ASKER

When i ran it it output "Black Cartridge HP Q5945A" which was just find for me it told the color of the toner and the HP part number.  I am how ever curious do you have a color printer?  The MIB for black appears to be cyan for a color device.  I am wondering if it is a color devices is there a way for the script to know that and if it is a color to run the other  3MIBs, Black/magenta/yellow?  Same with toner level if it is a color device it is reporting the cyan toner level for the current MIB, would it be able to report all 4 toner levels for color devices?

If I figure out the MIBs for color devices would you want to give it a try or is this getting to be a lot for a script to do?
This showed up on two color printers, models:

HP Color LaserJet CM3530 MFP
HP Color LaserJet CM6040 MFP

It appears to be reporting the black toner correctly (CB390A), but just formatting it differently than say the HP Color LaserJet 4500 did, where it returns the following all on one line:

Black Toner Cartridge HP C4191A

So I don't think it's really a color versus B/W issue, but rather different printers choosing to send back a line feed character in the middle.

I'll try one other thing but I'm not sure I can bite off the color versus not color logic right now.  While I have enjoyed this little project, we have to draw the line at some point.  

~bp
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tsukraw

ASKER

Would you like to undertake another script?
Sure, I'd at least like to hear about the problem to be solved.

You should have my email (I had sent an email to you earlier in this exchange) if you want to send it along that way, or add it to this question as a comment...

~bp
Avatar of tsukraw

ASKER

Could you please email me again?  I didnt see it come through unless it got flagged for spam which  i get so much spam i go through it just delete it normally.