标签:man cte process ati one fgets play eof cal
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <sstream>
extern int shell_call(std::string &);
template <class DataType>
void ReadDataFromFile(std::string &filename, std::vector<std::vector<DataType> > &lines_feat) {
std::ifstream vm_info(filename.c_str());
std::string lines, var;
std::vector<std::string> row;
lines_feat.clear();
while(!vm_info.eof()) {
getline(vm_info, lines);
if(lines.empty())
break;
std::stringstream stringin(lines);
row.clear();
while(stringin >> var) {
row.push_back(var);
}
lines_feat.push_back(row);
}
}
template <class DataType>
void Display2DVector(std::vector<std::vector<DataType> > &vv) {
std::cout<<"the total rows of 2d vector_data: "<<vv.size()<<"\n";
for(size_t i=0;i<vv.size();++i) {
for(typename::std::vector<DataType>::const_iterator it=vv[i].begin();it!=vv[i].end();++it) {
std::cout<<*it<<" ";
}
std::cout<<"\n";
}
std::cout<<"--------the end of the Display2DVector()--------\n";
}
template <class DataType>
void DisplayMapData(std::map<std::string, std::vector<DataType> > &mymap) {
std::cout<<"the number of map_data: "<<mymap.size()<<"\n";
for(typename::std::map<std::string, std::vector<DataType> >::const_iterator it=mymap.begin(); it!=mymap.end(); ++it) {
std::cout<<it->first<<" ";
for(typename::std::vector<DataType>::const_iterator sit=it->second.begin(); sit!=it->second.end(); ++sit) {
std::cout<<*sit<<" ";
}
std::cout<<"\n";
}
std::cout<<"--------the end of the DisplayMapData()--------\n";
}
template <class DataType>
void Vectors2Map(std::vector<std::vector<DataType> > &vv, std::map<std::string, std::vector<DataType> > &mymap) {
std::cout<<"-----convert the 2d vector(the original data) to map(key, value) (ip, <vector>)-------\n";
for(size_t i=0; i<vv.size(); ++i) {
size_t field=0;
std::vector<std::string> vm_s;
vm_s.clear();
for(typename::std::vector<DataType>::const_iterator it=vv[i].begin();it!=vv[i].end();++it) {
size_t len=vv[i].size();
if(len == 1) {
vm_s.push_back("value is none");
}
else if(len > 1 && field) {
vm_s.push_back(*it);
field++;
}
else {
field++;
}
}
mymap.insert(make_pair(vv[i][0], vm_s));
}
}
template<class DataType>
void ProcessMap(std::map<std::string, std::vector<DataType> > &mymap) {
std::ofstream fsa("/root/pingfail", std::ios::trunc);
std::ofstream fsb("/root/pingsucc", std::ios::trunc);
std::ofstream fsc("/root/sship", std::ios::trunc);
std::vector<std::string> v_ip;
v_ip.clear();
for(typename::std::map<std::string, std::vector<DataType> >::const_iterator it = mymap.begin(); it != mymap.end(); ++it) {
std::string pingcmd="ping -c 1 " + it->first;
if(!shell_call(pingcmd)) { /*ping is successful*/
fsb<<it->first<<"\n";
v_ip.push_back(it->first);
}
else { /*ping is failure*/
fsa<<it->first<<"\n";
}
}
std::string cmdstr="/bin/bash /root/expectexcute.sh";
for(typename::std::vector<std::string>::const_iterator it=v_ip.begin(); it!=v_ip.end(); ++it) {
std::fstream tempfs("/root/ipitem", std::ios::out);
tempfs<<*it<<"\n";
tempfs.close();
tempfs.open("/root/ipitem", std::ios::in);
while(!tempfs.eof()) {
std::string line;
getline(tempfs,line);
if(line.empty())
break;
std::cout<<line<<"\n";
}
tempfs.close();
if(!shell_call(cmdstr)) {
std::cout<<"ssh without password\n";
fsc<<*it<<"\n"; /*save the ip that ssh without password*/
}
else {
std::cout<<"ssh needs password\n";
}
}
}
int shell_call(std::string &cmdstr) {
enum { maxline=100 };
char line[maxline];
FILE *fpin;
int ret;
if((fpin = popen(cmdstr.c_str(), "r")) == NULL) {
printf("popen error\n");
exit(-1);
}
for(;;) {
fputs("prompt> ", stdout);
fflush(stdout);
if(fgets(line, maxline, fpin) == NULL) /*read from pipe*/
break;
if(fputs(line, stdout) == EOF) {
printf("fputs error to pipe\n");
exit(-1);
}
}
if((ret = pclose(fpin)) == -1) {
printf("pclose error\n");
exit(-1);
}
putchar(‘\n‘);
return ret; //return the status of cmdstr
}
int Select_menu() {
std::string reply;
int selector;
std::string cmdstr;
for(;;) {
std::cout<<"call the script or ‘q‘ to quit: \n";
std::cout<<"select 1: check the ratio of cpu.\n";
std::cout<<"select 2: check the ratio of fs.\n";
std::cout<<"Input your select: ";
std::cin>>reply;
if(reply.at(0) == ‘q‘)
break;
std::istringstream r(reply);
r>>selector;
switch(selector) {
case 1:
cmdstr = "/root/cpucheck.sh";
shell_call(cmdstr);
break;
case 2:
cmdstr = "/root/fscheck.sh";
shell_call(cmdstr);
break;
default:
std::cout<<"your select is out of range.\n";
}
}
return 0;
}
int main() {
std::map<std::string, std::vector<std::string> > my_map;
std::vector<std::vector<std::string> > lines_feat;
std::string filename="vm.data";
/*read data from file to 2d vector*/
ReadDataFromFile(filename, lines_feat);
/*display the raw data*/
Display2DVector(lines_feat);
/*convert the 2d vectors to map*/
Vectors2Map(lines_feat, my_map);
/*display the map*/
DisplayMapData(my_map);
/*process the map*/
ProcessMap(my_map);
/*process the ips, check the rate of fs and cpu*/
Select_menu();
std::cout<<"--------The end of main()--------\n";
return 0;
}
标签:man cte process ati one fgets play eof cal
原文地址:https://www.cnblogs.com/donggongdechen/p/9487818.html