Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Icingworld committed Jun 9, 2023
0 parents commit b8ed410
Show file tree
Hide file tree
Showing 15 changed files with 1,307 additions and 0 deletions.
Binary file added Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions NetTool.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
main.cpp \
mainwindow.cpp

HEADERS += \
mainwindow.h

FORMS += \
mainwindow.ui

TRANSLATIONS += \
NetTool_zh_CN.ts
CONFIG += lrelease
CONFIG += embed_translations

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

DISTFILES += \
logo.rc

RESOURCES += \
resources.qrc

RC_FILE += logo.rc
586 changes: 586 additions & 0 deletions NetTool.pro.user

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions NetTool_zh_CN.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="zh_CN"></TS>
5 changes: 5 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 以太网切换工具

1. 需要管理员权限运行
2. set.txt为以太网适配器名字,需要更改为自己设备的名字
使用`ipconfig`查看以太网名字
2 changes: 2 additions & 0 deletions log1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

192.160.0.100
2 changes: 2 additions & 0 deletions log2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

192.160.0.105
Binary file added logo.ico
Binary file not shown.
1 change: 1 addition & 0 deletions logo.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IDI_ICON1 ICON DISCARDABLE "logo.ico"
23 changes: 23 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "mainwindow.h"

#include <QApplication>
#include <QLocale>
#include <QTranslator>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QTranslator translator;
const QStringList uiLanguages = QLocale::system().uiLanguages();
for (const QString &locale : uiLanguages) {
const QString baseName = "FlashLight_" + QLocale(locale).name();
if (translator.load(":/i18n/" + baseName)) {
a.installTranslator(&translator);
break;
}
}
MainWindow w;
w.show();
return a.exec();
}
214 changes: 214 additions & 0 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("网络切换工具 by jjjzw");
setWindowFlags(windowFlags()& ~Qt::WindowMaximizeButtonHint);
setWindowIcon(QIcon(":/Icon.png"));
setFixedSize(this->width(), this->height());
ReadLogFile("log1.txt", ui->choose1, list1);
ReadLogFile("log2.txt", ui->choose2, list2);
EthernetName = ReadSetFile();
GetNetStatus();
}

MainWindow::~MainWindow()
{
delete ui;
}

QString MainWindow::ReadSetFile()
{
QString filePath = ":/set.txt";
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
Message("配置文件出错,请检查");
close();
}
QTextStream in(&file);
QString text = in.readAll().remove("\n");
file.close();
return text;
}

void MainWindow::GetNetStatus()
{
ui->adapter->setText(EthernetName);
QProcess process;
process.start("ipconfig");
process.waitForFinished();
QByteArray output = process.readAllStandardOutput();
QString outputStr = QString::fromLocal8Bit(output);
int ethernetIndex = outputStr.indexOf(EthernetName);
int nextAdapterIndex = outputStr.indexOf("\r\n\r\n", ethernetIndex + 10);
QString ethernetInfo = outputStr.mid(ethernetIndex, nextAdapterIndex - ethernetIndex);
QStringList lines = ethernetInfo.split("\r\n");
NetStatus net;
for (const QString & line : lines) {
if (line.contains("IPv4 地址")) {
net.ip = line.split(":").value(1).trimmed();
} else if (line.contains("子网掩码")) {
net.mask = line.split(":").value(1).trimmed();
} else if (line.contains("默认网关")) {
net.gate = line.split(":").value(1).trimmed();
}
}
ShowNetStatus(net);
}

bool MainWindow::isValid(const QString & string, const QString & pattern)
{
QRegularExpression regex(pattern);
QRegularExpressionMatch match = regex.match(string);
return match.hasMatch();
}

void MainWindow::ShowNetStatus(const NetStatus & net)
{
ui->ipnow->setText(net.ip);
ui->masknow->setText(net.mask);
ui->gatenow->setText(net.gate);
}

void MainWindow::Message(const QString & string)
{
QMessageBox messageBox;
messageBox.setWindowTitle("提示");
messageBox.setText(string);
messageBox.setIcon(QMessageBox::Information);
messageBox.addButton("确定", QMessageBox::AcceptRole);
messageBox.exec();
}

void MainWindow::ReadLogFile(const QString & path, QComboBox * box, QStringList & list)
{
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
}
QTextStream in(&file);
while (!in.atEnd()) {
list << in.readLine().remove("\n");
}
box->addItems(list);
file.close();
}

