Page 1 of 2

Printer IP Finder

Posted: 04 Apr 2016 02:39
by Olyrd
Hello.

I have the following code which goes through several registry keys and outputs all vales and their corresponding data to a text file. The keys relate to known printer settings location which enumerates stuff like printer IP, printer port, printer name etc. So the script must go through all of them.

Code: Select all

pushd %~dp0
ver | find "6.1" > nul
if %errorlevel%==0 goto win7

:win8
reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\SWD\DAFWSDProvider /s >>_%computername%_printers.txt
reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print /s >>_%computername%_printers.txt
reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print /s >>_%computername%_printers.txt
reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Control\Print /s >>_%computername%_printers.txt
reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print /s >>_%computername%_printers.txt
goto exit

:win7
reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum /s >>_%computername%_printers.txt
reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print /s >>_%computername%_printers.txt
reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print /s >>_%computername%_printers.txt
reg query HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Control\Print /s >>_%computername%_printers.txt
reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print /s >>_%computername%_printers.txt

:exit
exit


The link below is a snippet of the output. The original output contains from 2.000 to 5.000 lines.
http://pastebin.com/gMSudq66

What I would like is to extract is the IP address and the name (if possible) from the output file and append them to the top of the output file, keeping the data already in it. Due to the fact that each manufacturer stores the IP and name differently, some may use the FriendlyName value others just use the Name value. Some might have the name and IP in different keys.
The IP addresses range from Class C to Class B and even Class A.

Thank you for your time and patience.

Re: Printer IP Finder

Posted: 04 Apr 2016 02:54
by ShadowThief
Without consistent output, this isn't even remotely feasible.

Re: Printer IP Finder

Posted: 04 Apr 2016 03:08
by Olyrd
Sadly, I know.
What about the IP address? Would that be at least possible to get via a regex?

Re: Printer IP Finder

Posted: 04 Apr 2016 03:32
by ShadowThief
Yeah, that's fairly straightforward. Or at least, the example you've provided only shows IP addresses in the same lines as the word "LocationInformation" so you can just search for that.

Code: Select all

for /f "tokens=3" %%A in ('findstr /C:"LocationInformation" printerOut.txt') do echo %%A

Re: Printer IP Finder

Posted: 04 Apr 2016 03:44
by Olyrd
In this example yes, but some obscure printers don't use that. Could be done to just search for any number string which contains 4 dots, say XXX.XX.XXX.X, XXX.XXX.X.XXX, etc.

Re: Printer IP Finder

Posted: 04 Apr 2016 12:56
by Squashman
Are you looking for something like this.

Printer.vbs

Code: Select all

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections

For i = 0 to oPrinters.Count - 1 Step 2
   WScript.Echo "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
Next


When executed from the cmd prompt the output is:

Code: Select all

C:\Users\Squashman\Desktop>cscript //nologo printers.vbs
Port XPSPort: = Microsoft XPS Document Writer
Port SHRFAX: = Fax
Port IP_10.28.18.109 = \\Servername\WI5140-XEROX-9
Port IP_10.28.9.81 = \\Servername\WI5101-XEROX-12
Port IP_10.28.9.71 = \\Servername\WI5101-XEROX-1
Port IP_10.28.18.164 = \\Servername\WI5140-XEROX-31
Port IP_10.28.18.150 = \\Servername\WI5140-XEROX-15

Re: Printer IP Finder

Posted: 04 Apr 2016 13:07
by Squashman
Forgot about all the Printer Vbscripts that are builtin to the operating system as well. Might want to take a look at that bolded one.

C:\Windows\System32\Printing_Admin_Scripts\en-US\prncnfg.vbs
C:\Windows\System32\Printing_Admin_Scripts\en-US\prndrvr.vbs
C:\Windows\System32\Printing_Admin_Scripts\en-US\prnjobs.vbs
C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs
C:\Windows\System32\Printing_Admin_Scripts\en-US\prnport.vbs
C:\Windows\System32\Printing_Admin_Scripts\en-US\prnqctl.vbs
C:\Windows\System32\Printing_Admin_Scripts\en-US\pubprn.vbs

Re: Printer IP Finder

Posted: 04 Apr 2016 14:39
by Olyrd

Code: Select all

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections

For i = 0 to oPrinters.Count - 1 Step 2
   WScript.Echo "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
Next

Outputs the following:
http://h.dropcanvas.com/bltcm/Capture.PNG

And that's the issue with those microsoft scripts. All of our office printers does the same thinbg: istead of ip address, it gets the WSD port.

Re: Printer IP Finder

Posted: 05 Apr 2016 13:33
by foxidrive
This works for me, if the ip address has http://


Get jrepl.bat here viewtopic.php?f=3&t=6044

Place Jrepl.bat in the same folder as the batch file, or in a folder that is on the system path.



Code: Select all

call jrepl "^.*//([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*).*" "$1" /a /f "printerout.txt"

Re: Printer IP Finder

Posted: 05 Apr 2016 14:13
by thefeduke
In the first post:

Code: Select all

reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print /s >>_%computername%_printers.txt
has an embedded space and requires double quotes as below:

Code: Select all

reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print" /s >>_%computername%_printers.txt
As if you did not have enough to look at.

John A.

Re: Printer IP Finder

Posted: 06 Apr 2016 12:54
by foxidrive
foxidrive wrote:This works for me, if the ip address has http://


