zoukankan      html  css  js  c++  java
  • 对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
    "
            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)...
    ";
    
        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<<"
    ";
        }
        std::cout<<"--------the end of the Display2DVector()--------
    ";
    }
    
    template <class DataType>
    void DisplayMapData(std::map<std::string, std::vector<DataType> > &mymap) {
        std::cout<<"the number of map_data: "<<mymap.size()<<"
    ";
        std::cout<<"the format of map_data is (key, value), in which value is a vector of optional info
    ";
    
        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<<"
    ";
        }
        std::cout<<"--------the end of the DisplayMapData()--------
    ";
    }
    
    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>)-------
    ";
        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<<"
    ";
                v_ip.push_back(it->first);
                std::fstream tempfs("/root/ipitem", std::ios::out); /*open for write*/
                tempfs<<it->first<<"
    ";
                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<<"
    ";
                }
                tempfs.close();
                std::string cmdstr="/bin/bash /root/expectexcute.sh";
                int ret=shell_call(cmdstr);
                std::cout<<"<<<<<<<<<<<<<<<<<<<<<<<<<<ret>>>>>>>>>>>>>>>>>>"<<ret<<"
    ";
                if(ret == 0) {
                    std::cout<<"ssh without password
    ";
                    fsc<<it->first<<"
    "; /*save the ip that ssh without password*/
                }
                else if(ret == 256) {
                    std::cout<<"ssh needs password
    ";
                    fsd<<it->first<<"
    ";
                }
                else {
                    std::cout<<ret<<"windows ip
    ";
                    fse<<it->first<<"
    ";
                }
            }
            else { /*ping is failure*/
                fsa<<it->first<<"
    ";
            }
        }
    }
    
    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
    ");
            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
    ");
                exit(-1);
            }
        }
        if((ret = pclose(fpin)) == -1) {
            printf("pclose error
    ");
            exit(-1);
        }
        putchar('
    ');
    
        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: 
    ";
            std::cout<<"select 1: check the ratio of cpu.
    ";
            std::cout<<"select 2: check the ratio of fs.
    ";
            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.
    ";
            }
        }
    
        return 0;
    }
    
    int main() {
        std::cout<<"The raw data's path is: /root/vm.data(virtual machines data)
    ";
        std::cout<<"The format of vm.data is in row as below: 
    ";
        std::cout<<"field0(ipaddress) field1(e.g. applicant) field2(e.g. department) ...
    ";
    
        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
    ";
            std::cout<<"e.g. 192.168.127.[1,255), you can input 127 only.
    ";
            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()--------
    ";
    
        return 0;
    }
  • 相关阅读:
    C# 斐波拉契数列
    Visual Studio [即时窗口] & [命令窗口] (Immediate Window & Command Window) 转
    在.NET平台下 有哪些数据持久层框架 (转)
    WebPart 控件之间通讯 笔记
    WebPart的数据库连接问题 转
    C 语言函数要先声明后定义
    C#单例模式的三种写法(转)
    WCF 绑定wshttpbinding
    关于C#中派生类调用基类构造函数的理解 base使用
    ThinkPHP3.* 模型操作相关函数
  • 原文地址:https://www.cnblogs.com/donggongdechen/p/9487818.html
Copyright © 2011-2022 走看看