How to get printer IP addresses via the command line in Linux?

2

I’m trying to create a script to get all the IP address of wireless printers on the network. The problem is I’m not sure where to start I’ve looked into the CUPS but every document or tutorial only goes so far with the command line before it moves on to using the web interface, they never mention how to find the IP by using just the CLI, any ideas?

twigg

Posted 2015-03-22T23:42:56.037

Reputation: 355

Answers

1

According to this question and answer thread on the “Unix & Linux Stack Exchange” you can only get a list of installed printers via lpstat -s or using it with sudo (sudo lpstat -s) to get a full device path. Or maybe using nmap -A?

All that said, perhaps using arp with grep and sed chained together with pipes (|) can work like this:

arp -a | grep Printer | awk '{ print $2 }' | sed 's/[()]//g'

That command breaks down like this:

  • arp -a would show you all of the arp traffic.
  • grep Printer will parse the arp -a output for lines that have Printer in the name.
  • awk '{ print $2 }' grabs the IP address, but with parentheses ((/)) around them.
  • sed 's/[()]//g' then cleans up the IP address to remove the parentheses.

That said, I don’t have an easy way to test this concept on my local setup—since I don’t even have one Wi-Fi printer on my network—but I assume it will work. Maybe the grep argument should be adjusted to “printers” or maybe “print”; I don’t know for sure. These are mainly brainstorming ideas that hopefully will help you tackle this issue or at least set you on the right course.

JakeGould

Posted 2015-03-22T23:42:56.037

Reputation: 38 217

2

How about lpinfo (need CUPS installed)
(using Perl here to filter out the other stuff)
see here

$ sudo lpinfo -l -v |  perl -ne 'print if /^Device: uri = socket:/ .. /info/'

Device: uri = socket://192.168.0.3
      class = network
      info = Officejet J6400 series
Device: uri = socket://192.168.0.5:9100
      class = network
      info = HP Officejet Pro 8610

AAAfarmclub

Posted 2015-03-22T23:42:56.037

Reputation: 121