#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QProcess>
#include <QDebug>
#include <QTimer>
//connect(process, &QProcess::readyReadStandardOutput, [=]() {
// // 读取进程的标准输出
// QByteArray output = process->readAllStandardOutput();
// QString outputText = QString::fromUtf8(output); // 使用本地编码转换成 QString
// // 寻找并提取"错误消息:"后的内容
// int index = outputText.indexOf("错误消息:");
// if (index != -1) {
// QString errorMessage = outputText.mid(index + QString("错误消息:").length()).trimmed();
// ui->textBrowser->append(errorMessage);
// }
//});
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton_5, &QPushButton::clicked, this, &MainWindow::on_pushButton_5_clicked);
connect(ui->pushButton_6, &QPushButton::clicked, this, &MainWindow::on_pushButton_6_clicked);
// 读取cmsdk.ini中的ip和port,并显示在lineEdit和lineEdit_2上
QFile file("D:/SDFCheck/cmsdk.ini");
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine().trimmed();
if (line.startsWith("ip = "))
{
QString ip = line.mid(5).trimmed(); // 获取ip地址
ui->lineEdit->setText(ip);
}
else if (line.startsWith("port = "))
{
QString port = line.mid(7).trimmed(); // 获取端口号
ui->lineEdit_2->setText(port);
}
}
file.close();
}
else
{
QMessageBox::warning(this, "Error", "Failed to open cmsdk.ini for reading.");
}
}
// 槽函数,处理按钮点击事件
void MainWindow::on_pushButton_5_clicked()
{
QString newIp = ui->lineEdit->text().trimmed(); // 获取并清理IP地址
QString newPort = ui->lineEdit_2->text().trimmed(); // 获取并清理端口号
// 打开cmsdk.ini文件进行更新
QFile file("D:/SDFCheck/cmsdk.ini");
if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
{
QMessageBox::warning(this, "Error", "Failed to open cmsdk.ini for writing.");
return;
}
QTextStream in(&file);
QStringList lines;
bool foundServerSection = false;
while (!in.atEnd())
{
QString line = in.readLine();
if (!line.trimmed().isEmpty())
{
if (line.startsWith("[server]"))
{
foundServerSection = true;
lines.append(line); // 添加[section]行
continue;
}
if (foundServerSection && line.contains("="))
{
QStringList parts = line.split("=");
QString key = parts[0].trimmed();
if (key == "ip")
{
line = "ip = " + newIp; // 更新IP地址
}
else if (key == "port")
{
line = "port = " + newPort; // 更新端口号
}
}
}
lines.append(line); // 添加其他行
}
file.resize(0); // 清空文件内容
QTextStream out(&file);
// 重新写入更新后的内容
for (const QString &line : lines)
{
out << line << endl;
}
file.close();
QMessageBox::information(this, "Success", "Updated cmsdk.ini successfully.");
}
//void MainWindow::on_pushButton_6_clicked() {
// QString ipSubnet = ui->lineEdit_3->text();
// QString command = QString("nmap -sS %1").arg(ipSubnet);
// // Create a QProcess instance
// QProcess *process = new QProcess(this);
// process->setWorkingDirectory("C:/Program Files (x86)/Nmap");
// // Connect signals to handle process output
// connect(process, &QProcess::readyReadStandardOutput, [this, process]() {
// QByteArray output = process->readAllStandardOutput();
// QString outputText = QString::fromLocal8Bit(output);
// // Filter out "Active code page: 65001" message
// if (!outputText.contains("Active code page: 65001")) {
// ui->textBrowser_5->append(outputText);
// }
// });
// connect(process, &QProcess::readyReadStandardError, [this, process]() {
// QByteArray output = process->readAllStandardError();
// QString outputText = QString::fromLocal8Bit(output);
// ui->textBrowser_5->append(outputText);
// });
// // Start the process with the command
// process->start("cmd", QStringList() << "/c" << command);
//}
//void MainWindow::on_pushButton_6_clicked() {
// QString ipSubnet = ui->lineEdit_3->text();
// // Check if input is empty
// if (ipSubnet.isEmpty()) {
// ui->textBrowser_5->append("请输入扫描网段!");
// return; // Exit the function if input is empty
// }
// QString command = QString("nmap -sS %1").arg(ipSubnet);
// // Create a QProcess instance
// QProcess *process = new QProcess(this);
// process->setWorkingDirectory("C:/Program Files (x86)/Nmap");
// // Create a QTimer for timeout detection
// QTimer *timeoutTimer = new QTimer(this);
// timeoutTimer->setInterval(20000); // 20 seconds
// timeoutTimer->setSingleShot(true);
// // Connect the timeout signal to handle the timeout case
// connect(timeoutTimer, &QTimer::timeout, [this, process]() {
// if (process->state() == QProcess::Running) {
// process->terminate(); // Terminate the process if still running
// ui->textBrowser_5->append("扫描超时!");
// }
// });
// // Connect signals to handle process output
// connect(process, &QProcess::readyReadStandardOutput, [this, process]() {
// QByteArray output = process->readAllStandardOutput();
// QString outputText = QString::fromLocal8Bit(output);
// // Filter out "Active code page: 65001" message
// if (!outputText.contains("Active code page: 65001")) {
// ui->textBrowser_5->append(outputText);
// }
// });
// connect(process, &QProcess::readyReadStandardError, [this, process]() {
// QByteArray output = process->readAllStandardError();
// QString outputText = QString::fromLocal8Bit(output);
// ui->textBrowser_5->append(outputText);
// });
// // Connect the finished signal to stop the timer if the process completes
// connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), [timeoutTimer]() {
// timeoutTimer->stop(); // Stop the timer if the process finishes in time
// });
// // Start the timer
// timeoutTimer->start();
// // Start the process with the command
// process->start("cmd", QStringList() << "/c" << command);
//}
void MainWindow::on_pushButton_6_clicked() {
QString ipSubnet = ui->lineEdit_3->text();
if (ipSubnet.isEmpty()) {
ui->textBrowser_5->setText("请输入扫描网段!");
return;
}
QString command = QString("nmap -sS %1").arg(ipSubnet);
QProcess *process = new QProcess(this);
process->setWorkingDirectory("C:/Program Files (x86)/Nmap");//nmap目录
QTimer *timeoutTimer = new QTimer(this);
timeoutTimer->setInterval(20000); // 20 seconds
timeoutTimer->setSingleShot(true);
connect(timeoutTimer, &QTimer::timeout, [this, process]() {
if (process->state() == QProcess::Running) {
process->terminate();
ui->textBrowser_5->append("扫描超时!");
}
});
connect(process, &QProcess::readyRead
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
SDFCheck.zip (18个子文件)
SDFCheck
cmsdk.ini 54B
build-SDFCheck-Desktop_Qt_5_12_9_MinGW_64_bit-Debug
Makefile 31KB
debug
moc_mainwindow.cpp 3KB
moc_mainwindow.o 427KB
mainwindow.o 593KB
SDFCheck.exe 1.4MB
main.o 516KB
moc_predefs.h 14KB
release
Makefile.Release 46KB
ui_mainwindow.h 9KB
Makefile.Debug 45KB
.qmake.stash 1KB
SDFCheck
SDFCheck.pro.user 23KB
mainwindow.h 421B
mainwindow.cpp 10KB
SDFCheck.pro 1KB
main.cpp 183B
mainwindow.ui 8KB
共 18 条
- 1
资源评论
不爱学习的小刘
- 粉丝: 36
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功