www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to do same as 'nmap' command from within a D program?

reply Daren Scot Wilson <darenw darenscotwilson.com> writes:
I'm writing a command line program to control certain hardware 
devices. I can hardcode or have in a config file the IP addresses 
for the devices, if I know that info. If I don't?  Then I run an 
'nmap' command and look for the devices.  But why should I, a 
human, have to do any work like that? Bah!  I want my program to 
obtain this information at runtime, automatically, and "don't 
make me think".

One thing that might make it tough is nmap must run sudo to 
report the desired information. (To my knowledge; I'm no 
networking expert.) The exact command is:

sudo nmap -sn 192.168.11.0/24  |ack -B2 "Philips"

The IP address is printed two lines before the name match (ack is 
"better than grep"). Typical nmap output is a series of chunks of 
text like this:

Nmap scan report for 192.168.11.10
Host is up (0.00033s latency).
MAC Address: 00:17:88:4D:97:4D (Philips Lighting BV)

I don't see any D std.* libraries that do this. Are there a Dub 
packages I should look at?
Jan 22 2022
next sibling parent reply forkit <forkit gmail.com> writes:
On Saturday, 22 January 2022 at 20:55:38 UTC, Daren Scot Wilson 
wrote:

is this helpful:

// ---
module test;

import std;

void main()
{
     auto result = execute(["bash", "-c", "nmap -sn 
192.168.11.0/24 | ack -B2 \"Phillips\""]);

     if(canFind(result.to!string, "Host is up"))
         writeln("Host is up");
     else
         writeln("Host not found.");
}

// ---
Jan 22 2022
parent reply forkit <forkit gmail.com> writes:
On Saturday, 22 January 2022 at 22:44:31 UTC, forkit wrote:

and here is how to get the ip (depending on the formatting of 
your output of course)

// ---
module test;

import std;

void main()
{
     auto result = execute(["bash", "-c", "nmap -sn 
192.168.11.0/24 | ack -B2 \"Philips\""]);

     string ip;

     if(canFind(result.to!string, "Host is up"))
     {
         writeln("Host is up");

         string str = result.to!string.chop;
         ip = str[ (indexOf(str, "for Philips (") + 10)..$-4 ];
         writeln(ip);
     }
     else
         writeln("Host not found.");
}

// ----
Jan 22 2022
parent forkit <forkit gmail.com> writes:
On Saturday, 22 January 2022 at 23:15:18 UTC, forkit wrote:

oh.. this is better i think...

ip = str[ ((lastIndexOf(str, "(")) + 1) .. lastIndexOf(str, ")") 
];
Jan 22 2022
prev sibling next sibling parent reply frame <frame86 live.com> writes:
On Saturday, 22 January 2022 at 20:55:38 UTC, Daren Scot Wilson 
wrote:

 I don't see any D std.* libraries that do this. Are there a Dub 
 packages I should look at?
If you really want to this in D without any external app or OS API you could just ping all possible hosts, see which respond and then use `getHostByAddr()` to find the hostname. Another more professional way is to query the ARP protocol, where you send a packet as broadcast to all interfaces in the network to find a MAC for a given IP - if any host responses with a MAC, the host is up. You have to build the packet data for yourself, there are examples on the web. The socket to use is family:INET, type:RAW and protocol:ICMP for ping or RAW for ARP or anything that isn't listed in D. As you can see, it's required to test every possible IP out (except for any other discovery protocols supported by your network/router). For this reason, any OS does this scan periodically and caches the result. On UNIX you can just directly read the file `/proc/net/arp`, no need to use nmap.
Jan 22 2022
parent Daren Scot Wilson <darenw darenscotwilson.com> writes:
On Sunday, 23 January 2022 at 06:30:11 UTC, frame wrote:
 On Saturday, 22 January 2022 at 20:55:38 UTC, Daren Scot Wilson 
 wrote:

 I don't see any D std.* libraries that do this. Are there a 
 Dub packages I should look at?
If you really want to this in D without any external app or OS API you could just ping all possible hosts, see which respond and then use `getHostByAddr()` to find the hostname. Another more professional way is to query the ARP protocol, where you send a packet as broadcast to all interfaces in the network to find a MAC for a given IP - if any host responses with a MAC, the host is up. You have to build the packet data for yourself, there are examples on the web. The socket to use is family:INET, type:RAW and protocol:ICMP for ping or RAW for ARP or anything that isn't listed in D. As you can see, it's required to test every possible IP out (except for any other discovery protocols supported by your network/router). For this reason, any OS does this scan periodically and caches the result. On UNIX you can just directly read the file `/proc/net/arp`, no need to use nmap.
I'll try this. Looks more educational. This is a personal project, a show-off project. Once I'm done with another portion of it, I'll get onto this. My program will need to scan only once, not even once per run, since I can stash the results in a config file, but once whenever the user knows the hardware devices have changed.
Jan 24 2022
prev sibling parent Adam D Ruppe <destructionator gmail.com> writes:
On Saturday, 22 January 2022 at 20:55:38 UTC, Daren Scot Wilson 
wrote:
 I'm writing a command line program to control certain hardware 
 devices. I can hardcode or have in a config file the IP 
 addresses for the devices, if I know that info. If I don't?
Depending on the hardware, you might be able to send a broadcast packet and listen to replies too. The nmap command you do just does pings to each address in that range (btw I actually wrote a little module to turn one of those ranges into a bunch of ip address strings: https://github.com/adamdruppe/arsd/blob/master/cidr.d ). The stdlib doesn't have a convenient ping function though. But yah depending on the hardware you might be able to do udp broadcasts and such. I had this led tower light thing for a client I had to set up and that's what i did there - udp broadcast a config packet, get the list of all the mac addresses, send config packets to change their ips, and get going.
Jan 24 2022