用C/C++代碼檢測ip能否ping通(配合awk和system可以做到批量檢測)
遇到一個小需求, 快速搞定。 來看看用C/C++代碼檢測ip能否ping通:
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> using namespace std; string getCmdResult(const string &strCmd) // 這個是獲取命令執(zhí)行的結果, 類似于system, 之前我已經說過了 { char buf[10240] = {0}; FILE *pf = NULL; if( (pf = popen(strCmd.c_str(), "r")) == NULL ) { return ""; } string strResult; while(fgets(buf, sizeof buf, pf)) { strResult += buf; } pclose(pf); unsigned int iSize = strResult.size(); if(iSize > 0 && strResult[iSize - 1] == '\n') // linux { strResult = strResult.substr(0, iSize - 1); } return strResult; } int main(int argc, char *argv[]) { if(argc != 2) { cout << "no" << endl; return -1; } string strCmd = "ping " + string(argv[1]) + " -w 1"; string strRe = getCmdResult(strCmd); if(strRe.find("received") != string::npos && strRe.find(", 0 received") == string::npos) { cout << "ipok:" + string(argv[1]) << endl; } else { cout << argv[1] << endl; } return 0; }
測試一下:
ubuntu@VM-0-13-ubuntu:~$ ./a.out no ubuntu@VM-0-13-ubuntu:~$ ./a.out 1.1.1.1 1.1.1.1 ubuntu@VM-0-13-ubuntu:~$ ./a.out 172.16.0.13 ipok:172.16.0.13 ubuntu@VM-0-13-ubuntu:~$ ./a.out www.baidu.com ipok:www.baidu.com ubuntu@VM-0-13-ubuntu:~$
如上ping測試的超時時間是1s, 自己可以改。 另外, 如果有a.txt文件, 每行一個ip, 怎么知道哪些ip能否ping通呢? awk和system搞起吧, 我們已經說過了:
ubuntu@VM-0-13-ubuntu:~$ cat a.txt
1.1.1.1
www.baidu.com
www.qq.com
ubuntu@VM-0-13-ubuntu:~$
ubuntu@VM-0-13-ubuntu:~$
ubuntu@VM-0-13-ubuntu:~$
ubuntu@VM-0-13-ubuntu:~$ awk '{cmd="./a.out " $1; system(cmd)}' a.txt
1.1.1.1
ipok:www.baidu.com
ipok:www.qq.com
ubuntu@VM-0-13-ubuntu:~$
可見 1.1.1.1 ping不通, 其余的可以ping通。
上面用awk和system有個問題:如果ip過多, 則必須等到所有ip檢測完畢后, 才知道最后的結果。 也就是說, 并不是處理完一個ip后, 就立即能看到結果的。怎么辦呢?可以寫程序逐行讀取文件來搞起, 看下:
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fstream> #include <string> using namespace std; string getCmdResult(const string &strCmd) { char buf[10240] = {0}; FILE *pf = NULL; if( (pf = popen(strCmd.c_str(), "r")) == NULL ) { return ""; } string strResult; while(fgets(buf, sizeof buf, pf)) { strResult += buf; } pclose(pf); unsigned int iSize = strResult.size(); if(iSize > 0 && strResult[iSize - 1] == '\n') // linux { strResult = strResult.substr(0, iSize - 1); } return strResult; } string ipCheck(const string &ip) { string strCmd = "ping " + ip + " -w 1"; string strRe = getCmdResult(strCmd); if((strRe.find("received") != string::npos && strRe.find(", 0 received") == string::npos)) { return "ipok:" + string(ip); } else { return ip; } } int main(int argc, char *argv[]) // ./a.out a.txt b.txt { if(argc != 3) { cout << "error" << endl; return -1; } string strCmd = "rm -rf " + string(argv[2]); system(strCmd.c_str()); strCmd = "wc -l " + string(argv[1]) + "| awk '{print $1}'"; // 獲取文件行數(shù) string strNumLine = getCmdResult(strCmd); ifstream in(argv[1]); string filename; string line; unsigned int i = 0; if(in) // 有該文件 { while (getline (in, line)) // line中不包括每行的換行符 { // 這里最好做ip格式判斷 string strResult = ipCheck(line); strCmd = "echo " + strResult + " >> " + string(argv[2]) ; cout << strCmd << endl; system(strCmd.c_str()); } } else // 沒有該文件 { cout <<"no such file" << endl; } return 0; }
看下結果:
ubuntu@VM-0-13-ubuntu:~/tmp_test$ ls
a.txt test.cpp
ubuntu@VM-0-13-ubuntu:~/tmp_test$
ubuntu@VM-0-13-ubuntu:~/tmp_test$
ubuntu@VM-0-13-ubuntu:~/tmp_test$ cat a.txt
1.1.1.1
2.2.2.2
www.baidu.com
3.3.3.3
4.4.4.4
ubuntu@VM-0-13-ubuntu:~/tmp_test$
ubuntu@VM-0-13-ubuntu:~/tmp_test$
ubuntu@VM-0-13-ubuntu:~/tmp_test$ g++ test.cpp
ubuntu@VM-0-13-ubuntu:~/tmp_test$
ubuntu@VM-0-13-ubuntu:~/tmp_test$
ubuntu@VM-0-13-ubuntu:~/tmp_test$ ./a.out a.txt b.txt
echo 1.1.1.1 >> b.txt
echo 2.2.2.2 >> b.txt
echo ipok:www.baidu.com >> b.txt
echo 3.3.3.3 >> b.txt
echo 4.4.4.4 >> b.txt
ubuntu@VM-0-13-ubuntu:~/tmp_test$
ubuntu@VM-0-13-ubuntu:~/tmp_test$
ubuntu@VM-0-13-ubuntu:~/tmp_test$
ubuntu@VM-0-13-ubuntu:~/tmp_test$
ubuntu@VM-0-13-ubuntu:~/tmp_test$
ubuntu@VM-0-13-ubuntu:~/tmp_test$ cat b.txt
1.1.1.1
2.2.2.2
ipok:www.baidu.com
3.3.3.3
4.4.4.4
ubuntu@VM-0-13-ubuntu:~/tmp_test$
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對我們的支持。如果你想了解更多相關內容請查看下面相關鏈接
上一篇:c++編寫String類代碼實例
欄 目:C語言
下一篇:C++begin和end運算符的返回迭代器的類型如何判斷?
本文標題:用C/C++代碼檢測ip能否ping通(配合awk和system可以做到批量檢測)
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/352.html
您可能感興趣的文章
- 04-02c語言函數(shù)調用后清空內存 c語言調用函數(shù)刪除字符
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求階乘
- 01-10數(shù)據結構課程設計-用棧實現(xiàn)表達式求值的方法詳解
- 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
- 01-10HDOJ 1443 約瑟夫環(huán)的最新應用分析詳解
- 01-10深入理解C++中常見的關鍵字含義


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
隨機閱讀
- 01-10C#中split用法實例總結
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10delphi制作wav文件的方法