4

I have a project where I know a single computer and a single printer will be the only things on the network. What I want to do is detect when the printer is connected to the network. I also know that the computer is 192.168.3.1. However, with DHCP I won't know the printer address (yes, it could be made static to make it easier but, 'they' don't like that. 'They' want it dynamic)

What I have is a script that does the following and it works.

nmap -sP 192.168.3.0/24 \
  | awk '/192.168.3/ && !/192.168.3.1$/' \
  | sed 's/Nmap scan report for //'

Nmap output

 Nmap scan report for 192.168.3.1
 Host is up (0.014s latency).
 Nmap scan report for 192.168.3.100
 Host is up (0.012s latency).
 Nmap done: 256 IP addresses (2 hosts up) scanned in 2.54 seconds

Script output

 192.168.3.100

It only takes a couple seconds to work but is there a better/cleaner/faster way?

  • 8
    Piping awk to sed is redundant; awk does both jobs. Paste the output you are working with and the desired result. – jasonwryan Apr 11 '18 at 21:31
8

There's no need to scan the entire subnet if you know that you're not interested in part of it. (Avoiding the computer means you don't need to discard its result.)

nmap -oG - -sn 192.168.3.2-254 | awk '$NF=="Up" {print $2}'

or if you prefer using the XML output instead of the grep output

nmap -oX - -sP 192.168.3.2-254 | xmlstarlet sel -t -m '//address[@addrtype="ipv4"]' -v '@addr' -n

Use -sP instead of the newer -sn if your version of nmap requires it.


Incidentally, although your system administrators may want you to have your printer on DHCP, there should be little reason why they can't arrange for it to have a known unchanging address. (I do that for printers on my networks so that printer software doesn't need to worry about IP addresses changing unexpectedly.) Sometimes this is known as a "sticky" address, to differentiate it from a static (non-DHCP) address or a pseudo-random dynamic (DHCP) address.

Are you sure the DHCP server itself won't be on your subnet? Otherwise, how is your printer going to get its dynamic address?

  • in this case it's not administrators. the computer is a raspberry pi and is also the hotspot and dns server. eth0 is setup as dhcp to whatever gets plugged in. in this case the Pi is being rented and if the customer doesn't want to rent a printer they may buy one of their own of the same model. in this case, they would only be required to know how to plug in the eth cable and the Pi would find the 1 address on the subnet that is not itself. – Mike Kangas Apr 12 '18 at 22:56
  • @MikeKangas ah! Then if there's only going to be one device on your subnet, allow your DHCP server to offer only one lease. Make it short enough that a printer swap-over won't crash and burn (maybe 10 minutes) and then you've got a fixed known IP address for "any" printer the customer cares to attach. – roaima Apr 12 '18 at 22:59
  • OOOH! I have about 1 month of total linux experience and doing something like that never crossed my mind!!! huge huge thanks!! – Mike Kangas Apr 13 '18 at 0:13
  • 1
    i found the dnsmasq dhcp-range and turned it to 192.168.3.100,192.168.3.100 and then updated my nmap to only look for that IP. my nmap scan time went from 2.34 seconds to 0.02 seconds. thank you, thank you, thank you. – Mike Kangas Apr 13 '18 at 2:24
11

You can accomplish this with the following awk command:

nmap -sP 192.168.3.0/24 \
  | awk '/192.168.3/ && !/192.168.3.1$/{print $NF}'

This is telling awk to print the last field of the matched line(s)

3

Perhaps an even more efficient solution using only grep (requires GNU grep or a grep that supports perl regex):

nmap -sP 192.168.3.0/24 \
  | grep -o -P '192.168.3.(?!1$)[0-9]+'

This is greping for -o (only) the IP address matching any IP beginning with 192.168.3 except for 192.168.3.1

0

Once you have the IP address for the printer you can then get the host name with nmblookup -A 'IP address'. This should help in the future if the IP changes since you'll be asking the network for a machine with this name vs trying to find a machine by IP address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.