Files
scripts/Perl arp cache per snmp durchsuchen/new/search.pl
2024-10-14 00:08:40 +02:00

161 lines
4.3 KiB
Perl

#!/bin/perl
use menu;
use strict;
my %haupt;
my %konfig;
my %ip_mac_hash;
my %switch_hash;
my $community='con';
$haupt{'1'} = ['IP', '#search_ip'];
$haupt{'2'} = ['MAC', '#search_mac'];
$haupt{'3'} = ['Switch/Port', '#search_switchport'];
$haupt{'a'} = ['ARP Caches lesen','#read_arp'];
$haupt{'m'} = ['MAC Tables lesen','#read_mac'];
$haupt{'x'} = ['Ende', '#bye'];
$haupt{'desc'} = "fucking menu";
show(\%haupt);
sub read_arp {
%ip_mac_hash = ();
create_host_mac_hash('10.1.1.250',\%ip_mac_hash);
create_host_mac_hash('10.3.2.51',\%ip_mac_hash);
create_host_mac_hash('10.3.2.52',\%ip_mac_hash);
create_host_mac_hash('90.200.31.21',\%ip_mac_hash);
create_host_mac_hash('90.200.31.23',\%ip_mac_hash);
show(\%haupt);
}
sub read_mac {
%switch_hash = ();
create_switch_hash("10.1.1.101",\%switch_hash);
create_switch_hash("10.1.1.103",\%switch_hash);
create_switch_hash("10.1.1.104",\%switch_hash);
create_switch_hash("10.1.1.106",\%switch_hash);
create_switch_hash("10.1.1.107",\%switch_hash);
create_switch_hash("10.1.1.112",\%switch_hash);
show(\%haupt);
}
sub search_ip {
my $mac;
my $found=0;
print "IP?";
my $ip = <STDIN>;
chomp $ip;
foreach (keys %ip_mac_hash) {
if ($_ eq $ip) {
$mac = $ip_mac_hash{$ip};
$found=1;
last;
}
}
if ($found==1) {
print "\nGesuchte IP: $ip\nGefundene MAC-Adresse: $mac\n";
foreach my $swip (keys %switch_hash) {
my $count;
foreach my $int (keys %{$switch_hash{$swip}}) {
foreach my $maconsw (@{$switch_hash{$swip}{$int}{'MAC'}}) {
$count++;
print "$count $maconsw $mac\n";
if ($maconsw eq $mac) {
print "Switch: $swip\nPort: $switch_hash{$swip}{$int}{'ifDesc'}\n";
last;
}
}
}
}
}
<STDIN>;
show(\%haupt);
}
sub search_mac {
show(\%haupt);
}
sub search_switchport {
show(\%haupt);
}
sub bye {
exit;
}
sub check_ip {
my ($ip) = @_;
my $ok=0;
my ($i1,$i2,$i3,$i4);
if ( ($i1,$i2,$i3,$i4) = $ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/ ) {
$ok=1 if ($i1 < 255 and $i2 < 255 and $i3 < 255 and $i4 < 255);
}
return $ok;
}
sub check_mac {
my ($mac) = @_;
my $ok=0;
$ok=1 if ( $mac =~ /^[0-9a-f]{2}\.[0-9a-f]{2}\.[0-9a-f]{2}\.[0-9a-f]{2}\.[0-9a-f]{2}\.[0-9a-f]{2}$/i );
return $ok;
}
sub check_switch_port {
my ($switchport)=@_;
my $ok=0;
my ($ip,$port) = split /\//,$switchport;
$ok=1 if (check_ip($ip) and check_port($port));
return $ok;
}
sub check_port {
my ($port)=@_;
my $ok=0;
$ok=1 if ($port =~ /^[0-9]{1,2}$/ or $port =~ /^[a-z]{1}[0-9]{1}$/i);
return $ok;
}
sub create_host_mac_hash {
my ($switchip,$hashref)=@_;
my @array;
@array = `snmpwalk -mall -c $community $switchip .1.3.6.1.2.1.4.22.1.2`;
foreach (@array) {
chomp;
my ($ip,$mac) = $_ =~ /([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*) = Hex: ([0-9a-f ]*) $/i;
$mac =~ s/ /./g;
$$hashref{"$ip"}="$mac";
}
print "";
}
sub create_switch_hash {
my ($switchip,$hashref)=@_;
my @array;
@array = `snmpwalk -mall -c $community $switchip .1.3.6.1.2.1.17.4.3.1.1`; # MAC-Adr auf Switch ermitteln
foreach (@array) {
chomp;
my ($m1,$m2,$m3,$m4,$m5,$m6) = $_ =~ /= Hex: ([0-9a-f]{2}) ([0-9a-f]{2}) ([0-9a-f]{2}) ([0-9a-f]{2}) ([0-9a-f]{2}) ([0-9a-f]{2})/i;
my $mac="$m1."."$m2."."$m3."."$m4."."$m5."."$m6"; # MACs nach Dezimal umwandeln
my $d1 = hex($m1);
my $d2 = hex($m2);
my $d3 = hex($m3);
my $d4 = hex($m4);
my $d5 = hex($m5);
my $d6 = hex($m6);
my $oid=".1.3.6.1.2.1.17.4.3.1.2.$d1.$d2.$d3.$d4.$d5.$d6";
my $line=`snmpwalk -mall -c $community $switchip $oid`; # Interface auf dem MAC liegt suchen
chomp $line;
my ($interface) = $line =~ / = ([0-9]{2})$/;
# print "$interface\n";
push @{$$hashref{$switchip}{$interface}{'MAC'}},$mac unless ($interface eq "");
}
foreach my $ip (keys %$hashref) {
foreach my $intid (keys %{$$hashref{$ip}}) {
# .1.3.6.1.2.1.2.2.1.2 => ifDescr; dazu noch .$intid anhängen
# .1.3.6.1.2.1.2.2.1.4 => ifMTU; dazu noch .$intid anhängen
# ...
my $oid = ".1.3.6.1.2.1.2.2.1.2." . $intid;
my $line=`snmpwalk -mall -c $community $switchip $oid`;
my ($portname) = $line =~ /"([a-z0-9]{1,2})"/i;
$$hashref{$switchip}{$intid}{'ifDesc'} = $portname;
# print "$$hashref{$switchip}{$intid}{'ifDesc'}\n";
}
}
}
1;