用C/C++代码检测ip能否ping通(配合awk和system可以做到批量检测)
来源:互联网 发布:java springmvc框架 编辑:程序博客网 时间:2024/04/29 15:52
遇到一个小需求, 快速搞定。 来看看用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) // 这个是获取命令执行的结果, 类似于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 noubuntu@VM-0-13-ubuntu:~$ ./a.out 1.1.1.11.1.1.1ubuntu@VM-0-13-ubuntu:~$ ./a.out 172.16.0.13ipok:172.16.0.13ubuntu@VM-0-13-ubuntu:~$ ./a.out www.baidu.comipok:www.baidu.comubuntu@VM-0-13-ubuntu:~$
如上ping测试的超时时间是1s, 自己可以改。 另外, 如果有a.txt文件, 每行一个ip, 怎么知道哪些ip能否ping通呢? awk和system搞起吧, 我们已经说过了:
ubuntu@VM-0-13-ubuntu:~$ cat a.txt 1.1.1.1www.baidu.comwww.qq.comubuntu@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.txt1.1.1.1ipok:www.baidu.comipok:www.qq.comubuntu@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$ lsa.txt test.cppubuntu@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.12.2.2.2www.baidu.com3.3.3.34.4.4.4ubuntu@VM-0-13-ubuntu:~/tmp_test$ ubuntu@VM-0-13-ubuntu:~/tmp_test$ ubuntu@VM-0-13-ubuntu:~/tmp_test$ g++ test.cppubuntu@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.txtecho 1.1.1.1 >> b.txtecho 2.2.2.2 >> b.txtecho ipok:www.baidu.com >> b.txtecho 3.3.3.3 >> b.txtecho 4.4.4.4 >> b.txtubuntu@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.12.2.2.2ipok:www.baidu.com3.3.3.34.4.4.4ubuntu@VM-0-13-ubuntu:~/tmp_test$
阅读全文
0 0
- 用C/C++代码检测ip能否ping通(配合awk和system可以做到批量检测)
- centos下批量ping检测IP是不是通的
- linux 批量ping检测
- Linux下用c实现通过ping检测连接
- PING,ip ping .c
- C语言实现IP地址合法性检测和子网匹配
- C代码检测内存泄漏
- Splint C语言代码检测
- Objective-C 动态类型检测 类和实力对象能否响应方法
- MFC检测网络连接和ping IP地址
- MFC检测网络连接和ping IP地址
- 如何检测ping通
- VS2005中检测和更正 C/C++ 代码缺陷
- VS2005中检测和更正 C/C++ 代码缺陷
- C#-如何ping IP
- (转)SHELL脚本:检测一坨IP是否都能ping通(网卡监控)
- SHELL脚本:检测一坨IP是否都能ping通(网卡监控)
- ping检测
- 基于stm32串口环形缓冲队列处理机制—入门级(单字节)
- Android Parcelable问题
- zlib数据格式及解压缩实现
- struts2+hibernate+ehcache二级缓存(注解方式)
- getFilesDir()与getExternalFilesDir()的区别
- 用C/C++代码检测ip能否ping通(配合awk和system可以做到批量检测)
- 分布式消息队列
- python自动测试
- 【Scikit-Learn 中文文档】模型选择:选择估计量及其参数
- 深入理解Java垃圾回收机制
- 让机器帮你写博客-机器学习实战
- 欢迎使用CSDN-markdown编辑器
- 【Scikit-Learn 中文文档】无监督学习: 寻求数据表示
- 各种主流 SQLServer 迁移到 MySQL 工具对比