如何从Linux应用程序发送ifconfig

How to send ifconfig from a Linux application?

本文关键字:ifconfig 应用程序 Linux      更新时间:2023-10-16

我在Linux上写一个程序。我想在选项卡上阅读ifconfig和屏幕。我不知道该怎么做。

我找到了这个例子。我想升级一下。


我有很多错误

main.cpp: In function ‘int main(int, char**)’:
main.cpp:7:19: error: variable ‘QApplication app’ has initializer but incomplete type
/usr/include/qt4/QtGui/qtabwidget.h:167:14: error: ‘QTabBar* QTabWidget::tabBar() const’ is protected
main.cpp:21:14: error: within this context
main.cpp:21:15: error: invalid use of incomplete type ‘struct QTabBar’
/usr/include/qt4/QtGui/qtabwidget.h:56:7: error: forward declaration of ‘struct QTabBar’
main.cpp:21:33: error: incomplete type ‘QTabBar’ used in nested name specifier
main.cpp:21:63: error: ‘button3’ was not declared in this scope
main.cpp:23:1: error: ‘myprocess’ was not declared in this scope
main.cpp:25:11: error: ‘ps’ was not declared in this scope
main.cpp:26:9: error: ‘myTabWidget’ was not declared in this scope
make: *** [main.o] Error 1
int main(int argc, char *argv[])
{
        QApplication app(argc, argv);
        QMainWindow *window = new QMainWindow();  
        window->setWindowTitle(QString::fromUtf8("MainWindow"));
        window->resize(480, 480);
        QWidget *centralWidget = new QWidget(window);
        QTabWidget *tabs = new QTabWidget(centralWidget);
        tabs->setFixedSize(440, 440);
        tabs->addTab(new QWidget(),"TAB 1");  
        tabs->addTab(new QWidget(),"TAB 2");
        tabs->addTab(new QWidget(),"TAB 3");
tabs->tabBar()->setTabButton(2, QTabBar::LeftSide,((QWidget*)(button3)));
QProcess myProcess;
myprocess.start("ifconfig");
if (myProcess.waitForStarted(-1)) {
    while(ps.waitForReadyRead(-1)) {
        myTabWidget.setText(1, myprocess.readAllStandardOutput());
    }
}
window->setCentralWidget(centralWidget);

        window->setCentralWidget(centralWidget);
        window->show();
        return app.exec();
}

您正在寻找QProcess和/或QtNetwork以将此信息获取到您的选项卡小部件中。

...
tabs->tabBar()->setTabButton(2, QTabBar::LeftSide,((QWidget*)(button3)));
QProcess myProcess;
myprocess.start("ifconfig");
if (myProcess.waitForStarted(-1)) {
    while(ps.waitForReadyRead(-1)) {
        myTabWidget.setText(your_index, myprocess.readAllStandardOutput());
    }
}
window->setCentralWidget(centralWidget);
...

话虽这么说,你可以看看QtNetwork更便携,因为"ifconfig"将无法在Windows上工作,等等。您需要将其更改为ipconfig,等等。

公平地说,即使在Linux上,您也应该查看"ip"命令,而不是旧的且难以维护的"ifconfig"命令。

因此,您将寻找具有适当跨平台解决方案的QNetworkInterface和QHostAddress。下面是一个简短的例子:

foreach(const QNetworkInterface &interface, QNetworkInterface::allInterfaces())
    qDebug() << interface.hardwareAddress();
foreach(const QHostAdress &address, QNetworkInterface::allAddresses())
    qDebug() << address.toString();

如果您正在寻找的是IP地址,您可能希望使用gethostbyname()而不是调用外部程序(如Linux上的ifconfig,或Windows上的ipconfig)。

如果您需要额外的信息(除了IP地址),您可能还需要考虑从/proc/net(仅限Linux)读取:

  • http://www.onlamp.com/pub/a/linux/2000/11/16/LinuxAdmin.html