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