diff --git a/pipe_test_with_routing.cpp b/pipe_test_with_routing.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d4fc065534a8e6d6800c77d30634ce81509877e0 --- /dev/null +++ b/pipe_test_with_routing.cpp @@ -0,0 +1,34 @@ +#include <QCoreApplication> +#include <QTextStream> +#include <QRegExp> +#include <cstdio> + +#define cout QTextStream(stdout) + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + FILE *cmdHandle; +#if defined( Q_OS_WIN ) + // original command: route print -4 | findstr /r "[^0-9]0\.0\.0\.0" + cmdHandle = popen( + "route print -4 | findstr /r \"[^0-9]0\\.0\\.0\\.0\" ", "r" + ); +#elif defined ( Q_OS_LINUX ) +#else +#error "We do not support this version." +#endif + constexpr int BUFFSIZE = 256; + char buffer[BUFFSIZE]; + fgets(buffer, BUFFSIZE-1, cmdHandle); + + QRegExp regx("^(?:\\s*(?:0\\.0\\.0\\.0)){2}\\s*([^\\s]+)"); + regx.indexIn(buffer); + QString ipAddr = regx.capturedTexts()[1]; + + cout << ipAddr; + + fclose(cmdHandle); + return a.exec(); +}