用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) // 这个是获取命令执行的结果, 类似于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$

总结

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

(0)

相关推荐

  • C++与namespace有关的两个编译错误的讲解

    某次,在大型的工程代码中,我这样调用: #include <iostream> using namespace std; namespace A { void fun() { printf("aaa\n"); } } namespace B { void fun() { printf("bbb\n"); } } int main() { fun(); return 0; } 编译出错:error: 'fun' was not declared in th

  • c++编写String类代码实例

    本文实例为大家分享了c++编写String类的具体代码,供大家参考,具体内容如下 class String { public: String(const char* = nullptr); //普通构造函数 String(const String& other); //拷贝构造函数 ~String(void); //析构函数 String& operator = (const String& other); //赋值函数 private: char* m_data; }; //普通

  • C++基类指针和派生类指针之间的转换方法讲解

    函数重载.函数隐藏.函数覆盖 函数重载只会发生在同作用域中(或同一个类中),函数名称相同,但参数类型或参数个数不同. 函数重载不能通过函数的返回类型来区分,因为在函数返回之前我们并不知道函数的返回类型. 函数隐藏和函数覆盖只会发生在基类和派生类之间. 函数隐藏是指派生类中函数与基类中的函数同名,但是这个函数在基类中并没有被定义为虚函数,这种情况就是函数的隐藏. 所谓隐藏是指使用常规的调用方法,派生类对象访问这个函数时,会优先访问派生类中的这个函数,基类中的这个函数对派生类对象来说是隐藏起来的.

  • C++中rapidjson组装继续简化的方法

    rapidjson组装继续简化------人生苦短,我用rapidjson 看最简单的: #include <iostream> #include <stdio.h> #include<unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include<sstream> // 请自己下载开源的rapidjson #includ

  • C++直接cout指针名的含义?

    首先看下面这个代码实例: #include <iostream> using namespace std; int main() { char *str = "this is a test"; cout << "str=" << str << endl; cout << "*str=" << *str << endl; cout << "&a

  • C++中rapidjson组装map和数组array的代码示例

    rapidjson组装map和数组array的代码示例 直接上码: #include <iostream> #include <map> // 请自己下载开源的rapidjson #include "rapidjson/prettywriter.h" #include "rapidjson/rapidjson.h" #include "rapidjson/document.h" #include "rapidjs

  • strings命令分析浅谈Go和C++编译时的一点小区别

    最近查一个bug, 用strings命令分析, 竟然出乎意料地没有结果, 非常纳闷. 最后根据这个线索查出了bug的根本原因. 1.  在C++中, 即使函数在代码层面没有被调用, 也会最终编译到二进制中, 用strings可以分析.  #include <iostream> using namespace std; void fun() { printf("hello world\n"); // strings分析有结果 } int main() { return 0;

  • 详解C++中虚析构函数的作用及其原理分析

    C++中的虚析构函数到底什么时候有用的,什么作用呢. 一.虚析构函数的作用 总的来说虚析构函数是为了避免内存泄露,而且是当子类中会有指针成员变量时才会使用得到的.也就说虚析构函数使得在删除指向子类对象的基类指针时可以调用子类的析构函数达到释放子类中堆内存的目的,而防止内存泄露的. 我们知道,用C++开发的时候,用来做基类的类的析构函数一般都是虚函数.可是,为什么要这样做呢?下面用一个小例子来说明: #include<iostream> using namespace std; class Cl

  • C++类中变量也可以是引用的代码实例

    C++类中变量也可以是引用哈------要用初始化列表来初始化(因为C++引用一旦绑定,就无法更换,有点类似const) #include <iostream> using namespace std; class A { public: int &x; int &y; A(int &tmpX, int &tmpY):x(tmpX), y(tmpY){} }; int main() { int tmpX = 1; int tmpY = 2; A a(tmpX,

  • c++文件监控之FileSystemWatcher

    具体代码如下: #using <System.dll> #include <iostream> using namespace std; using namespace System; using namespace System::IO; using namespace System::Security::Permissions; public ref class Watcher { private: // Define the event handlers. static vo

随机推荐