void MainWindow::WriteLogFile(const QString & path, const QString & string)
{
QFile file(path);
if (!file.open(QIODevice::Append | QIODevice::Text)) {
}
QTextStream out(&file);
out << string << "\n";
out.flush();
file.close();
}

void MainWindow::on_choose1_currentTextChanged(const QString & arg1)
{
ui->ipnew->setText(arg1);
}

void MainWindow::on_choose2_currentTextChanged(const QString & arg1)
{
ui->tobepinged->setText(arg1);
}

void MainWindow::on_pushButton_clicked()
{
QProcess process;
if (ui->dhcp->isChecked())
{
process.start("netsh interface ip set address name=\"" + EthernetName + "\" dhcp");
} else
{
if (!isValid(ui->ipnew->text(), "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"))
{
Message("请输入正确的IP");
} else if (!isValid(ui->masknew->text(), "^((128|192)|2(24|4[08]|5[245]))(\\.(0|(128|192)|2((24)|(4[08])|(5[245])))){3}$"))
{
Message("请输入正确的子网掩码");
} else if (ui->gatenew->text() != "" && !isValid(ui->gatenew->text(), "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"))
{
Message("请输入正确的网关");
} else {
if (!list1.contains(ui->ipnew->text()))
{
list1 << ui->ipnew->text();
ui->choose1->addItem(ui->ipnew->text());
WriteLogFile("log1.txt", ui->ipnew->text());
}
process.start("netsh interface ip set address name=\"" + EthernetName + "\" static " + ui->ipnew->text() + " " + ui->masknew->text() + " " + ui->gatenew->text());
}
}
process.waitForFinished();
//QByteArray output = process.readAllStandardOutput();
//QString outputStr = QString::fromLocal8Bit(output);
//qDebug() << outputStr;
ui->ipnow->setText("重新检测中");
ui->masknow->setText("重新检测中");
ui->gatenow->setText("重新检测中");
ui->ipnow->repaint();
ui->masknow->repaint();
ui->gatenow->repaint();
QThread::msleep(5000);
GetNetStatus();
}

void MainWindow::on_pushButton_2_clicked()
{
if (!isValid(ui->tobepinged->text(), "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"))
{
Message("请输入正确的IP");
} else {
if (!list2.contains(ui->tobepinged->text()))
{
list2 << ui->tobepinged->text();
ui->choose2->addItem(ui->tobepinged->text());
WriteLogFile("log2.txt", ui->tobepinged->text());
}
ui->status->setText("检测中...");
ui->status->repaint();
QProcess process;
QString command = ui->tobepinged->text();
process.start("ping -n 1 " + command);
process.waitForFinished();
QByteArray output = process.readAllStandardOutput();
QString outputStr = QString::fromLocal8Bit(output);
if (outputStr.contains("回复"))
{
ui->status->setText("连通");
} else if (outputStr.contains("超时"))
{
ui->status->setText("未连通");
}
}
}

void MainWindow::on_dhcp_stateChanged(int arg1)
{
if (arg1 == 2)
{
ui->ipnew->setEnabled(false);
ui->masknew->setEnabled(false);
ui->gatenew->setEnabled(false);
ui->choose1->setEnabled(false);
} else {
ui->ipnew->setEnabled(true);
ui->masknew->setEnabled(true);
ui->gatenew->setEnabled(true);
ui->choose1->setEnabled(true);
}
}


void MainWindow::on_check_clicked()
{
EthernetName = ui->adapter->text();
GetNetStatus();
}

62 changes: 62 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma execution_character_set("utf-8")
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
#include <QProcess>
#include <QMessageBox>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QFile>
#include <QTextStream>
#include <QComboBox>
#include <QThread>
#include <QTime>
#include <QMetaObject>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

struct NetStatus
{
QString ip;
QString mask;
QString gate;
};

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
void GetNetStatus();
bool isValid(const QString& string, const QString & pattern);
void ShowNetStatus(const NetStatus & net);
void Message(const QString & string);
QString ReadSetFile();
void ReadLogFile(const QString & path, QComboBox * box, QStringList & list);
void WriteLogFile(const QString & path, const QString & string);
QString EthernetName;
QStringList list1;
QStringList list2;
~MainWindow();

public slots:
void on_choose1_currentTextChanged(const QString & arg1);
void on_choose2_currentTextChanged(const QString & arg1);
void on_pushButton_clicked();
void on_pushButton_2_clicked();

private slots:
void on_dhcp_stateChanged(int arg1);

void on_check_clicked();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
Loading

0 comments on commit b8ed410

Please sign in to comment.