用C/C++代码检测ip能否ping通(配合awk和system可以做到批量检测)
2017-12-15 12:36
621 查看
遇到一个小需求, 快速搞定。 来看看用C/C++代码检测ip能否ping通:
测试一下:
如上ping测试的超时时间是1s, 自己可以改。 另外, 如果有a.txt文件, 每行一个ip, 怎么知道哪些ip能否ping通呢? awk和system搞起吧, 我们已经说过了:
可见 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}'"; // 获取文件行数
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$
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> using namespace std; string getCmdResult(const string &strCmd) // 这个是获取命令执行的结果, 类似于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}'"; // 获取文件行数
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$
相关文章推荐
- Cppcheck--C/C++代码静态检测工具
- 基于C++和OpenCv的SIFT_图像局部特征检测算法代码的实现
- MATLAB Coder可以从MATLAB代码生成独立的、可读性强、可移植的C/C++代码
- 如何用C++代码检测你用的编程环境是64系统还是32位系统?
- awk中可以使用system来执行复杂的shell命令
- 检测到通信错误。正在使用的通信协议:"TCP/IP"。正在使用的通信API:"SOCKETS"。检测到错误的位置:""。检测到错误的通信函数:"gethostbyname"。协议特定的错误代码:"*"、"11004"、"*"。 SQLST
- Opencv检测交通中红色标识轮廓c++代码实例及运行结果
- 交通标识检测c++代码实例及运行结果
- 一段检测IP设备是否在线的代码
- PHP制作登录异常ip检测功能的实例代码
- 写一个函数,可以处理C++代码,将其中的注释去掉
- awk中可以使用system来执行复杂的shell命令
- 分享一个可以生成各种进制格式IP的小工具实例代码
- UML工具推荐 Poseidon版本 可以免费下载,自动生成代码 C++ Java C# VB.NET等 (来自德国汉堡的国际性建模软件开发商Gentleware AG的)
- Opencv检测交通中红色标识轮廓c++代码实例及运行结果
- C++代码检测工具
- C++ 代码分块也可以的
- 对于一个糟糕的设计来说,通过阅读C++代码你可以很容易地识别出它。
- 使用强大的工具来解决前后台的协调配合,做到互不干扰对方的开发工作,只需要前后台都完工的情况合并代码就好了
- ios 检测网络 无须代码就可以设置