3

Explanation:

I need a script or program to discover devices on my network. I was thinking maybe doing the scan with nmap and I need to display just the name of device, what it is, and also the ip address/mac address of the device as well. I would like to do the scan in the background and only display the desired information in the form of a list I guess.

Example:

after script / program runs:

-There are 2 hosts up

  • Host 1: Lenovo-PC | 192.168.1.86 | 0A:65:3F:2B:F1 | Windows
  • Host 2: LG-3444 | 192.168.1.89 | A9:B2:C3:D4:E5 | LG Electronics

ect... you get the point.

PS: BTW, these are examples not real IP's.

Overview

So I want to scan my network for devices / hosts and display important info about each one in a list (using bash script, python, or anything that can achieve this).

  • 2
    nmap -sn 192.168.1.0/24 | grep report; arp -an will give you a good place to start. – DopeGhoti Jul 10 '17 at 18:47
  • @DopeGhoti Thanks for the tip, but It's not quite what I was looking for. I don't want to just run a scan, I want to copy certain parts from the scan and paste them in a certain order and in a list if there are more than 1 device. – iamr00t Jul 10 '17 at 18:57
  • 1
    nmap -oG - 192.168.1.0/24 gives output that you can grep. The -oX option provides it as XML, so you can use XPath on it. – roaima Jul 10 '17 at 19:19
  • @roaima you say "output that you can grep" ...Could you please provide an example to clarify? I'm just getting into scripting so I'm a little new to grep – iamr00t Jul 10 '17 at 21:21
  • @iamt00t run the example I've given – roaima Jul 10 '17 at 21:48
0

This is an example in perl. The code run nmap with XML output format and then parse the output using Nmap::Parser library. You need to provide network mask as an argument. So if you save this skript to the explore.pl file, you can run it as

explore.pl 192.168.1.0/24

You need to run this script as root if you want to display MAC addresses.

#!/usr/bin/env perl
use utf8;
use strict;
use warnings;
use Nmap::Parser;

sub safeGet($){
  defined $_[0] ? $_[0] : "unknown";
}

my $nmOutput=`/usr/bin/nmap -oX - -A @ARGV`;

my $np = new Nmap::Parser;
$np->parse($nmOutput);

my $cnt=0;
for my $host ($np->all_hosts()){
  $cnt++;

  my $hostName = 'unknown';
  if(@{$host->{hostnames}} > 0){ # $host->hostname returns "0" when no hostname was found.
    $hostName = $host->hostname;
  }
  print "Host $cnt: $hostName|"
    . safeGet($host->addr) ."|"
    . safeGet($host->mac_addr)."|"
    . safeGet($host->os_sig->name)."\n";
}

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.