Navigation Menu

Skip to content

Commit

Permalink
Merge branch 'master' of github.com:cvicente/Netdot
Browse files Browse the repository at this point in the history
  • Loading branch information
cvicente committed Dec 1, 2017
2 parents 1828399 + 8409b0c commit 6f1fbef
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 303 deletions.
82 changes: 5 additions & 77 deletions bin/find_address.pl
Expand Up @@ -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
Expand All @@ -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},
);
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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{
Expand Down
33 changes: 0 additions & 33 deletions htdocs/management/address_tasks.html
Expand Up @@ -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
Expand Down Expand Up @@ -51,7 +50,6 @@

my $covering_block;
my @list;
my $live_info;
my $manager = $ui->get_permission_manager($r);
</%init>

Expand Down Expand Up @@ -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');
}
Expand All @@ -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 &>
% }

30 changes: 0 additions & 30 deletions htdocs/management/mac.html
Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 6f1fbef

Please sign in to comment.