系统账户密码的前提下,再把用户名与密码插入mysql的system_user库的windows表,并记录ip和时间,然后密码可以自动发送到邮箱
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import subprocess
import mysql.connector
from datetime import datetime
import socket
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# 定义要更改密码的用户名
username = 'your_username'
# 生成新的密码
new_password = 'your_new_password'
# 更改系统用户密码
subprocess.run('echo "{}:{}" | chpasswd'.format(username, new_password), shell=True)
# 获取当前IP
ip_address = socket.gethostbyname(socket.gethostname())
# 获取当前时间
current_time = datetime.now()
# 连接到MySQL数据库,插入新的用户名、密码、IP和时间
cnx = mysql.connector.connect(user='mysql_user', password='mysql_password', host='localhost', database='system_user')
cursor = cnx.cursor()
query = ("INSERT INTO windows (username, password, ip, time) VALUES (%s, %s, %s, %s)")
data = (username, new_password, ip_address, current_time)
cursor.execute(query, data)
cnx.commit()
cursor.close()
cnx.close()
# 定义邮件服务器、邮件账户、邮件密码和收件人
smtp_server = 'your_smtp_server'
mail_user = 'your_mail_username'
mail_pass = 'your_mail_password'
to_email = 'receiver_email_address'
# 发送邮件
msg = MIMEMultipart()
msg['From'] = mail_user
msg['To'] = to_email
msg['Subject'] = 'Password Changed'
body = 'The password for user {} has been changed to {}. The new credentials were inserted into the database at {} from IP {}.'.format(username, new_password, current_time, ip_address)
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP(smtp_server, 587)
server.starttls()
server.login(mail_user, mail_pass)
text = msg.as_string()
server.sendmail(mail_user, to_email, text)
server.quit()
# Load MySQL .NET Connector Objects
[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
# Create a new password
$password = -join ((65..90) + (97..122) + (48..57) | Get-Random -Count 10 | % {[char]$_})
# Change the password
$user = "test01"
net user $user $password
# Get the IP address
$ip = (Test-Connection -ComputerName (hostname) -Count 1).IPV4Address.IPAddressToString
# Insert the new password into MySQL
$connectionString = "server=120.51.29.11;uid=root;pwd=FE64CasZkh3XCshPFw;database=vmware"
$connection = New-Object MySql.Data.MySqlClient.MySqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
$query = "INSERT INTO windows (username, password, ip, timestamp) VALUES ('$user', '$password', '$ip', NOW())"
$command = New-Object MySql.Data.MySqlClient.MySqlCommand($query, $connection)
$command.ExecuteNonQuery()
$connection.Close()
# Send the new password to the email address
$EmailFrom = "devopsssdh@163.com"
$EmailTo = "1800asdsad@163.com"
$Subject = "New password"
$Body = "Username: $user`nNew Password: $password"
$SMTPServer = "smtp.163.com"
$SMTPPort = 587
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($EmailFrom, "qazwsdfsx13")
$MailMessage = New-Object System.Net.Mail.MailMessage
$MailMessage.From = $EmailFrom
$MailMessage.To.Add($EmailTo)
$MailMessage.Subject = $Subject
$MailMessage.Body = $Body
$SMTPClient.Send($MailMessage)
CREATE TABLE windows (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
ip VARCHAR(255) NOT NULL,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);