对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 ...
随机推荐
- Python 内置函数 day4
import random s = 'abczfg' st= {3,4,9,1,8} print(dir(random))#打印模块内的方法,输出模块/变量可以调用的方法 print(dir(s))# ...
- openstack——nova计算服务
一.nova介绍 Nova 是 OpenStack 最核心的服务,负责维护和管理云环境的计算资源.OpenStack 作为 IaaS 的云操作系统,虚拟机生命周期管理也就是 ...
- 【vue】vue中实现标签页
前言 tab标签页实现很多, 纯css实现, js实现等, 外加一些特殊动画. vue中实现标签页实现 keep-alive标签和is特性 vue-router中嵌套路由 is特性实现(推荐) 优点: ...
- 洛谷——P2341 [HAOI2006]受欢迎的牛//POJ2186:Popular Cows
P2341 [HAOI2006]受欢迎的牛/POJ2186:Popular Cows 题目背景 本题测试数据已修复. 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所 ...
- HDU-1864&&HDU-2602(01背包问题)
DP-01背包问题例题 输入处理有点恶心人,不过处理完后就是简单的DP了 从头开始dp[i]表示从0开始到i的最优结果,最后从都边里dp数组,求得最大的报销额. 对于每个i都要从头维护最优结果.(二刷 ...
- JAVA学习总结-基础语法
/** * 这篇文章供自己学习JAVA总结回顾使用 * 主要借鉴了马士兵老师的视频进行总结 * @author Kingram */ 标识符的概念和命名规则 JAVA常量---不可变的变量 程序的执行 ...
- 关于MySQL中自增的理解和设置
show create table t10;--查看表的创建结果 show create table t10\G;--竖列查看 --设置自增为20 ); insert into t2(name) va ...
- 【codeforces 796C】Bank Hacking(用一些技巧来代替multiset)
[题目链接]:http://codeforces.com/contest/796/problem/C [题意] 给你n个节点,你一开始选择一个节点,然后打掉它,然后与被打掉过的节点相连的节点才能被 打 ...
- 51nod挑的部分5级题
最近心情不好所以写代码来获得快落 4级题有点难做?然后就开始挑简单的5级题开始写 然后准备记录一些自己没有做出来 参考讨论区或者博客才做出来的题目 51nod_1189 阶乘分数 这个题参考了讨论区 ...
- Dubbo学习总结(2)——Dubbo架构详解
一.前言 部门去年年中开始各种改造,第一步是模块服务化,这边初选dubbo试用在一些非重要模块上,慢慢引入到一些稍微重要的功能上,半年时间,学习过程及线上使用遇到的些问题在此总结下. 整理这篇文章差不 ...