Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Network device discovery with Perl

by perldragon80 (Sexton)
on Aug 26, 2004 at 18:35 UTC ( [id://386119]=note: print w/replies, xml ) Need Help??


in reply to Re: Network device discovery with Perl
in thread Network device discovery with Perl

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 192.168.0.1-254)'); # $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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://386119]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-07-16 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    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.