对ip数据进行分类----c++
#!/usr/bin/expect set ip [lindex $argv ]
set password [lindex $argv ] spawn ssh -o ConnectTimeout= -l root ${ip} "hostname"
expect {
"(yes/no)? " {
send "yes\n"
expect "*assword:*" {
exit
}
}
"*assword: " {
exit
}
exit
} catch wait ret
set result [lindex $ret ]
puts $result
if { $result == || $result == } {
exit $result
}
#!/bin/bash password='password'
for ip in $(cat ipitem);
do {
/usr/bin/expect expect.sh $ip $password
ret=$?
if [ $ret -eq ]; then
exit
elif [ $ret -eq ]; then
exit
else
exit
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=;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=; i<vv.size(); ++i) {
size_t field=;
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 == ) {
vm_s.push_back("value is none");
}
else if(len > && field) {
vm_s.push_back(*it);
field++;
}
else {
field++;
}
}
mymap.insert(make_pair(vv[i][], 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 == ) {
std::cout<<"ssh without password\n";
fsc<<it->first<<"\n"; /*save the ip that ssh without password*/
}
else if(ret == ) {
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=;i<;++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 == )
return true;
else
return false;
} int shell_call(std::string &cmdstr) {
enum { maxline= };
char line[maxline];
FILE *fpin;
int ret; if((fpin = popen(cmdstr.c_str(), "r")) == NULL) {
printf("popen error\n");
exit(-);
} 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(-);
}
}
if((ret = pclose(fpin)) == -) {
printf("pclose error\n");
exit(-);
}
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() == 'q')
break;
std::istringstream r(reply);
r>>selector; switch(selector) {
case :
cmdstr = "/root/cpucheck.sh";
shell_call(cmdstr);
break;
case :
cmdstr = "/root/fscheck.sh";
shell_call(cmdstr);
break;
default:
std::cout<<"your select is out of range.\n";
}
} return ;
} 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 ;
}
对ip数据进行分类----c++的更多相关文章
- SNAT,是源地址转换,其作用是将ip数据包的源地址转换成另外一个地址
SNAT,可能有人觉得奇怪,好好的为什么要进行ip地址转换啊,为了弄懂这个问题,我们要看一下局域网用户上公网的原理,假设内网主机A(192.168.2.8)要和外网主机B(61.132.62.131) ...
- IP报文解析及基于IP 数据包的洪水攻击
版本(4bit) 报头长度(4bit) 优先级和服务类型(8bit) 总长度(16bit) 标识(16bit) 标志(3bit) 分段偏移(13bit) 存活期(8bit) 协议(8bit) 报头校验 ...
- TCP/IP数据包结构具体解释
[关键词] TCP IP 数据包 结构 具体解释 网络 协议 一般来说,网络编程我们仅仅须要调用一些封装好的函数或者组件就能完毕大部分的工作,可是一些特殊的情况下,就须要深入的理解 网络数据包的结构, ...
- Node.js学习之TCP/IP数据通讯
Node.js学习之TCP/IP数据通讯 1.使用net模块实现基于TCP的数据通讯 提供了一个net模块,专用于实现TCP服务器与TCP客户端之间的通信 1.1创建TCP服务器 在Node.js利用 ...
- 使用SVM对多类多维数据进行分类
最近,本人要做个小东西,使用SVM对8类三维数据进行分类,搜索网上,发现大伙讨论的都是二维数据的二分类问题,遂决定自己研究一番.本人首先参考了opencv的tutorial,这也是二维数据的二分类问题 ...
- Tracert(跟踪路由)是路由跟踪实用程序,用于确定 IP 数据包访问目标所采取的路径。
Tracert(跟踪路由)是路由跟踪实用程序,用于确定 IP 数据包访问目标所采取的路径. Tracert 命令用 IP 生存时间 (TTL) 字段和 ICMP 错误消息来确定从一个主机到网络上其 ...
- [TCP/IP] 网络层-抓包分析IP数据包首部
ip数据包的结构:首部+数据部分 1.版本(v4或者v6)+首部长度(固定的20字节,所以就没有)+区分服务优先级(我的例子是 assured forwarding 31 0x1a 26,保证转发) ...
- 用寄快递模拟数据在网络中的传送过程,发送一个ip数据报到接受这个ip数据报经历的过程
发送一个ip数据报到接受这个ip数据报经历的过程 好比寄一个快递的过程 这个包裹一共有三层, 这个包裹的最里面是数据,数据 第二层写着目的ip地址和源ip地址,即收件人和发件人的姓名 第三层也就是最外 ...
- TCP/IP 数据包报文格式(IP包、TCP报头、UDP报头)(转)
reference:http://blog.51cto.com/lyhbwwk/2162568 https://blog.csdn.net/wangzhen209 ...
随机推荐
- Linux 查询PID和端口号
https://www.cnblogs.com/understander/p/5546458.html
- ProE复杂曲线方程:Python Matplotlib 版本代码(L系统,吸引子和分形)
对生长自动机的研究由来已久,并在计算机科学等众多学科中,使用元胞自动机的概念,用于生长模拟.而复杂花纹的生成,则可以通过重写一定的生长规则,使用生成式来模拟自然纹理.当然,很多纹理是由人本身设计的,其 ...
- 非常好用的1款UI自动化测试工具:airTest
网易团队开发的UI自动化测试神器airTest,下载地址:http://airtest.netease.com/tutorial/Tutorial.html Appium和airTest对比,我的看法 ...
- 00_Rust安装及Hello World
Rust 官网: https://www.rust-lang.org 版本:nightly.beta.stable 如何设计语言的讨论:https://github.com/rust-lang/rfc ...
- Python - 模块(二)
目录 Python - 模块(二) re re下面的方法 logging os Python - 模块(二) re re模块提供了正则表达式的相关操作 主要字符介绍: . 通配符,除了换行符之外的任意 ...
- 8.1.2 Cursor 对象
游标Cursor也是sqlite3模块中比较重要的一个类,下面简单介绍下Cursor对象的常用方法. 1 execute(sql[,parameters]) 该方法用于执行一条SQL语句,下面的代码演 ...
- textarea 提交到数据库的内容,输出到 html 中显示正常的格式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【codeforces 755F】PolandBall and Gifts
[题目链接]:http://codeforces.com/contest/755/problem/F [题意] n个人; 计划是每个人都拿一个礼物来送给一个除了自己之外的人; 且如果一个人没有送出礼物 ...
- Java基础学习总结(73)——Java最新面试题汇总
1.super()与this()的区别? this():当前类的对象,super父类对象. super():在子类访问父类的成员和行为,必须受类继承规则的约束 而this他代表当前对象,当然所有的资源 ...
- c++ primer 第三章 标准库类型
1. string 标准库 1.1初始化 string s1; 默认构造函数s1为空 string s2(s1); 将s2初始化为s1的一个副本 string s3("value" ...