Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Network device discovery with Perl

by perldragon80 (Sexton)
on Aug 19, 2004 at 20:04 UTC ( [id://384434]=perlquestion: print w/replies, xml ) Need Help??

perldragon80 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks!
I am creating a web interface for network device management on a local LAN and I am trying to setup auto device discovery. In my case it is a company specific device that needs to be managed and all other switches, routers, pc's, servers, etc..on the same network should be ignored. So the web server will try to contact every device and then determine which ones to manage and discard all others. Ideas so far:

**The device has a login similar to cisco routers so the Net::Telnet::Cisco module works with it.
1)Use Net::Ping and ping a broadcast address and then use the telnet module to see if the associated login would work for all responses from the broadcast, and if it did then add that device to a database, otherwise ignore.
2)Use arp to get a list of associated IP's and MAC addresses and then telnet to check.
3)Force the user to enter IP range and just step through (checking active status with Net::Ping) each IP with telnet module to see if device responds accordingly.
4)Use nmap or some other network mapper to get some host info....although this usually requires root access.Then use telnet module to check

I would probably use a threaded model to do these checks in parallel for speedy response, and make sure the timeouts were set to a small value, but haven't had too much experience with network management and perl so I thought the monks might have some good ideas.

Replies are listed 'Best First'.
Re: Network device discovery with Perl
by Mr_Person (Hermit) on Aug 19, 2004 at 20:29 UTC
    You probably want to take a look at Nmap::Scanner, which is a nice way to integrate nmap with Perl. Using nmap is an advantage because it has already been optimized for scanning large numbers of hosts quickly and accurately. Just have it do a TCP connect() scan (which doesn't require any special privileges) on port 23 for all your hosts, then try to telnet on the ones that are successful. Not all nmap scans require root access, they just might not be as efficient or sneaky without it.
Re: Network device discovery with Perl
by tachyon (Chancellor) on Aug 19, 2004 at 23:01 UTC
    Network::Discovery and its Detect, Scan, Sniff, Register, Tk .... modules may be of use.



      Thanks for the info and pointers!!
      Here is a code snippet using those modules (pretty much straight out of the docs, but it might be handy to see an example). In my case I am only looking for a host that, when scanned on the telnet port, will return a hostname, and then I make sure the status is 'up'. Then for all the hosts that are up I do an interface discovery but only using port 23, so the port listing should be changed if you want to discover all open ports, etc..:
      #!/usr/bin/perl -w use strict; use Nmap::Scanner; use NetworkInfo::Discovery; use NetworkInfo::Discovery::Register; use NetworkInfo::Discovery::Scan; my $scanner = new Nmap::Scanner; $scanner->tcp_connect_scan(); $scanner->add_scan_port(23); $scanner->max_rtt_timeout(200); $scanner->add_target('enter range here(eg'); # $results is an instance of Nmap::Scanner::Backend::Results my $results = $scanner->scan(); my $host_list = $results->get_host_list(); my @upHosts = (); my $counter = 0; while (my $host = $host_list->get_next()) { unless (!$host->hostname()) { if( $host->status() eq 'up' ) { $upHosts[$counter] = ($host->addresses)[0]->addr(); print "Found host named: " . $host->hostname() . "\n"; print "The host IP is $upHosts[$counter] \n"; $counter++; } } } my $disc = new NetworkInfo::Discovery::Register ( 'file' => '/tmp/scan.register', 'autosave' => 1 ) || warn ("failed to make new obj"); my $scan = new NetworkInfo::Discovery::Scan ( hosts=>\@upHosts, ports=>[23], timeout=>1, 'wait'=>0, protocol => 'tcp' ); $scan->do_it(); $disc->add_interface($_) for ($scan->get_interfaces); foreach my $h ($scan->get_interfaces) { print $h->{ip} . "\n"; print " has tcp ports: " . join(',',@{$h->{tcp_open_ports}}) . +"\n" if (exists $h->{tcp_open_ports}) ; } print "/nThe register looks like:/n"; $disc->print_register;

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://384434]
Approved by roju
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (1)
As of 2024-07-19 03:38 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.