#!/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++的更多相关文章

  1. SNAT,是源地址转换,其作用是将ip数据包的源地址转换成另外一个地址

    SNAT,可能有人觉得奇怪,好好的为什么要进行ip地址转换啊,为了弄懂这个问题,我们要看一下局域网用户上公网的原理,假设内网主机A(192.168.2.8)要和外网主机B(61.132.62.131) ...

  2. IP报文解析及基于IP 数据包的洪水攻击

    版本(4bit) 报头长度(4bit) 优先级和服务类型(8bit) 总长度(16bit) 标识(16bit) 标志(3bit) 分段偏移(13bit) 存活期(8bit) 协议(8bit) 报头校验 ...

  3. TCP/IP数据包结构具体解释

    [关键词] TCP IP 数据包 结构 具体解释 网络 协议 一般来说,网络编程我们仅仅须要调用一些封装好的函数或者组件就能完毕大部分的工作,可是一些特殊的情况下,就须要深入的理解 网络数据包的结构, ...

  4. Node.js学习之TCP/IP数据通讯

    Node.js学习之TCP/IP数据通讯 1.使用net模块实现基于TCP的数据通讯 提供了一个net模块,专用于实现TCP服务器与TCP客户端之间的通信 1.1创建TCP服务器 在Node.js利用 ...

  5. 使用SVM对多类多维数据进行分类

    最近,本人要做个小东西,使用SVM对8类三维数据进行分类,搜索网上,发现大伙讨论的都是二维数据的二分类问题,遂决定自己研究一番.本人首先参考了opencv的tutorial,这也是二维数据的二分类问题 ...

  6. Tracert(跟踪路由)是路由跟踪实用程序,用于确定 IP 数据包访问目标所采取的路径。

    Tracert(跟踪路由)是路由跟踪实用程序,用于确定 IP 数据包访问目标所采取的路径.   Tracert 命令用 IP 生存时间 (TTL) 字段和 ICMP 错误消息来确定从一个主机到网络上其 ...

  7. [TCP/IP] 网络层-抓包分析IP数据包首部

    ip数据包的结构:首部+数据部分 1.版本(v4或者v6)+首部长度(固定的20字节,所以就没有)+区分服务优先级(我的例子是 assured forwarding 31 0x1a 26,保证转发) ...

  8. 用寄快递模拟数据在网络中的传送过程,发送一个ip数据报到接受这个ip数据报经历的过程

    发送一个ip数据报到接受这个ip数据报经历的过程 好比寄一个快递的过程 这个包裹一共有三层, 这个包裹的最里面是数据,数据 第二层写着目的ip地址和源ip地址,即收件人和发件人的姓名 第三层也就是最外 ...

  9. TCP/IP 数据包报文格式(IP包、TCP报头、UDP报头)(转)

    reference:http://blog.51cto.com/lyhbwwk/2162568                    https://blog.csdn.net/wangzhen209 ...

随机推荐

  1. cookie范例

    GET /locate/api/getLocByIp?key=C6E22B7D480E3312C74EC7EF013E50C5&callback=bowlder.cb._0 HTTP/1.1 ...

  2. 前端精选文摘:css之GFC 神奇背后的原理(整理)

    CSS3 Grid Layout Web页面的布局,我们常见的主要有“浮动布局(float)”.“定位布局(position)”.“行内块布局(inline-block)”.“CSS3的多栏布局(Co ...

  3. 2018年为什么要学习Linux?Linux运维的前景还好吗?

    Linux一直是很多人入行IT的首选,无论是从入行难度还是职业寿命来说,Linux运维都比开发有着更大的优势.为了得到高薪工作,很多人在Linux学习过程中付出了非常大的努力,最终也得到了不错的收获. ...

  4. maxtrid 3D视差

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Oracle数据库的自动备份脚本

    @echo off echo ================================================ echo Windows环境下Oracle数据库的自动备份脚本 echo ...

  6. display: table-cell; 自适应布局

    #extras {display: table-cell;width: 180px;padding-left: 10px;border-right: 1px dotted #d7ad7b;} tabl ...

  7. golang实现高阶函数之map

    package main import "fmt" func iMap(num []int, f func(a int) int) []int{ var r []int for _ ...

  8. PHP面试:说下什么是堆和堆排序?

    堆是什么? 堆是基于树抽象数据类型的一种特殊的数据结构,用于许多算法和数据结构中.一个常见的例子就是优先队列,还有排序算法之一的堆排序.这篇文章我们将讨论堆的属性.不同类型的堆以及堆的常见操作.另外我 ...

  9. -------------Django-----URLS路由

    一.相约Django. 1.Django的特点:Django定义了服务分布.路由映射.模板编程.数据处理的一套完整的功能. (1)集成数据访问组件:Django的model层自带数据库ORM组件. ( ...

  10. PAT 1098. Insertion or Heap Sort

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...