Java 调用外部s扫描并获取扫描结果

yzmm
650 阅读
这个实在太简单了,不过还是记录下来吧。java.lang.Runtime.getRuntime().exec("xxxxx");执行完之后其实返回的是一个流对象,那么也就是说调用一个外部的exe之类执行过程中的结果也可以在流中看到。调用后记得关闭后台的exe进程哦,偷懒就不写了。

简单实现代码:


package net.ltan.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class STest {
	public static List<String> test(String type,String ip,int port,int th){
		String cmd = "c:/s.exe "+type+" "+ip+" "+port+" "+th;
		Pattern reg =Pattern.compile("(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})");
		List<String> ls = new ArrayList<String>();
		try {
			Process p = java.lang.Runtime.getRuntime().exec("cmd /c "+cmd);
			BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream(),"UTF-8"));
			String temp;
			while ((temp = in.readLine()) != null) {
				Matcher matcher = reg.matcher(temp);
				if(matcher.find()){
					ls.add(matcher.group());
					System.out.println(temp);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ls;
	}
	public static void main(String[] args) {
		List<String> ls = test("TCP","210.73.128.10 210.73.164.10",8080,512);
		System.out.println("---------------------------------------------");
		for(String s:ls){
			System.out.println(s);
		}
	}
}

评论 (0)

发表评论