服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 用C/C++代码检测ip能否ping通(配合awk和system可以做到批量检测)

用C/C++代码检测ip能否ping通(配合awk和system可以做到批量检测)

2021-07-27 12:14stpeace C/C++

今天小编就为大家分享一篇关于用C/C++代码检测ip能否ping通(配合awk和system可以做到批量检测),小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

遇到一个小需求, 快速搞定。 来看看用C/C++代码检测ip能否ping通:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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;
}

测试一下:

?
1
2
3
4
5
6
7
8
9
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 <a href="http://www.baidu.comipok:www.baidu.comubuntu@VM-0-13-ubuntu:~$" rel="external nofollow">www.baidu.com
ipok:www.baidu.com
ubuntu@VM-0-13-ubuntu:~$</a>

如上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后, 就立即能看到结果的。怎么办呢?可以写程序逐行读取文件来搞起, 看下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#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$

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接

延伸 · 阅读

精彩推荐
  • C/C++C语言实现电脑关机程序

    C语言实现电脑关机程序

    这篇文章主要为大家详细介绍了C语言实现电脑关机程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    xiaocaidayong8482021-08-20
  • C/C++C/C++经典实例之模拟计算器示例代码

    C/C++经典实例之模拟计算器示例代码

    最近在看到的一个需求,本以为比较简单,但花了不少时间,所以下面这篇文章主要给大家介绍了关于C/C++经典实例之模拟计算器的相关资料,文中通过示...

    jia150610152021-06-07
  • C/C++学习C++编程的必备软件

    学习C++编程的必备软件

    本文给大家分享的是作者在学习使用C++进行编程的时候所用到的一些常用的软件,这里推荐给大家...

    谢恩铭10102021-05-08
  • C/C++深入理解goto语句的替代实现方式分析

    深入理解goto语句的替代实现方式分析

    本篇文章是对goto语句的替代实现方式进行了详细的分析介绍,需要的朋友参考下...

    C语言教程网7342020-12-03
  • C/C++c++ 单线程实现同时监听多个端口

    c++ 单线程实现同时监听多个端口

    这篇文章主要介绍了c++ 单线程实现同时监听多个端口的方法,帮助大家更好的理解和学习使用c++,感兴趣的朋友可以了解下...

    源之缘11542021-10-27
  • C/C++C++之重载 重定义与重写用法详解

    C++之重载 重定义与重写用法详解

    这篇文章主要介绍了C++之重载 重定义与重写用法详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...

    青山的青6062022-01-04
  • C/C++C语言中炫酷的文件操作实例详解

    C语言中炫酷的文件操作实例详解

    内存中的数据都是暂时的,当程序结束时,它们都将丢失,为了永久性的保存大量的数据,C语言提供了对文件的操作,这篇文章主要给大家介绍了关于C语言中文件...

    针眼_6702022-01-24
  • C/C++详解c语言中的 strcpy和strncpy字符串函数使用

    详解c语言中的 strcpy和strncpy字符串函数使用

    strcpy 和strcnpy函数是字符串复制函数。接下来通过本文给大家介绍c语言中的strcpy和strncpy字符串函数使用,感兴趣的朋友跟随小编要求看看吧...

    spring-go5642021-07-02