Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use Expect;
- use Data::Dumper;
- use Net::SSH::Expect;
- use Net::SNMP qw(snmp_dispatcher oid_lex_sort);
- my $host = $ARGV[0];
- my $ssh = Net::SSH::Expect->new (
- host => "$host",
- password=> 'PXEQvgos',
- user => 'root',
- raw_pty => 1,
- # log_file => "log_file",
- timeout => 5
- );
- my ($session, $error) = Net::SNMP->session(
- -hostname => $host,
- -community => 'sysiq',
- -port => '161',
- -version => 'snmpv2c'
- );
- # Was the session created?
- if (!defined($session)) {
- printf("ERROR: %s.\n", $error);
- exit 1;
- }
- my $aliases;
- my $ports;
- my $portnames;
- my $macports;
- my $ifTable = '1.3.6.1.2.1.2.2';
- my $ifAlias = '1.3.6.1.2.1.31.1.1.1.18';
- my $arpTable = '1.3.6.1.2.1.17.4.3.1.2';
- my $portIndex = '1.3.6.1.2.1.17.1.4.1.2';
- my $portStatus = '1.3.6.1.2.1.2.2.1.8';
- my $result;
- # Get ifAliases
- $ifAliasRes = $session->get_table(-baseoid => $ifAlias) or die($session->error());
- for my $ifA (keys %$ifAliasRes) {
- my $desc = $ifAliasRes->{$ifA};
- my $idx = $1 if $ifA =~ /$ifAlias\.(.*)/;
- next if $desc eq '';
- next if $desc =~ /.*\d+ Interface/; # Unnamed ports on HP 1910 are labeled "Ethernet1/0/10 Interface"
- #print "ifA: $idx desc: $desc \n";
- $aliases->{$idx} = $desc;
- }
- #Get port status
- $pStatusRes = $session->get_table(-baseoid => $portStatus) or die($session->error());
- for my $pIndex (keys %$pStatusRes) {
- my $pStatus = $pStatusRes->{$pIndex};
- print my $idx = $1 if $pIndex =~ /$portStatus\.(.*)/;
- $portStatusTable->{$idx} = $pStatus;
- }
- print Dumper($portStatusTable);
- #Get blocked ports
- my $blocklog = "./blockswports.log";
- if ( -s "$blocklog"){
- open(FILE,"$blocklog");
- while(<FILE>){
- chomp ($_);
- if ($_ =~ /^(\S+)\|(\S+)\|(\S+)\|(\S+)/){
- print $blockhost = "$1";
- print "QQ\n" if ($host eq $blockhost);
- }
- }
- }
- else {
- print "$blocklog file is empty\n";
- }
- #Get port indexes and fill port names
- $pIndexRes = $session->get_table(-baseoid => $portIndex) or die($session->error());
- for my $pIdx (keys %$pIndexRes) {
- my $idx = sprintf('%02d',$1) if $pIdx =~ /$portIndex\.(.*)/;
- $ports->{$idx} = $pIndexRes->{$pIdx};
- $portnames->{$idx} = $aliases->{$pIndexRes->{$pIdx}} if defined $aliases->{$pIndexRes->{$pIdx}};
- }
- #print Dumper($pIndexRes);
- #print Dumper($portnames);
- # Now, get MAC address table.
- $arpRes = $session->get_table(-baseoid => $arpTable) or die($session->error());
- #print Dumper($arpRes);
- for my $arpIdx (keys %$arpRes) {
- my $idx = $1 if $arpIdx =~ /$arpTable\.(.*)/;
- my $pn = sprintf('%02d',$arpRes->{$arpIdx});
- if ($idx =~ m/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/) {
- $mac = sprintf("%02x:%02x:%02x:%02x:%02x:%02x", $1, $2, $3, $4, $5, $6);
- }
- #print "$mac \n";
- if (!defined $portnames->{$pn}) {
- push (@{$macs->{$pn}}, $mac);
- $macports->{$mac} = $pn;
- }
- }
- #print Dumper($macs,$macports);
- #Get MACs from glpi file
- my @glpi;
- if ( -s "glpi.txt"){
- open(FILE,"glpi.txt");
- while(<FILE>){
- push (@glpi,$1) if ($_ =~ /^(\S+)\s+\S+/);
- }
- }
- else {
- print "GLPI file is empty\n";
- }
- #Find unknown MACs
- for my $port (keys %$macs) {
- print "$port : \n";
- foreach my $mac( @{$macs->{$port}} ) {
- my $i = 0;
- my $flag = 0;
- foreach my $glpi_mac (@glpi){
- last if ($mac eq $glpi_mac);
- $flag = 1 if ($i == $#glpi);
- $i++;
- }
- print "flg : $flag\n";
- # print my $out= `./portToGuest.pl $host $port $mac` if ($flag == 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement