#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'
)
{
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[])
{
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))
{
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;
}