用C/C++代碼檢測(cè)ip能否ping通(配合awk和system可以做到批量檢測(cè))
遇到一個(gè)小需求, 快速搞定。 來(lái)看看用C/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) // 這個(gè)是獲取命令執(zhí)行的結(jié)果, 類(lèi)似于system, 之前我已經(jīng)說(shuō)過(guò)了
{
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;
}
測(cè)試一下:
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測(cè)試的超時(shí)時(shí)間是1s, 自己可以改。 另外, 如果有a.txt文件, 每行一個(gè)ip, 怎么知道哪些ip能否ping通呢? awk和system搞起吧, 我們已經(jīng)說(shuō)過(guò)了:
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:~$
可見(jiàn) 1.1.1.1 ping不通, 其余的可以ping通。
上面用awk和system有個(gè)問(wèn)題:如果ip過(guò)多, 則必須等到所有ip檢測(cè)完畢后, 才知道最后的結(jié)果。 也就是說(shuō), 并不是處理完一個(gè)ip后, 就立即能看到結(jié)果的。怎么辦呢?可以寫(xiě)程序逐行讀取文件來(lái)搞起, 看下:
#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 // 沒(méi)有該文件
{
cout <<"no such file" << endl;
}
return 0;
}
看下結(jié)果:
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$
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
C++深入分析數(shù)據(jù)在內(nèi)存中的存儲(chǔ)形態(tài)
使用編程語(yǔ)言進(jìn)行編程時(shí),需要用到各種變量來(lái)存儲(chǔ)各種信息。變量保留的是它所存儲(chǔ)的值的內(nèi)存位置。這意味著,當(dāng)您創(chuàng)建一個(gè)變量時(shí),就會(huì)在內(nèi)存中保留一些空間。您可能需要存儲(chǔ)各種數(shù)據(jù)類(lèi)型的信息,操作系統(tǒng)會(huì)根據(jù)變量的數(shù)據(jù)類(lèi)型,來(lái)分配內(nèi)存和決定在保留內(nèi)存中存儲(chǔ)什么2023-01-01
C語(yǔ)言入門(mén)篇--四大常量(字面,const修飾,宏,枚舉)及標(biāo)識(shí)符
本篇文章是c語(yǔ)言基礎(chǔ)篇,主要講述一下常量,常量即不可被直接修改的量(const修飾的常變量可間接修改,后續(xù)文章會(huì)繼續(xù)說(shuō)明)請(qǐng)大家持續(xù)關(guān)注腳本之家2021-08-08
C語(yǔ)言實(shí)現(xiàn)刪除某一個(gè)數(shù)組值的方法
這篇文章主要給大家分享C語(yǔ)言數(shù)組中刪除數(shù)組中某個(gè)值的方法,既然要學(xué)習(xí)刪除數(shù)組中的元素,我們就必須得先知道數(shù)組中有哪些元素。同時(shí)還要定義一個(gè)變量,并將需要?jiǎng)h除的元素賦值給那個(gè)變量。下面來(lái)看看文章的詳細(xì)內(nèi)容吧2021-11-11
C語(yǔ)言中查詢(xún)進(jìn)程信號(hào)是否被遮罩或擱置的簡(jiǎn)單方法
這篇文章主要介紹了C語(yǔ)言中查詢(xún)進(jìn)程信號(hào)是否被遮罩或擱置的簡(jiǎn)單方法,包括sigprocmask函數(shù)和sigpending函數(shù)的簡(jiǎn)介,需要的朋友可以參考下2015-09-09
C++標(biāo)準(zhǔn)模板庫(kù)STL的介紹
今天小編就為大家分享一篇關(guān)于C++標(biāo)準(zhǔn)模板庫(kù)STL的介紹,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Matlab實(shí)現(xiàn)繪制玫瑰線(xiàn)的示例代碼
這篇文章主要為大家介紹了如何利用Matlab繪制3好看的玫瑰線(xiàn),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Matlab有一定幫助,感興趣的小伙伴可以動(dòng)手試一試2022-08-08
解析為何要關(guān)閉數(shù)據(jù)庫(kù)連接,可不可以不關(guān)閉的問(wèn)題詳解
本篇文章是對(duì)為何要關(guān)閉數(shù)據(jù)庫(kù)連接,可不可以不關(guān)閉的問(wèn)題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

