Permalink
Browse files

Merge pull request #94 from cvicente/cleanup_live_search

Remove Device::search_address_live() and associated code
  • Loading branch information...
cvicente committed Nov 22, 2017
2 parents 643cf64 + 5ea743d commit 8409b0c92c68e7225db8222162075e70a85bbcee
Showing with 5 additions and 303 deletions.
  1. +5 −77 bin/find_address.pl
  2. +0 −33 htdocs/management/address_tasks.html
  3. +0 −30 htdocs/management/mac.html
  4. +0 −163 lib/Netdot/Model/Device.pm
View
@@ -15,19 +15,12 @@
Locate a device given its name, MAC or IP address.
By default, this script uses information from the Netdot database.
The user also has the option of doing a "live" search by querying
relevant devices in the network. In this case, providing a VLAN id
can significantly speed up the search.
Usage: $0 [options] <ether|ip|name>
Available options:
-A|--arp_limit <value> Number of latest ARP cache entries to show (default: $self{ARP_LIMIT})
-F|--fwt_limit <valud> Number of latest Forwarding Table entries to show (default: $self{FWT_LIMIT})
-v|--vlan <vlanid> VLAN id to use when searching addresses "live"
-f|--forcelive Force a "live" search
-d|--debug Show debugging output
-h|--help Show help
@@ -38,8 +31,6 @@
# handle cmdline args
my $result = GetOptions( "A|arp_limit:s" => \$self{ARP_LIMIT},
"F|fwt_limit:s" => \$self{FWT_LIMIT},
"v|vlan:s" => \$self{VLAN},
"f|forcelive" => \$self{FORCE_LIVE},
"h|help" => \$self{HELP},
"d|debug" => \$self{DEBUG},
);
@@ -70,28 +61,15 @@
if ( $address =~ /^$MAC$/ ){
$address = PhysAddr->format_address_db($address);
if ( $self{FORCE_LIVE} ){
&search_live(mac=>$address, vlan=>$self{VLAN});
}else{
&show_mac($address, 1);
}
&show_mac($address, 1);
}elsif ( Ipblock->matches_ip($address) ){
if ( $self{FORCE_LIVE} ){
&search_live(ip=>$address, vlan=>$self{VLAN});
}else{
&show_ip($address, 1);
}
&show_ip($address, 1);
}else{
# Try to resolve
if ( my @ips = Netdot->dns->resolve_name($address) ){
foreach my $ip ( @ips ){
if ( $self{FORCE_LIVE} ){
&search_live(ip=>$ip, vlan=>$self{VLAN});
}else{
&show_ip($ip, 1);
}
&show_ip($ip, 1);
}
}else{
die "$address not found\n"
@@ -149,7 +127,7 @@ sub show_ip {
}
}
}else{
warn "$address not found in DB. Try searching live (--forcelive)\n";
print "$address not found in DB";
exit 0;
}
}
@@ -161,7 +139,7 @@ sub show_mac {
my $mac = PhysAddr->search(address=>$address)->first;
if ( !$mac ){
warn "$address not found in DB. Try searching live (--forcelive)\n";
print "$address not found in DB\n";
exit 0;
}
@@ -241,56 +219,6 @@ sub show_mac {
}
}
###############################################################################
sub search_live{
my (%argv) = @_;
my $info = Device->search_address_live(%argv);
if ( $info ){
my ($ipaddr, $macaddr);
$ipaddr = $info->{ip};
$macaddr = $info->{mac};
if ( scalar keys %{$info->{routerports}} ){
if ( $self{ARP_LIMIT} ){
print "\nARP entries: \n";
}
foreach my $id ( keys %{$info->{routerports}} ){
my $iface = Interface->retrieve($id);
my $ip = (keys %{$info->{routerports}{$id}})[0];
$ipaddr = $ip unless $ipaddr;
my $mac = $info->{routerports}{$id}{$ip};
$macaddr = $mac unless $macaddr;
if ( $self{ARP_LIMIT} ){
print $iface->get_label, ", ", $ip, ", ", $mac, "\n";
}
}
}
if ( scalar keys %{$info->{switchports}} && $self{FWT_LIMIT} ){
print "\nFWT entries: \n";
foreach my $id ( keys %{$info->{switchports}} ){
my $iface = Interface->retrieve($id);
print $iface->get_label;
print " ";
}
print "\n";
}
print "\n";
if ( $macaddr ){
print "MAC Address : ", $macaddr, "\n";
print "Vendor : ", $info->{vendor}, "\n" if $info->{vendor};
}
if ( $ipaddr ){
print "IP Address : ", $ipaddr, "\n";
print "DNS : ", $info->{dns}, "\n" if $info->{dns};
}
print "\n";
&print_location($info->{edge}) if $info->{edge};
}else{
die "$address not found\n";
}
}
###############################################################################
sub print_location{
@@ -19,7 +19,6 @@
$search_regex => undef
$search_other => undef
$search => undef
$search_live => undef
$user => $ui->get_current_user($r);
$submit => undef
$show_tasks => undef
@@ -51,7 +50,6 @@
my $covering_block;
my @list;
my $live_info;
my $manager = $ui->get_permission_manager($r);
</%init>
@@ -209,11 +207,6 @@
$search = $search_other;
$search = $ui->rem_lt_sp($search);
@list = Ipblock->keyword_search($search);
}elsif( $search_live ){
eval { $live_info = Device->search_address_live(ip=>$search_live) };
if ( my $e = $@ ){
$m->comp('/generic/error.mhtml', error=>$e);
}
}elsif( $submit eq "Search" ){
$m->comp('/generic/no_search_criteria.html');
}
@@ -239,34 +232,8 @@
</div>
</div>
% }
% if ($search_address){
% # We do not want to offer a live search unless it's an end-node address
% my $nip = NetAddr::IP->new($search_address);
% if ( defined($nip) &&
% (($nip->version == 4 && $nip->masklen == 32) ||
% ($nip->version == 6 && $nip->masklen == 128)) ){
<p>
<div class="container">
<div class="containerhead">Live Search:</div>
<div class="containerbody">
<p>Would you like to perform a "live" search in the network? (It might take a while).
<p>
<form action="address_tasks.html" method="POST">
<input name="submit" value="Live Search" class="btn" type="submit">
<input type="hidden" name="search_live" value="<% $search %>">
</form>
</div>
</div>
% }
% }
% }
% }
</div>
</div>
% if ( $search_live && $live_info ){
<& live_results.mhtml, info=>$live_info &>
% }
View
@@ -12,7 +12,6 @@
<%args>
$id => undef
$search => undef
$search_live => undef
$vlan => undef
$delete => undef
$fwt_limit => Netdot->config->get('SHOW_LAST_N_FTE') || 10
@@ -55,41 +54,12 @@
<& /generic/sortresults.mhtml, object => \@list, dowindow => $dowindow &>
% }elsif ( @list ){
% $o = $list[0];
% }else{
<p>Would you like to perform a "live" search in the network? (It might take a while).
<p>
<form action="mac.html" method="POST">
<select name="vlan">
<option value="" SELECTED>Select VLAN ID</option>
% foreach my $v ( sort { $a->vid <=> $b->vid } Vlan->retrieve_all ){
<option value="<% $v->vid %>"><% $v->vid %></option>
% }
</select>
<input name="submit" value="Live Search" class="btn" type="submit">
<input type="hidden" name="search_live" value="<% $search %>">
</form>
% }
</div>
</div>
<%perl>
}elsif ( $search_live ){
if ( !$vlan ){
$m->comp('../generic/error.mhtml', error=>"Please select a VLAN ID");
}
my $info;
my $address = PhysAddr->format_address_db($search_live);
eval { $info = Device->search_address_live(mac=>$address, vlan=>$vlan) };
if ( my $e = $@ ){
$m->comp('/generic/error.mhtml', error=>$e);
}
if ( $info ){
$m->comp('live_results.mhtml', info=>$info);
}else{
print "Not found";
}
}
if ( $id && !$o ){
unless ( $o = PhysAddr->retrieve($id) ){
$m->comp('/generic/error.mhtml', error => "Could not retrieve PhysAddr id: $id");
Oops, something went wrong.

0 comments on commit 8409b0c

Please sign in to comment.