/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lw_ar_2;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
*
* @author eu
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws UnknownHostException, IOException {
List<String> ips = new ArrayList<String>();
List<String> ports = new ArrayList<String>();
int ipTimeout = 1000;
int portTimeout = 1000;
int ipRetries = 5;
int portRetries = 5;
String usage = "\n\rUsage: java -jar lw_ar_2.jar [-i IP [IP...]] [-p PORT [PORT...]] [OPTION...]\n\r" +
" -h print this help and exit\n\r" +
" -i IP_LIST ip's to scan\n\r" +
" -p PORT_LIST ports tp scan\n\r" +
" -it TIMEOUT timeout for ip scan. defaults to 1000 ms\n\r" +
" -pt TIMEOUT timeout for port scan. defaults to 1000 ms\n\r" +
" -ir RETRIES nr. of retries for ip scan. defaults to 5 times\n\r" +
" -pr RETRIES nr. of retries for port scan. default to 5 times\n\r" +
"\n\rWarning: option grouping not suported.\n\r";
String shortUsage = "Try java -jar lw_ar_2.jar -h for more information.";
if ( args.length < 1 ) {
throw new IllegalArgumentException("not enought arguments to run the program" + "\n\r" + shortUsage);
} else if ( args[0].equals("-h") && args.length > 1) {
throw new IllegalArgumentException("-h does not take any arguments" + "\n\r" + shortUsage);
} else if ( args[0].equals("-h")) {
System.out.println(usage);
System.exit(0);
} else {
for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("-")) {
if (args[i].equals("-i")) {
for (int j = i + 1; j < args.length; j++) {
if ( ! args[j].startsWith("-")) {
ips.add(args[j]);
} else {
break;
}
}
} else if (args[i].equals("-p")) {
for (int j = i + 1; j < args.length; j++) {
if (args[j].equals("all")) {
for (int l = 1; l < 65535; l++) {
ports.add(Integer.toString(l));
}
break;
} else if (args[j].equals("all_root")) {
for (int l = 1; l < 1024; l++) {
ports.add(Integer.toString(l));
}
break;
} else if ( ! args[j].startsWith("-")) {
ports.add(args[j]);
} else {
break;
}
}
} else if (args[i].equals("-it")) {
ipTimeout = Integer.parseInt(args[i+1]);
} else if (args[i].equals("-pt")) {
portTimeout = Integer.parseInt(args[i+1]);
} else if (args[i].equals("-ir")) {
ipRetries = Integer.parseInt(args[i+1]);
} else if (args[i].equals("-pr")) {
portRetries = Integer.parseInt(args[i+1]);
}//else if some new option
}
}
System.out.println("");
System.out.println("ips to scan :");
System.out.println(ips.toString());
System.out.println("");
System.out.println("ports to scan :");
System.out.println(ports.toString());
System.out.println("");
System.out.println("ipTimeout = " + ipTimeout);
System.out.println("portTimeout = " + portTimeout);
System.out.println("ipRetries = " + ipRetries);
System.out.println("portRetries = " + portRetries);
System.out.println("");
//throw new IllegalArgumentException("unknown arguments" + "\n\r" + shortUsage);
}
//parse ips (for example, 192.168.1.100/30 should be expanded etc.)
//parse ports (hz)
List<InetAddress> ipList = new ArrayList<InetAddress>();
byte[] byteIp = new byte[4]; // or 6
int i;
for (String ip : ips) {
i = 0;
for (String octet : ip.split("\\.")) {
byteIp[i] = (byte) Integer.parseInt(octet);
i++;
}
ipList.add(InetAddress.getByAddress(byteIp));
}
HostMonitor hostMonitor = new HostMonitor(ipList, ipTimeout, ipRetries);
Map<InetAddress,Boolean> hosts = hostMonitor.scan();
List<InetSocketAddress> inetSocketAddrList = new ArrayList<InetSocketAddress>();
Set<InetAddress> hostSet = hosts.keySet();
for (InetAddress inetAddr : hostSet) {
if (hosts.get(inetAddr)) {
for (String port : ports) {
InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddr, Integer.parseInt(port));
inetSocketAddrList.add(inetSocketAddress);
}
}
}
PortMonitor portMonitor = new PortMonitor(inetSocketAddrList, portTimeout, portRetries);
Map<InetSocketAddress,Boolean> services = portMonitor.scan();
Outputer outputer = new Outputer(hosts, services);
outputer.toSystemOut();
}
}
lw_ar_2.rar_The Client
版权申诉
127 浏览量
2022-09-19
15:58:53
上传
评论
收藏 4KB RAR 举报
邓凌佳
- 粉丝: 65
- 资源: 1万+