Guest User

Untitled

a guest
Jun 18th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.98 KB | None | 0 0
  1.      1  #!/usr/bin/perl
  2.      2 
  3.      3  use warnings;
  4.      4  use strict;
  5.      5  use Switch;
  6.      6  use Net::SSH::Expect;
  7.      7  use Data::Dumper;
  8.      8  my %hosts=();
  9.      9  my $login;
  10.     10  my @sn;
  11.     11  my %stoh=();
  12.     12  open(HSTS, "<", "/etc/hosts") or die "Unable to open: $!";
  13.     13  my $type=$ARGV[0];
  14.     14  my $cmd;
  15.     15  my $passwd;
  16.     16  my $user;
  17.     17 
  18.     18  if($#ARGV != 0) {
  19.     19      print_usage();
  20.     20  }
  21.     21  switch($type) {
  22.     22      case('tippingpoint') { $cmd = "show version\r"; $passwd = '<removed>'; $user= '<removed>';}
  23.     23      case('cisco') { $cmd = "show version\r"; $passwd = "<removed>"; $user='<removed>';}
  24.     24      case('mcafee') {$cmd = "show\r"; $passwd = '<removed>'; $user='<removed>'; }
  25.     25  }
  26.     26  if($type eq 'tippingpoint') {
  27.     27 
  28.     28      while(<HSTS>) {
  29.     29 
  30.     30          chomp;
  31.     31          if(/MTI/i) {
  32.     32              my @split=split(/\s+/);
  33.     33              next if($split[0] =~ /^#/);
  34.     34              $hosts{$split[0]}=$split[1];
  35.     35          }
  36.     36      }
  37.     37  getSerial();
  38.     38  }
  39.     39  elsif($type eq 'cisco') {
  40.     40 
  41.     41      while(<HSTS>) {
  42.     42 
  43.     43          chomp;
  44.     44          if(/MID/i) {
  45.     45              my @split=split(/\s+/);
  46.     46              next if($split[0] =~ /^#/i);
  47.     47              $hosts{$split[0]}=$split[1];
  48.     48          }
  49.     49      }
  50.     50  getSerial();
  51.     51  #print Dumper(%hosts);
  52.     52  }
  53.     53  elsif($type eq 'mcafee') {
  54.     54 
  55.     55      while(<HSTS>) {
  56.     56         
  57.     57          chomp;
  58.     58          if(/MII/i) {
  59.     59              my @split=split(/\s+/);
  60.     60              next if($split[0] =~ /^#|XXX/i);
  61.     61              $hosts{$split[0]}=$split[1];
  62.     62          }
  63.     63 
  64.     64      }
  65.     65  getSerial();
  66.     66  }
  67.     67 
  68.     68 
  69.     69 
  70.     70 
  71.     71  sub getSerial {
  72.     72      foreach my $key(keys %hosts) {
  73.     73          my $ssh = Net::SSH::Expect->new (
  74.     74                      host => "$hosts{$key}",
  75.     75                      password=> "$passwd",
  76.     76                      user => "$user",
  77.     77                      raw_pty => 1,
  78.     78                  timeout => 5,
  79.     79              ssh_option => '-oStrictHostKeyChecking=no',
  80.     80              );
  81.     81          eval {
  82.     82              $login = $ssh->login();
  83.     83          };
  84.     84 
  85.     85          switch($type) {
  86.     86              case('tippingpoint') {
  87.     87              if($login =~ /#/) {
  88.     88                  $ssh->waitfor('# ');
  89.     89                  $ssh->send("$cmd\r");
  90.     90                  my $line;
  91.     91 
  92.     92                  while ( defined ($line = $ssh->read_line()) ) {
  93.     93 
  94.     94                  if($line =~ /Serial/) {
  95.     95                      @sn=split(/:/,$line);
  96.     96                      $stoh{$hosts{$key}}=$sn[1];
  97.     97                 
  98.     98                      }
  99.     99                  }
  100.    100                  $ssh->close();
  101.    101         
  102.    102              }
  103.    103                  else {
  104.    104                      print "Login wasn't successful: $login\n";
  105.    105                  }
  106.    106          }
  107.    107              case('cisco')
  108.    108              {
  109.    109                  if($login =~ /cont/)
  110.    110                  {
  111.    111                      $ssh->exec("\r");  
  112.    112                      $ssh->waitfor('$ ');
  113.    113                      $ssh->send("echo \"show version\" | /usr/cids/idsRoot/bin/cidcli");
  114.    114                      my $line;
  115.    115                      while ( defined ($line = $ssh->read_line()) )
  116.    116                      {
  117.    117                          if($line =~ /Serial/)
  118.    118                          {
  119.    119                              @sn=split(/:/,$line);
  120.    120                      #       print "\n\n$key\t\t$hosts{$key}\t\t$sn[1]\n\n";
  121.    121                              $stoh{$hosts{$key}}=$sn[1];
  122.    122 
  123.    123                          }
  124.    124                      }
  125.    125                      $ssh->close();
  126.    126                  }
  127.    127                  else
  128.    128                  {
  129.    129                      print "Login wasn't successful: $login \t\t $hosts{$key}\n";
  130.    130                  }
  131.    131              }  
  132.    132              case('mcafee')
  133.    133              {
  134.    134                  if($login =~ />/)
  135.    135                  {
  136.    136                      $ssh->waitfor('> ');
  137.    137                      $ssh->send("$cmd");
  138.    138                      my $line;
  139.    139             
  140.    140                      while ( defined ($line = $ssh->read_line()) )
  141.    141                      {
  142.    142                 
  143.    143                          if($line =~ /Serial/)
  144.    144                          {
  145.    145                              @sn=split(/:/,$line);
  146.    146                              $stoh{$hosts{$key}}=$sn[1];
  147.    147                          }
  148.    148                      }  
  149.    149                      $ssh->close();
  150.    150 
  151.    151                  }
  152.    152                  else
  153.    153                  {
  154.    154                      print "Login wasn't successful: $login $hosts{$key}\n";
  155.    155                  }
  156.    156              }  
  157.    157  }
  158.    158  }
  159.    159  #print Dumper(%stoh);
  160.    160  sendReport();
  161.    161  }
  162.    162  sub sendReport {
  163.    163     
  164.    164      my $sendmail = "/usr/sbin/sendmail -t";
  165.    165      my $reply_to = "Reply-to: <address removed>\n";
  166.    166      my $subject = "Subject: MSS Device Serials: $type\n";
  167.    167      #my $to = "To: <addresses removed>\n";
  168.    168      my $to = "To: <addressed removed>\n";
  169.    169      open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
  170.    170      print SENDMAIL $reply_to;
  171.    171      print SENDMAIL $subject;
  172.    172      print SENDMAIL $to;
  173.    173      print SENDMAIL "Content-type: text/plain\n\n";
  174.    174      foreach my $x(keys %stoh) {
  175.    175          print SENDMAIL "$x\t\t$stoh{$x}\n";
  176.    176      }
  177.    177     
  178.    178 
  179.    179  }
  180.    180  sub print_usage {
  181.    181 
  182.    182      print "Provide the type of device as the first option to the script (tp, cisco, sourcefire).\nThe second option should be the command to display the serial number with double quotes: \"show version\"\n";
  183.    183      exit(1);
  184.    184  }
Add Comment
Please, Sign In to add comment