我写了一段Java程序,给一个参数 比如 134.23.33.0/27
27是子网掩码 算法32-27=5, 2的5次方是32,这样就是扫描134.23.33.1-134.23.33.31网段上的主机(端口80) ,看他们的web Server的类型是什么,比如Apache 或者IIS 之类的
我基本的功能都实现了,但是一些高级的error handling,比如timeout, retry和Parse Http Response我不是很理解是什么含义,这个timeout的设置有什么说法吗?
还有就是我写的Java不是很多,能给我点建议嘛,比如规范和设计方面的,谢谢!import java.io.*;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.MalformedURLException;public class webScann
{
private static HttpURLConnection connection;
/**
* @param args
*/
private static final int timeout = 3000;//这里该设置多大的值怎么知道呢?
public static void main (String args[])
{
int rangePosition;
int range;
int r;
int webRange;
int IPPosition;
int lastIP;
String IPAddress = args[0];
String newLastIP;
/*这里在算要扫描的网段的IP*/
rangePosition = args[0].indexOf("/");
System.out.println("The position of '/' is " + rangePosition);
range = Integer.parseInt(args[0].substring(rangePosition+1));
r = 32 - range;
Double d = new Double(Math.pow(2,r));
webRange = d.intValue();
System.out.println("The web range is "+ webRange);
System.out.println("The pow(2,3) is " + Math.pow(2,3));
IPPosition = args[0].lastIndexOf(".");
lastIP = Integer.parseInt(args[0].substring(IPPosition+1,rangePosition));
//这里开始进行扫描
for (int i = 1;i < webRange; i++)
{
System.out.println("--------------------------");
newLastIP = Integer.toString(lastIP+i);
try{
IPAddress = "http://"+args[0].substring(0, IPPosition)+"."+newLastIP+":80";//这是组合成的要扫描的当前server的IP
URL url = new URL(IPAddress);
connection = (HttpURLConnection)url.openConnection();
try{
connection.setConnectTimeout(timeout);/*是不是自己设一个timeout这样就不会长久的等待,只要超过这个timeout时间限制就让connect()方法抛异常了?*/
connection.connect();
System.out.println("ResponseMessage is " + connection.getResponseMessage());/*如果抛异常那么就不print这句了,那么抛了异常的还能Parse Http Response嘛?*/
}catch (SocketTimeoutException s)
{
System.out.println("Connect Time out!");
}
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
} String t = connection.getHeaderField("Server");//我知道这里给这个关键字就能给server的类型,但是怎么能看到完整的HeaderField?
if (t == null)
{
t = "Not Identified";//如果主机类型没有找到就输出这个Not Identified
}
System.out.println(args[0].substring(0, IPPosition)+"."+newLastIP+ " " + t);
System.out.println("--------------------------");
}
}
}
27是子网掩码 算法32-27=5, 2的5次方是32,这样就是扫描134.23.33.1-134.23.33.31网段上的主机(端口80) ,看他们的web Server的类型是什么,比如Apache 或者IIS 之类的
我基本的功能都实现了,但是一些高级的error handling,比如timeout, retry和Parse Http Response我不是很理解是什么含义,这个timeout的设置有什么说法吗?
还有就是我写的Java不是很多,能给我点建议嘛,比如规范和设计方面的,谢谢!import java.io.*;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.MalformedURLException;public class webScann
{
private static HttpURLConnection connection;
/**
* @param args
*/
private static final int timeout = 3000;//这里该设置多大的值怎么知道呢?
public static void main (String args[])
{
int rangePosition;
int range;
int r;
int webRange;
int IPPosition;
int lastIP;
String IPAddress = args[0];
String newLastIP;
/*这里在算要扫描的网段的IP*/
rangePosition = args[0].indexOf("/");
System.out.println("The position of '/' is " + rangePosition);
range = Integer.parseInt(args[0].substring(rangePosition+1));
r = 32 - range;
Double d = new Double(Math.pow(2,r));
webRange = d.intValue();
System.out.println("The web range is "+ webRange);
System.out.println("The pow(2,3) is " + Math.pow(2,3));
IPPosition = args[0].lastIndexOf(".");
lastIP = Integer.parseInt(args[0].substring(IPPosition+1,rangePosition));
//这里开始进行扫描
for (int i = 1;i < webRange; i++)
{
System.out.println("--------------------------");
newLastIP = Integer.toString(lastIP+i);
try{
IPAddress = "http://"+args[0].substring(0, IPPosition)+"."+newLastIP+":80";//这是组合成的要扫描的当前server的IP
URL url = new URL(IPAddress);
connection = (HttpURLConnection)url.openConnection();
try{
connection.setConnectTimeout(timeout);/*是不是自己设一个timeout这样就不会长久的等待,只要超过这个timeout时间限制就让connect()方法抛异常了?*/
connection.connect();
System.out.println("ResponseMessage is " + connection.getResponseMessage());/*如果抛异常那么就不print这句了,那么抛了异常的还能Parse Http Response嘛?*/
}catch (SocketTimeoutException s)
{
System.out.println("Connect Time out!");
}
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
} String t = connection.getHeaderField("Server");//我知道这里给这个关键字就能给server的类型,但是怎么能看到完整的HeaderField?
if (t == null)
{
t = "Not Identified";//如果主机类型没有找到就输出这个Not Identified
}
System.out.println(args[0].substring(0, IPPosition)+"."+newLastIP+ " " + t);
System.out.println("--------------------------");
}
}
}
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.MalformedURLException;
public class webScann
{
private static HttpURLConnection connection;
/**
* @param args
*/
// private static final int timeout = 3000;
public static void main (String args[])
{
int rangePosition;
int range;
int r;
int webRange;
int IPPosition;
int lastIP;
String IPAddress = args[0];
String newLastIP;
rangePosition = args[0].indexOf("/");
System.out.println("The position of '/' is " + rangePosition);
range = Integer.parseInt(args[0].substring(rangePosition+1));
r = 32 - range;
Double d = new Double(Math.pow(2,r));
webRange = d.intValue();
System.out.println("The web range is "+ webRange);
IPPosition = args[0].lastIndexOf(".");
lastIP = Integer.parseInt(args[0].substring(IPPosition+1,rangePosition));
for (int i = 1;i < webRange; i++)
{
System.out.println("--------------------------");
newLastIP = Integer.toString(lastIP+i);
try{
IPAddress = "http://"+args[0].substring(0, IPPosition)+"."+newLastIP+":80";
URL url = new URL(IPAddress); connection = (HttpURLConnection)url.openConnection();
for(int j=1;j<4;j++)
{
webConnection wc = new webConnection(connection);
if (wc.webConn()==true)
{
System.out.println("Connection is successful!");
break;
}
else
System.out.println("Connection Timeout! Times: " + j+" Please try another connection!");
}
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
String t = connection.getHeaderField("Server");
if (t == null)
{
t = "Not Identified";
}
System.out.println(args[0].substring(0, IPPosition)+"."+newLastIP+ " " + t);
System.out.println("--------------------------");
}
}
}class webConnection
{
private static final int timeout = 3000;
HttpURLConnection conn;
public webConnection(HttpURLConnection connection)
{
conn = connection;
}
public boolean webConn()
{
try{
conn.setConnectTimeout(timeout);
conn.connect();
System.out.println("ResponseMessage is " + conn.getResponseMessage());
return true;
}catch (SocketTimeoutException s)
{
System.out.println("Connect Time out!");
return false;
}
catch(IOException e)
{
System.out.println(e.toString());
return false;
}
}
}