Ping命令详解参数
用法:
ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS]
[-r count] [-s count] [[-j host-list] | [-k host-list]]
[-w timeout] [-R] [-S srcaddr] [-c compartment] [-p]
[-4] [-6] target_name
选项:
-t
Ping 指定的主机,直到停止。
若要查看统计信息并继续操作,请键入 Ctrl+Break;
若要停止,请键入 Ctrl+C。
-a
将地址解析为主机名。
-n count
要发送的回显请求数。
-l size
发送缓冲区大小。
-f
在数据包中设置“不分段”标记(仅适用于 IPv4)。
-i TTL
生存时间。
-v TOS
服务类型(仅适用于 IPv4。该设置已被弃用,
对 IP 标头中的服务类型字段没有任何
影响)。
-r count
记录计数跃点的路由(仅适用于 IPv4)。
-s count
计数跃点的时间戳(仅适用于 IPv4)。
-j host-list
与主机列表一起使用的松散源路由(仅适用于 IPv4)。
-k host-list
与主机列表一起使用的严格源路由(仅适用于 IPv4)。
-w timeout
等待每次回复的超时时间(毫秒)。
-R
同样使用路由标头测试反向路由(仅适用于 IPv6)。
根据 RFC 5095,已弃用此路由标头。
如果使用此标头,某些系统可能丢弃
回显请求。
-S srcaddr
要使用的源地址。
-c compartment
路由隔离舱标识符。
-p
Ping Hyper-V 网络虚拟化提供程序地址。
-4
强制使用 IPv4。
-6
强制使用 IPv6。
了解完ping命令的选项以及用法之后我们开始接下来的代码模块
首先应该在.Pro文件中添加 network模块
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp \
ping.cpp
HEADERS += \
mainwindow.h \
ping.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
unix|win32: LIBS += -lWS2_32
先看配置文件 lanIP.ini
接下来添加ping.h文件
#ifndef PING_H
#define PING_H
#pragma once
#include <QObject>
#include <QNetworkInterface>
#include <QDebug>
#include <QTimer>
#include <QThread>
#include <QDateTime>
#include <QProcess>
#include <QHostInfo>
#include <QSettings>
#include <QTextCodec>
#include<QVector>
#include <unistd.h>
#pragma execution_character_set("utf-8")
#pragma warning( disable : 4819 )
class Ping : public QObject
{
Q_OBJECT
public:
Ping(QObject *parent = nullptr);
virtual ~Ping();
QString m_msg = "";
private:
QProcess* cmd;
QTimer *timer;
QVector<QString> v_lanIP;
private:
void sendMsgBox(QString msg);
signals :
void signal_msgboxWarning(QString msg);
public slots :
void slot_pingIP();
};
#endif // PING_H
ping.cpp
#include "ping.h"
Ping::Ping(QObject *parent)
{
}
Ping::~Ping()
{
if (timer->isActive()) {
timer->stop();
delete timer;
}
cmd->close();
cmd->kill();
delete cmd;
}
void Ping::slot_pingIP()
{
//读取配置文件
QSettings *configIniRead=new QSettings("lanIP.ini",QSettings::IniFormat);
configIniRead->setIniCodec(QTextCodec::codecForName("GB2312"));//可读写ini文件中的中文
//根据自己配置文件来读取ip并添加到v_lanIP中
v_lanIP <<configIniRead->value("/lanIP/1").toString()
<< configIniRead->value("/lanIP/2").toString()
<< configIniRead->value("/lanIP/3").toString()
<< configIniRead->value("/lanIP/4").toString()
<< configIniRead->value("/lanIP/5").toString();
delete configIniRead;
#if 0
//第一种方式
QVector<QString>::iterator num;
for (num=v_lanIP.begin(); num!=v_lanIP.end(); num++)
{
qDebug()<< *num;
}
#else
//第二种方式
for (int i=0; i<v_lanIP.count(); i++)
{
qDebug()<< v_lanIP[i];
}
#endif
cmd = new QProcess();
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, [=]()
{
for (int i=0; i<v_lanIP.count(); i++)
{
qDebug()<< v_lanIP[i];
QString ping = "ping ";
ping +=v_lanIP[i];
ping+=" -n 1";
cmd->start(ping);
// 等待ping 的结果
while (cmd->waitForFinished())
{
QString result = QString::fromLocal8Bit(cmd->readAll());
//qDebug() << result;
if (result.indexOf("TTL") == -1)
sendMsgBox(v_lanIP[i]+"网线被拔掉或其他原因,连接已断开");
else
sendMsgBox(v_lanIP[i]+"连接成功!");
}
}
});
timer->start(1000);
}
//弹窗信号
void Ping::sendMsgBox(QString msg)
{
emit signal_msgboxWarning(msg);
}
mainwindow.h
#pragma once
#include <QMainWindow>
#include "ui_mainwindow.h"
#include "ping.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = Q_NULLPTR);
~MainWindow();
Ping* m_ping;
QThread* m_pingThread;
signals:
void signal_pingIP();
private slots :
void slot_msgBoxWarning(QString msgText);
private:
Ui::MainWindow ui;
};
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
m_ping = new Ping();
m_pingThread = new QThread();
m_ping->moveToThread(m_pingThread);
m_pingThread->start();
connect(this, &MainWindow::signal_pingIP, m_ping, &Ping::slot_pingIP);
emit signal_pingIP();
connect(m_ping, &Ping::signal_msgboxWarning, this, &MainWindow::slot_msgBoxWarning);
}
MainWindow::~MainWindow()
{
m_pingThread->quit();
m_pingThread->wait();
delete m_ping;
}
void MainWindow::slot_msgBoxWarning(QString msgText)
{
qDebug() << "连接状态:" << msgText;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}