Advertisement
Guest User

Untitled

a guest
Nov 15th, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use Expect;
  3. use Data::Dumper;
  4. use Net::SSH::Expect;
  5. use Net::SNMP qw(snmp_dispatcher oid_lex_sort);
  6.  
  7.  
  8. my $host = $ARGV[0];
  9.  
  10. my $ssh = Net::SSH::Expect->new (
  11. host => "$host",
  12. password=> 'PXEQvgos',
  13. user => 'root',
  14. raw_pty => 1,
  15. # log_file => "log_file",
  16. timeout => 5
  17. );
  18.  
  19. my ($session, $error) = Net::SNMP->session(
  20. -hostname => $host,
  21. -community => 'sysiq',
  22. -port => '161',
  23. -version => 'snmpv2c'
  24. );
  25.  
  26. # Was the session created?
  27. if (!defined($session)) {
  28. printf("ERROR: %s.\n", $error);
  29. exit 1;
  30. }
  31.  
  32. my $aliases;
  33. my $ports;
  34. my $portnames;
  35. my $macports;
  36.  
  37. my $ifTable = '1.3.6.1.2.1.2.2';
  38. my $ifAlias = '1.3.6.1.2.1.31.1.1.1.18';
  39. my $arpTable = '1.3.6.1.2.1.17.4.3.1.2';
  40. my $portIndex = '1.3.6.1.2.1.17.1.4.1.2';
  41. my $portStatus = '1.3.6.1.2.1.2.2.1.8';
  42.  
  43. my $result;
  44.  
  45. # Get ifAliases
  46. $ifAliasRes = $session->get_table(-baseoid => $ifAlias) or die($session->error());
  47.  
  48. for my $ifA (keys %$ifAliasRes) {
  49. my $desc = $ifAliasRes->{$ifA};
  50. my $idx = $1 if $ifA =~ /$ifAlias\.(.*)/;
  51. next if $desc eq '';
  52. next if $desc =~ /.*\d+ Interface/; # Unnamed ports on HP 1910 are labeled "Ethernet1/0/10 Interface"
  53. #print "ifA: $idx desc: $desc \n";
  54. $aliases->{$idx} = $desc;
  55.  
  56. }
  57.  
  58.  
  59. #Get port status
  60. $pStatusRes = $session->get_table(-baseoid => $portStatus) or die($session->error());
  61.  
  62. for my $pIndex (keys %$pStatusRes) {
  63. my $pStatus = $pStatusRes->{$pIndex};
  64. print my $idx = $1 if $pIndex =~ /$portStatus\.(.*)/;
  65. $portStatusTable->{$idx} = $pStatus;
  66. }
  67. print Dumper($portStatusTable);
  68.  
  69. #Get blocked ports
  70. my $blocklog = "./blockswports.log";
  71. if ( -s "$blocklog"){
  72. open(FILE,"$blocklog");
  73. while(<FILE>){
  74. chomp ($_);
  75. if ($_ =~ /^(\S+)\|(\S+)\|(\S+)\|(\S+)/){
  76. print $blockhost = "$1";
  77. print "QQ\n" if ($host eq $blockhost);
  78. }
  79. }
  80. }
  81. else {
  82. print "$blocklog file is empty\n";
  83. }
  84.  
  85.  
  86. #Get port indexes and fill port names
  87. $pIndexRes = $session->get_table(-baseoid => $portIndex) or die($session->error());
  88.  
  89. for my $pIdx (keys %$pIndexRes) {
  90. my $idx = sprintf('%02d',$1) if $pIdx =~ /$portIndex\.(.*)/;
  91. $ports->{$idx} = $pIndexRes->{$pIdx};
  92. $portnames->{$idx} = $aliases->{$pIndexRes->{$pIdx}} if defined $aliases->{$pIndexRes->{$pIdx}};
  93. }
  94. #print Dumper($pIndexRes);
  95. #print Dumper($portnames);
  96.  
  97.  
  98. # Now, get MAC address table.
  99.  
  100. $arpRes = $session->get_table(-baseoid => $arpTable) or die($session->error());
  101.  
  102. #print Dumper($arpRes);
  103.  
  104. for my $arpIdx (keys %$arpRes) {
  105. my $idx = $1 if $arpIdx =~ /$arpTable\.(.*)/;
  106. my $pn = sprintf('%02d',$arpRes->{$arpIdx});
  107. if ($idx =~ m/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/) {
  108. $mac = sprintf("%02x:%02x:%02x:%02x:%02x:%02x", $1, $2, $3, $4, $5, $6);
  109. }
  110. #print "$mac \n";
  111. if (!defined $portnames->{$pn}) {
  112. push (@{$macs->{$pn}}, $mac);
  113. $macports->{$mac} = $pn;
  114. }
  115. }
  116.  
  117. #print Dumper($macs,$macports);
  118.  
  119. #Get MACs from glpi file
  120. my @glpi;
  121. if ( -s "glpi.txt"){
  122. open(FILE,"glpi.txt");
  123. while(<FILE>){
  124. push (@glpi,$1) if ($_ =~ /^(\S+)\s+\S+/);
  125. }
  126. }
  127. else {
  128. print "GLPI file is empty\n";
  129. }
  130. #Find unknown MACs
  131. for my $port (keys %$macs) {
  132. print "$port : \n";
  133. foreach my $mac( @{$macs->{$port}} ) {
  134. my $i = 0;
  135. my $flag = 0;
  136. foreach my $glpi_mac (@glpi){
  137. last if ($mac eq $glpi_mac);
  138. $flag = 1 if ($i == $#glpi);
  139. $i++;
  140. }
  141.  
  142. print "flg : $flag\n";
  143. # print my $out= `./portToGuest.pl $host $port $mac` if ($flag == 1);
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement