How to send ifconfig from a Linux application?

如何从一个Linux应用程序发送ifconfig?

Tags: c++ linux qt
标签: c++ linux qt

问题 (Question)

I am writing a program on Linux. I would like to read ifconfig and screen on the tabwidged. I have no idea how I can do that.

I found this example. I would like to upgrade this.


I have many errors

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();

}

我写在Linux程序。我想读的tabwidged 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();

}

最佳答案 (Best Answer)

You are looking for QProcess and/or QtNetwork to get this information into your tab widget.

...
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);
...

That being said, you could look into QtNetwork to be more portable as "ifconfig" will not work on Windows, etc. You would need to change that to ipconfig and so on.

To be fair, even on Linux, you should look into the "ip" command instead of the old and hardly maintained "ifconfig".

Therefore, you would be looking for QNetworkInterface and QHostAddress with a proper cross-platform solution. Here is a short example:

foreach(const QNetworkInterface &interface, QNetworkInterface::allInterfaces())
    qDebug() << interface.hardwareAddress();

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

你要找的性的和/或支持要获得此信息到你的Tab控件。

...
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);
...

也就是说,你可以去支持更便捷的“ifconfig”将不工作在Windows,等你需要改变,ipconfig等。

是公平的,即使在Linux,你应该看看“IP”的命令,而不是旧的和难以保持“ifconfig”。

因此,你要寻找qnetworkinterfaceqhostaddress用一个适当的跨平台解决方案。这里是一个简短的例子:

foreach(const QNetworkInterface &interface, QNetworkInterface::allInterfaces())
    qDebug() << interface.hardwareAddress();

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

答案 (Answer) 2

If all you're looking for is the IP address, you might want to use gethostbyname() instead of calling an external program (like ifconfig on Linux, or ipconfig on Windows).

If you want additional information (besides IP addresses), you might also want to consider reading from /proc/net (Linux only):

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

如果你想要更多的信息(除了IP地址),您可能还需要考虑从/触发/网络阅读(Linux只):

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/23441236