package com.dcm360.common.controller;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.net.telnet.TelnetClient;
import com.jfinal.core.Controller;
public class IpServicedistribution extends Controller{
List> list =new
ArrayList>();
static MapmapRed=new
LinkedHashMap();
static MapmapGreen=new
LinkedHashMap();
static MapmapGray=new
LinkedHashMap();
public void index() {
render("ip_servicedistribution.html");
}
public void allPort(String startIp,String endIp,int port)
{
String ip ="192.168.1.31";
}
//让ip自增
public static void ipIncrease(String startIp,String endIp,int
port) {
String[] endIps = endIp.split("\\.");
Integer endIpc = Integer.valueOf(endIps[2]);
Integer endIpd = Integer.valueOf(endIps[3]);
String[] startIps = startIp.split("\\.");
Integer startIpa = Integer.valueOf(startIps[0]);
Integer startIpb = Integer.valueOf(startIps[1]);
Integer startIpc = Integer.valueOf(startIps[2]);
Integer startIpd = Integer.valueOf(startIps[3]);
if(startIpc==endIpc) {
if(startIpd<=endIpd) {
String str=startIpa+"."+startIpb+"."+startIpc+".";
for(int i=startIpd;i<=endIpd;i++) {
String append=str+i;
boolean checkPort= checkPort(append,port);
judge(checkPort,append,port);
}
}else {
System.err.println("ipd输入范围错误");
}
}else if(startIpc!=endIpc){
if(startIpc
String ipb=startIpa+"."+startIpb+".";
for(int j=startIpc;j<=endIpc;j++) {
String ipc=ipb+j+".";
if(j==startIpc) {
for(int i=startIpd;i<=255;i++) {
String append=ipc+i;
boolean checkPort= checkPort(append,port);
judge(checkPort,append,port);
}
}
if(j==endIpc) {
for(int i=0;i<=endIpd;i++) {
String append=ipc+i;
boolean checkPort= checkPort(append,port);
judge(checkPort,append,port);
}
}
if(startIpc
for(int i=0;i<=255;i++) {
String append=ipc+i;
boolean checkPort= checkPort(append,port);
judge(checkPort,append,port);
}
}
}
}else {
System.out.println("ipc输入范围错误");
}
}else {
System.out.println("请输入正确的查询ip字段");
}
}
//判断端口是否启用后,把不通的数据分配到3各集合中
public static void judge(boolean flag,String append,int port)
{
if(flag) {
if(mapGreen.containsKey(append) &&
!mapGreen.get(append).equals(port)) {
mapGreen.remove(append);
mapRed.put(append, port);
System.out.println(append+":"+port+":该服务器已经启用多个端口");
}else {
mapGreen.put(append, port);
System.out.println(append+":"+port+":端口已经开启");
}
}else {
mapGray.put(append, port);
System.out.println(append+":"+port+"端口未开启");
}
}
//检查端口是否被占用
public static boolean checkPort(String ip,int port) {
boolean flag=false;
TelnetClient telnet=new TelnetClient();
try {
telnet.setConnectTimeout(500);
telnet.connect(ip,port);
flag=true;
} catch (Exception e) {
System.err.println("is not reachable!!");
}
return flag;
}
public static void main(String[] args) {
ipIncrease("192.168.1.10","192.168.1.39",3306);
ipIncrease("192.168.1.10","192.168.1.39",81);
ipIncrease("192.168.1.10","192.168.1.39",8080);
System.out.println(mapGray);
System.out.println(mapGreen);
System.out.println(mapRed);
}
//把字符串转换成数字整数
public Integer ipToInt(String ip) {
String replace = ip.trim().replace(".","");
Integer valueOf = Integer.valueOf(replace);
return valueOf;
}
}