----Windows script for password expiry----
# MySQL Database Connection Information
$MySQLServer = "your_mysql_server"
$MySQLUser = "your_mysql_username"
$MySQLPassword = "your_mysql_password"
$DatabaseName = "your_mysql_database"
# Email Configuration
$EmailServer = "your_smtp_server"
$EmailFrom = "sender@[Link]"
$EmailTo = "recipient@[Link]"
$EmailSubject = "MySQL Password Expiry Alert"
$EmailBody = "The following MySQL users' passwords will expire within 20
days:`r`n`r`n"
# Calculate the date 20 days from now in YYYYMMDD format
$ExpirationDate = (Get-Date).AddDays(20).ToString("yyyyMMdd")
# Query MySQL to get user password expiration information
$Query = @"
SELECT user, host, password_expire
FROM [Link]
WHERE DATE_FORMAT(password_expire, '%Y%m%d') <= '$ExpirationDate';
"@
# Execute the MySQL command-line client with the query
$Result = & mysql -u $MySQLUser -p$MySQLPassword -h $MySQLServer -e $Query
$DatabaseName
# Check if there are any results
if ($Result -match "User\s+Host\s+Password_expire") {
Write-Host "No MySQL users found with passwords expiring within 20 days."
} else {
# Parse the result and format the email body
$EmailBody += $Result
Send-MailMessage -SmtpServer $EmailServer -From $EmailFrom -To $EmailTo -
Subject $EmailSubject -Body $EmailBody
Write-Host "Email sent successfully."
}
----Linux password expiry----
#!/bin/bash
# MySQL Database Connection Information
MYSQL_SERVER="your_mysql_server"
MYSQL_USER="your_mysql_username"
MYSQL_PASSWORD="your_mysql_password"
MYSQL_DATABASE="your_mysql_database"
# Email Configuration
SMTP_SERVER="your_smtp_server"
EMAIL_FROM="sender@[Link]"
EMAIL_TO="recipient@[Link]"
EMAIL_SUBJECT="MySQL Password Expiry Alert"
EMAIL_BODY="The following MySQL users' passwords will expire within 20 days:
"
# Calculate the date 20 days from now in YYYYMMDD format
EXPIRATION_DATE=$(date -d "+20 days" "+%Y%m%d")
# Execute MySQL query to get user password expiration information
QUERY="SELECT user, host, password_expire FROM [Link] WHERE
DATE_FORMAT(password_expire, '%Y%m%d') <= '$EXPIRATION_DATE';"
# Run the MySQL query and store the result in a temporary file
mysql -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" -h "$MYSQL_SERVER" -e "$QUERY"
"$MYSQL_DATABASE" > /tmp/mysql_password_expiry.txt
# Check if there are any results
if [ -s /tmp/mysql_password_expiry.txt ]; then
EMAIL_BODY+="$(cat /tmp/mysql_password_expiry.txt)"
# Send the email
echo -e "$EMAIL_BODY" | mailx -s "$EMAIL_SUBJECT" -r "$EMAIL_FROM" "$EMAIL_TO"
echo "Email sent successfully."
else
echo "No MySQL users found with passwords expiring within 20 days."
fi
# Clean up the temporary file
rm /tmp/mysql_password_expiry.txt