Sorry for not reading your question well. I saw "IP address" and my brain stopped there.

Re: Printer IP Finder

Posted: 06 Apr 2016 14:24
by foxidrive
Can you test this code? It's not going to be totally what you want at this stage, but if you can have a gander at what is at the top of the file and comment on what you see on your live system, with some sample data, then maybe it can be tweaked a little.


Code: Select all

@echo off
pushd "%~dp0"
set "wintype=win8-"
ver | find "6.1" > nul && set "wintype=win7-"

set find-yes=findstr /i    ":\/\/[0-9] FriendlyName DisplayName ^....Name \\%%~nb$"
set find-no= findstr /i /v "services$ fax.ws$ fax$ writer$ onenote \.dll$"

rem win8
set win8-01="HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\SWD\DAFWSDProvider"
set win8-02="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print"
set win8-03="HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print"
set win8-04="HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Control\Print"
set win8-05="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print"

rem win7
set win7-01="HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum"
set win7-02="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print"
set win7-03="HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print"
set win7-04="HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Control\Print"
set win7-05="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print"

(
for /f "tokens=1,* delims==" %%a in ('set %wintype%') do reg query %%b /s |%find-yes%|%find-no%
  echo(
  for /L %%z in (1,1,3) do echo( ===================================================
  echo(
for /f "tokens=1,* delims==" %%a in ('set %wintype%') do reg query %%b /s
)>"_%computername%_printers.txt"
pause & goto :EOF

Re: Printer IP Finder

Posted: 07 Apr 2016 11:56
by Olyrd
It works but in console it outputs:

Code: Select all

FINDSTR: Line 412 is too long.
FINDSTR: Line 412 is too long.
FINDSTR: Line 491 is too long.
FINDSTR: Line 493 is too long.
FINDSTR: Line 493 is too long.
FINDSTR: Line 494 is too long.
FINDSTR: Line 412 is too long.
FINDSTR: Line 412 is too long.
FINDSTR: Line 491 is too long.
FINDSTR: Line 493 is too long.
FINDSTR: Line 493 is too long.
FINDSTR: Line 494 is too long.
ERROR: The system was unable to find the specified registry key or value.
FINDSTR: Line 170 is too long.
FINDSTR: Line 170 is too long.
FINDSTR: Line 249 is too long.
FINDSTR: Line 251 is too long.
FINDSTR: Line 251 is too long.
FINDSTR: Line 252 is too long.
ERROR: The system was unable to find the specified registry key or value.
Press any key to continue . . .


Entire output is here:
http://pastebin.com/r6kZrd2k


As far as the IP address, it must output all IP addresses (which may start with http://, \\, or the IP without any header). In other words it should match:
http://192.168.1.100
\\192.168.1.100
192.168.1.100

Otherwise it's great.

Re: Printer IP Finder

Posted: 07 Apr 2016 17:38
by foxidrive
Is it reasonable to assume that the first octet of the IP address will never be a single digit? This does that.


The line too long errors are from "Default DevMode" lines. If you don't need those hex blocks then this code below has an extra filter to remove them, which should fix those error messages.

This error of course is due to the registry missing one of the keys you're looking for.
ERROR: The system was unable to find the specified registry key or value.


Code: Select all

@echo off
pushd "%~dp0"
set "wintype=win8-"
ver | find "6.1" > nul && set "wintype=win7-"

set find-yes=findstr /i    "[0-9][0-9]\.[0-9]*\.[0-9]*\.[0-9]* FriendlyName DisplayName ^....Name \\%%~nb$"
set find-no= findstr /i /v "services$ fax.ws$ fax$ writer$ onenote \.dll$ driverversion"

rem win8
set win8-01="HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\SWD\DAFWSDProvider"
set win8-02="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print"
set win8-03="HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print"
set win8-04="HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Control\Print"
set win8-05="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print"

rem win7
set win7-01="HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum"
set win7-02="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print"
set win7-03="HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Print"
set win7-04="HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Control\Print"
set win7-05="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print"

(
for /f "tokens=1,* delims==" %%a in ('set %wintype%') do reg query %%b /s |find /i /v "Default DevMode"|%find-yes%|%find-no%
  echo.&(for /L %%z in (1,1,3) do echo ===============================================)&echo.
for /f "tokens=1,* delims==" %%a in ('set %wintype%') do reg query %%b /s
)>"_%computername%_printers.txt"
pause & goto :EOF


Re: Printer IP Finder

Posted: 08 Apr 2016 03:35
by Compo
Just an observation…

Depending upon how you booted your machine at least two of the \CurrentControlSet\, \ControlSet001\ and \ControlSet002\ keys will match exactly. (One of them may differ in that the keys will contain the data of the machines last known good configuration).

I see no real reason to check the last known good configuration in this scenario so just check the \CurrentControlSet\ keys which will contain the keys for the currently booted configuration and reduce your output data dramatically.

Code: Select all

rem win8
set win8-01="HKLM\SYSTEM\CurrentControlSet\Enum\SWD\DAFWSDProvider"
set win8-02="HKLM\SYSTEM\CurrentControlSet\Control\Print"
set win8-03="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print"

rem win7
set win7-01="HKLM\SYSTEM\CurrentControlSet\Enum"
set win7-02="HKLM\SYSTEM\CurrentControlSet\Control\Print"
set win7-03="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print"