Batch file to get printer info and MAC if printer port is TCP/IP

Please refer to the previous question for more details...

Using the code in Bill's solution I was wondering if it would be possible to add a function while it is grabbing the printer's information that would check if the port was an IP address then it would get the MAC address from the printer and then pop that information into the log file under the Port information.

Maybe something like a nbtstat -a xxx.xxx.xxx.xxx, findstr "MAC Address ="

There maybe another command line tool that will grab the MAC and work better, this was just the first one I thought about.

Thanks much,
Ryan
Ryan BennettAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
I'm not aware of any reliable way to obtain a MAC address from a printer via it's IP address, under Windows.

I don't think NBTSTAT will do it these days, since most printers aren't using using NetBios over TCP/IP to get their job done.

ARP can sometimes be used, but I tried that on my network, even after doing a successful PING to a printer IP, but it was not in the ARP table.

GETMAC /S didn't work on an IP printer here, it failed indicating there was no RPC service on the remote system.  Makes sense, it was an HP printer.

If you can determine a consistent way to obtain the MAC address in your environment that could be added to the script, but at this point I don't have a recommendation for that.

~bp
Ryan BennettAuthor Commented:
Dang! All the same, do you think it would be possible to code it so I can play around with any way?
Bill PrewIT / Software Engineering ConsultantCommented:
Yes, I will work up an example.

~bp
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Ryan BennettAuthor Commented:
Any luck coming up with a working example?
Bill PrewIT / Software Engineering ConsultantCommented:
This seems to work, except that my printers don't return a MAC to nbtstat.  But when I tried a computer IP in that line (hard coding it for a test) then this code did return the MAC address.  See if this helps...

@echo off

setlocal enabledelayedexpansion

set printers_key=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers
set name_val=Name
set port_val=Port
set location_val=Location
set description_val=Description
set printerdriver_val=Printer Driver
set sharename_val=Share Name
set report=%userprofile%\desktop\Printers.txt

(
  for /f "tokens=7 delims=\" %%P in ('reg.exe query "%printers_key%"') do (
    for /f "tokens=2*" %%a in ('reg.exe query "%printers_key%\%%P" /v "%name_val%" 2^>NUL ^| find /i "%name_val%"') do echo Printer Name = %%~b
    for /f "tokens=3*" %%a in ('reg.exe query "%printers_key%\%%P" /v "%printerdriver_val%" 2^>NUL ^| find /i "%printerdriver_val%"') do echo Model = %%b
    for /f "tokens=2*" %%a in ('reg.exe query "%printers_key%\%%P" /v "%port_val%" 2^>NUL ^| find /i "%port_val%"') do (
      echo Port = %%~b
      for /f "tokens=*" %%c in ('echo %%~b^|findstr /r /b /e "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"') do (
        echo IP = %%c
        for /f "tokens=2 delims==" %%d in ('nbtstat -a %%c^|find /i "mac address ="') do (
          echo MAC = %%d
        )
      )
    )
    for /f "tokens=2*" %%a in ('reg.exe query "%printers_key%\%%P" /v "%location_val%" 2^>NUL ^| find /i "%location_val%"') do echo Location = %%~b
    for /f "tokens=2*" %%a in ('reg.exe query "%printers_key%\%%P" /v "%description_val%" 2^>NUL ^| find /i "%description_val%"') do echo Description = %%~b
    for /f "tokens=3*" %%a in ('reg.exe query "%printers_key%\%%P" /v "%sharename_val%" 2^>NUL ^| find /i "%sharename_val%"') do echo Share Name = %%~b
  )
)>"%report%"

Open in new window

~bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ryan BennettAuthor Commented:
I figured from what you said in your previous comments that it was a long shot, I get the same results as you. But this will still prove useful to me in other ways. I appreciate you not giving up on this.

 Thanks again,
Ryan
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome, glad to help.

~bp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.