#!/usr/bin/perl -w
##
## Brian T. Hunter
## 04/27/2022
## Script to check SSL Status, like ssllabs.com
#a Crontab of:  0 8 * * 1 /home/hchadmin/bin/check_ssllabs.pl
###
## This Script calls the following Script to runs test against servers.
##	my $CMD1="/home/hchadmin/bin/ssllabs-scan-master/ssllabs-scan-v3 -grade $server";
##
## This Script calls the following BASH Script to runs test against servers.
## 	$CMD2="/home/hchadmin/bin/testssl.sh $server"; 
##


use strict;

my @SERVERS = qw(mail.sutechy.com www.sutechy.com email.sutechy.com weba.sutechy.com);
foreach my $server (@SERVERS) {
	my @DATA;
	my $datetime=`date +'%m%d%y_%H%M%S'`; 
	chomp($datetime); 

	my $logfile="/home/hchadmin/bin/logs/testssl.$datetime.txt";
	my $mailit="/home/hchadmin/bin/logs/testssl.$datetime.$server.txt";
	#unlink $mailit if($mailit);

	open(OUT,">>$mailit") or die ("Unable to write to file $mailit:$!\n"); 
	print OUT "=============================================================\n";

	# Gives Grade of protection
	my $CMD1="/home/hchadmin/bin/ssllabs-scan-master/ssllabs-scan-v3 -grade $server";
	system("$CMD1 1>$logfile");
	
	#my $CMD="/home/hchadmin/bin/testssl.sh --ssl-native mail.sutechy.com"; 
	my $CMD2="/home/hchadmin/bin/testssl.sh $server"; 
	system("$CMD2 1>>$logfile");
	
	open(FILE, "<$logfile") or warn "Unable to open $logfile :$!\n";
	while() {
	    $_ =~ s/\[1m|\[m|\[4m//g;
	    $_ =~ s/\[0;31m//g;
	    $_ =~ s/\[0;32m//g;
	    $_ =~ s/\[0;33m//g;
	    $_ =~ s/\[0;35m//g;
	    $_ =~ s/\[1;32m//g;
	    $_ =~ s/\[1;33m//g;
	    $_ =~ s/\[1;35m//g;
	    print OUT "$_\n";
	    push(@DATA, $_);
	 }
	close(FILE);
	print OUT "=============================================================\n";
	close(OUT);

	use MIME::Lite;
	MIME::Lite->send ("smtp", "mail.sutechy.com");
	my $msg = MIME::Lite->new
	  (
	   From    => 'admin ',
	   To      => 'bhunter@sutechy.com', 
	   Subject => "$server SSLLABS STATUS",
	   Type    => 'multipart/mixed',
	  );
	
	$msg->attach(Type => 'TEXT', Data => @DATA);
	$msg->attach(Type => 'TEXT', Path => $mailit);
	
	$msg->send ();

}#foreach{}
exit(0);