[chatGPT 測試] 寫一隻 php 連線 mysql 檢查指定的路徑 低於設定的百分比,進行發信

寫一隻php,功能需求如下
1.連線MySQL Server
2.使用SScan資料庫
3.讀取Path_List
4.資料表中
Server 遠端路徑
Path 檢查路徑欄位
Mount_Point 掛載點
Account 帳號
Password 密碼
Percen 百分比
5.使用 exec 語法
使用 mount 指令
用 Account / Password 通過 Server的驗證
掛載Path到Mount_Point上
檢查總空間,可用空間,已用空間
6.如果可用空間低於Percen 設定的百分比
使用phpmailer發信通知
7.另外要有副本收件者

ChatGPT 寫出來的程式碼如下
<?php

require ‘vendor/autoload.php’; // 引入 PHPMailer 库

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// 邮件设置
$mail = new PHPMailer(true);

try {
// 邮件服务器设置
$mail->SMTPDebug = SMTP::DEBUG_OFF; // 可以设置为DEBUG_SERVER以查看更多调试信息
$mail->isSMTP();
$mail->Host = ‘smtp.example.com’; // 你的SMTP服务器
$mail->SMTPAuth = true;
$mail->Username = ‘your_username’; // 你的SMTP用户名
$mail->Password = ‘your_password’; // 你的SMTP密码
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // 使用SMTPS加密
$mail->Port = 465; // SMTP端口

// 设置发件人
$mail->setFrom(‘[email protected]’, ‘Your Name’);

// 设置收件人
$mail->addAddress(‘[email protected]’, ‘Recipient Name’); // 主要收件人
$mail->addCC(‘[email protected]’, ‘CC Name’); // 抄送收件人

// 连接到MySQL数据库
$servername = “your_mysql_server”;
$username = “your_mysql_username”;
$password = “your_mysql_password”;
$database = “SScan”;

$conn = new mysqli($servername, $username, $password, $database);

if ($conn->connect_error) {
die(“数据库连接失败: ” . $conn->connect_error);
}

// 查询Path_List表
$sql = “SELECT Server, Path, Mount_Point, Account, Password, Percen FROM Path_List”;
$result = $conn->query($sql);

if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$server = $row[“Server”];
$path = $row[“Path”];
$mountPoint = $row[“Mount_Point”];
$account = $row[“Account”];
$password = $row[“Password”];
$percen = $row[“Percen”];

// 使用exec命令挂载路径
$mountCommand = “mount -t cifs -o username={$account},password={$password} //{$server}{$path} {$mountPoint}”;
exec($mountCommand);

// 检查磁盘空间
$diskTotalSpace = disk_total_space($mountPoint);
$diskFreeSpace = disk_free_space($mountPoint);
$diskUsedSpace = $diskTotalSpace – $diskFreeSpace;

// 如果可用空间低于设置的百分比,发送通知邮件
if (($diskFreeSpace / $diskTotalSpace) * 100 < $percen) {
$mail->Subject = ‘磁盘空间低于设定百分比’;
$mail->Body = “路径:{$mountPoint}\n总空间:{$diskTotalSpace}字节\n可用空间:{$diskFreeSpace}字节\n已用空间:{$diskUsedSpace}字节”;
$mail->send();
}

// 卸载路径
exec(“umount {$mountPoint}”);
}
} else {
echo “没有找到路径列表。”;
}

// 关闭数据库连接
$conn->close();
} catch (Exception $e) {
echo “邮件发送失败: {$mail->ErrorInfo}”;
}
?>

建立資料庫的SQL

CREATE TABLE Path_List (
SN INT AUTO_INCREMENT PRIMARY KEY,
Server VARCHAR(255) NOT NULL,
Path VARCHAR(255) NOT NULL,
Mount_Point VARCHAR(255) NOT NULL,
Account VARCHAR(255) NOT NULL,
Password VARCHAR(255) NOT NULL,
Percen DECIMAL(5, 2) NOT NULL
);

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *