diff --git a/scripts/smtp-check b/scripts/smtp-check new file mode 100755 index 0000000..7971df6 --- /dev/null +++ b/scripts/smtp-check @@ -0,0 +1,63 @@ +#!/bin/bash + +function usage { + echo 'usage: [USERNAME PASSWORD]' + exit 1 +} + +function connect_server { + openssl s_client -starttls smtp -connect "$server:587" -ign_eof 2>/dev/null </dev/null < port 587 ... " + if connect_server | grep -q CONNECTED; then + echo SUCCESS + else + echo FAILURE && exit 1 + fi +} + +function check_smtp_auth { + echo -n "check smtp authentication <$server> port 587 username=<$username> ... " + set +e + output=$(auth_server) + if echo "$output" | grep -q CONNECTED; then + if echo "$output" | grep -q "Authentication successful"; then + echo OK + else + echo wrong username or password! && exit 2 + fi + else + echo wrong server connection! && exit 1 + fi + set -e +} + +### MAIN + +set -Eeuo pipefail + +server=${1:-} +username=${2:-} +password=${3:-} + +[[ -z "$server" ]] && usage + +if [[ -z "$username" ]]; then + check_smtp_connection +else + [[ -z "$password" ]] && usage + check_smtp_auth +fi diff --git a/scripts/smtp-chek b/scripts/smtp-chek deleted file mode 100755 index cbcbc8a..0000000 --- a/scripts/smtp-chek +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -username=postmaster@mangue-et-solidaire.re -password= -base64=$(echo -ne "\0$username\0$password" | base64) - -openssl s_client -starttls smtp -connect mail1.zourit.net:587 -ign_eof < [ https | 443 | smtps | 587 | pop3 | 993 | imap | 995 | ALL ]' - exit -1 + exit 1 } function check_ssl { local protocol=$1 case $protocol in - SMTPS ) - local extra="-starttls smtp -showcerts" - ;; + SMTPS) + local extra="-starttls smtp -showcerts" + ;; esac - - echo -n "$protocol " + echo -n "$protocol " certificate_info=$(echo | timeout $TIMEOUT openssl s_client $extra -connect $DOMAIN:$2 2>/dev/null) issuer=$(echo "$certificate_info" | openssl x509 -noout -text 2>/dev/null | grep Issuer: | cut -d: -f2) - date=$( echo "$certificate_info" | openssl x509 -noout -enddate 2>/dev/null | cut -d'=' -f2) + date=$(echo "$certificate_info" | openssl x509 -noout -enddate 2>/dev/null | cut -d'=' -f2) date_s=$(date -d "${date}" +%s) now_s=$(date -d now +%s) - date_diff=$(( (date_s - now_s) / 86400 )) + date_diff=$(((date_s - now_s) / 86400)) if [[ -z $date ]]; then echo -n "does not respond " echo -ne "\033[31;1m" echo FAILURE - (( result += 1 )) + ((result += 1)) elif [[ $date_diff -gt 20 ]]; then echo -n "issuer:$issuer " echo -n "will expire in $date_diff days " echo -ne "\033[32;1m" echo ok - elif [[ $date_diff -gt 0 ]];then + elif [[ $date_diff -gt 0 ]]; then echo -n "issuer:$issuer " echo -n "will expire in $date_diff days " echo -ne "\033[31;1m" echo WARNING - (( result += 1 )) + ((result += 1)) else echo -n "issuer:$issuer " echo -n "has already expired $date_diff ago " echo -ne "\033[31;1m" echo FAILURE - (( result += 1 )) + ((result += 1)) fi echo -ne "\033[0m" } @@ -58,23 +57,27 @@ function check_ssl { #MAIN [[ -z "$DOMAIN" ]] && usage case $PROTOCOL in - https | 443 ) - check_ssl HTTPS 443;; - smtps | 587 ) - check_ssl SMTPS 587;; - pop3 | 995 ) - check_ssl POP3 995;; - imap | 993 ) - check_ssl IMAP 993;; - all | ALL ) - check_ssl HTTPS 443 - check_ssl SMTPS 587 - check_ssl POP3 995 - check_ssl IMAP 993 - ;; - *) - usage - ;; +https | 443) + check_ssl HTTPS 443 + ;; +smtps | 587) + check_ssl SMTPS 587 + ;; +pop3 | 995) + check_ssl POP3 995 + ;; +imap | 993) + check_ssl IMAP 993 + ;; +all | ALL) + check_ssl HTTPS 443 + check_ssl SMTPS 587 + check_ssl POP3 995 + check_ssl IMAP 993 + ;; +*) + usage + ;; esac exit "$result"