对ip数据进行分类----c++

#!/usr/bin/expect

set ip        [lindex $argv 0]
set password  [lindex $argv 1]

spawn ssh -o ConnectTimeout=15 -l root ${ip} "hostname"
expect {
    "(yes/no)? " {
        send "yes\n"
        expect "*assword:*" {
            exit 1
        } 
    }
    "*assword: " {
        exit 1
    }
    exit 0
}

catch wait ret
set result [lindex $ret 3]
puts $result
if { $result == 255 || $result == 0 } {
    exit $result 
}

 

#!/bin/bash

password='password'
for ip in $(cat ipitem);
do {
    /usr/bin/expect expect.sh $ip $password
    ret=$?
    if [ $ret -eq 0 ]; then
        exit 0
    elif [ $ret -eq 255 ]; then
        exit 255
    else
        exit 1
    fi
}
done
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <sys/stat.h>
#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()<<std::endl;
    std::cout<<"field0(ipaddress necessary) field1(optional) field2(optional)...\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";
    std::cout<<"the format of map_data is (key, value), in which value is a vector of optional info\n";

    for(typename::std::map<std::string, std::vector<DataType> >::const_iterator it=mymap.begin(); it!=mymap.end(); ++it) {
        std::cout<<it->first<<", ";
        typename::std::vector<DataType>::const_iterator sit;
        for(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::ofstream fsd("/root/sshfailip", std::ios::trunc);
    std::ofstream fse("/root/winip", 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);
            std::fstream tempfs("/root/ipitem", std::ios::out); /*open for write*/
            tempfs<<it->first<<"\n";
            tempfs.close();
            tempfs.open("/root/ipitem", std::ios::in); /*open for read*/
            while(!tempfs.eof()) {
                std::string line;
                getline(tempfs,line);
                if(line.empty())
                    break;
                std::cout<<line<<"\n";
            }
            tempfs.close();
            std::string cmdstr="/bin/bash /root/expectexcute.sh";
            int ret=shell_call(cmdstr);
            std::cout<<"<<<<<<<<<<<<<<<<<<<<<<<<<<ret>>>>>>>>>>>>>>>>>>"<<ret<<"\n";
            if(ret == 0) {
                std::cout<<"ssh without password\n";
                fsc<<it->first<<"\n"; /*save the ip that ssh without password*/
            }
            else if(ret == 256) {
                std::cout<<"ssh needs password\n";
                fsd<<it->first<<"\n";
            }
            else {
                std::cout<<ret<<"windows ip\n";
                fse<<it->first<<"\n";
            }
        }
        else { /*ping is failure*/
            fsa<<it->first<<"\n";
        }
    }
}

void format_the_data(std::string &filepath, const int &ip_num) {
    std::fstream fs(filepath.c_str(), std::ios::out);
    for(size_t i=1;i<255;++i) {
        fs<<"192.168."<<ip_num<<"."<<i<<std::endl;
    }
}

bool file_is_empty(std::string &filename) {
    struct stat buf;
    stat(filename.c_str(), &buf);
    size_t size=buf.st_size;
    if(size == 0)
        return true;
    else
        return false;
}

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::cout<<"The raw data's path is: /root/vm.data(virtual machines data)\n";
    std::cout<<"The format of vm.data is in row as below: \n";
    std::cout<<"field0(ipaddress) field1(e.g. applicant) field2(e.g. department) ...\n";

    std::map<std::string, std::vector<std::string> > my_map;
    std::vector<std::vector<std::string> > lines_feat;
    std::string filename="vm.data";
 
    /*the data file is empty or not*/
    if(file_is_empty(filename)) {
        std::cout<<"the raw data file /root/vm.data is empty\n";
        std::cout<<"e.g. 192.168.127.[1,255), you can input 127 only.\n";
        std::cout<<"please input the ip you want to process: ";
        size_t ip_num;
        std::cin>>ip_num;
        format_the_data(filename, ip_num);
    }

    /*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;
}
posted @ 2018-08-16 15:56  东宫得臣  阅读(215)  评论(0编辑  收藏  举报