欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用C/C++代碼檢測ip能否ping通(配合awk和system可以做到批量檢測)

 更新時間:2019年04月08日 09:51:17   作者:stpeace  
今天小編就為大家分享一篇關(guān)于用C/C++代碼檢測ip能否ping通(配合awk和system可以做到批量檢測),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

遇到一個小需求, 快速搞定。 來看看用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) // 這個是獲取命令執(zhí)行的結(jié)果, 類似于system, 之前我已經(jīng)說過了
{ 
  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搞起吧, 我們已經(jīng)說過了:

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檢測完畢后, 才知道最后的結(jié)果。 也就是說, 并不是處理完一個ip后, 就立即能看到結(jié)果的。怎么辦呢?可以寫程序逐行讀取文件來搞起, 看下:

#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}'"; // 獲取文件行數(shù)
 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;
}

看下結(jié)果:

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$

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

  • C++虛函數(shù)的實現(xiàn)機制分析

    C++虛函數(shù)的實現(xiàn)機制分析

    這篇文章主要介紹了C++虛函數(shù)的實現(xiàn)機制分析,需要的朋友可以參考下
    2014-07-07
  • C語言算法練習(xí)之數(shù)組元素排序

    C語言算法練習(xí)之數(shù)組元素排序

    這篇文章主要為大家介紹了C語言算法練習(xí)中數(shù)組元素排序的實現(xiàn)方法,文中的示例代碼講解詳細,對我們學(xué)習(xí)C語言有一定幫助,需要的可以參考一下
    2022-09-09
  • win32 api實現(xiàn)簡單的消息窗口示例

    win32 api實現(xiàn)簡單的消息窗口示例

    這篇文章主要介紹了使用win32 api實現(xiàn)簡單的消息窗口示例,需要的朋友可以參考下
    2014-03-03
  • C語言實現(xiàn)分治法實例

    C語言實現(xiàn)分治法實例

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)分治法的實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 詳解C++中的const和constexpr

    詳解C++中的const和constexpr

    這篇文章主要為大家介紹了C++中的const和constexpr ,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • C++實現(xiàn)關(guān)系與關(guān)系矩陣的代碼詳解

    C++實現(xiàn)關(guān)系與關(guān)系矩陣的代碼詳解

    這篇文章主要介紹了C++實現(xiàn)關(guān)系與關(guān)系矩陣,功能實現(xiàn)包括關(guān)系的矩陣表示,關(guān)系的性質(zhì)判斷及關(guān)系的合成,本文結(jié)合示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C語言求解定積分的方法

    C語言求解定積分的方法

    這篇文章主要為大家詳細介紹了C語言求解定積分的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • C語言 實現(xiàn)遍歷一個文件夾的所有文件

    C語言 實現(xiàn)遍歷一個文件夾的所有文件

    這篇文章主要介紹了C語言 實現(xiàn)遍歷一個文件夾的所有文件的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • 淺析C++中類模板的用法

    淺析C++中類模板的用法

    C++類模板是一種用于創(chuàng)建通用類的工具,它允許我們定義一個通用類,支持多種類型。本文就來簡單講講它的具體使用吧,感興趣的可以了解一下
    2023-04-04
  • C++虛函數(shù)注意事項

    C++虛函數(shù)注意事項

    這篇文章主要給大家分享了EC++虛函數(shù)注意事項,
    2022-01-01

最新評論