Using C and C++ code to detect whether ip ping can cooperate with awk and system to achieve batch detection

  • 2020-06-15 09:57:28
  • OfStack

Meet a small need, quickly solve. Let's see if C/C++ code can be used to detect ip:


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
using namespace std;
string getCmdResult(const string &strCmd) //  This is the result of the command execution,   Similar to the system,  I've said that before 
{ 
  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;
}

Test 1:


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:~$ 

The timeout for the ping test above is 1s, which you can change yourself. In addition, if you have a.txt files with one ip per line, how do you know which ip will work with ping? Let's do awk and system, we've already said:

[

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:~$

]

It can be seen that 1.1.1.1 ping cannot pass, and the rest can pass ping.

There is a problem with using awk and system above: if there are too many ip, you won't know the final result until all ip tests have been completed. That is, it is not possible to process one ip and see the results immediately. What to do? You can write a program to read the file line by line to make it work. Here it is:


#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}'"; //  Gets the number of file lines 
 string strNumLine = getCmdResult(strCmd);
 ifstream in(argv[1]);
 string filename;
 string line;
 unsigned int i = 0;
 if(in) //  Have the file 
 {
 while (getline (in, line)) // line Line breaks for each line are not included in 
 {
  //  The best thing to do here ip Format to judge 
  string strResult = ipCheck(line);
  strCmd = "echo " + strResult + " >> " + string(argv[2]) ;
  cout << strCmd << endl;
  system(strCmd.c_str());
 }
 }
 else //  There is no such file 
 {
 cout <<"no such file" << endl;
 }
 return 0;
}

Take a look at the results:

[

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$

]

conclusion


Related articles: