java調用全網最快端口掃描器

如果需要外部傳入參數,可以傳入args或者使用  讀取配置文件的方式

public class Execute {

    public static void main(String[] args) {
        scan();
    }

    public static void scan(){
        //這段代碼爲了打成jar包的時候能用=======================
        ResourceLoader resourceLoader=new DefaultResourceLoader();
        Resource resource=resourceLoader.getResource("classpath:s.exe");
        InputStream is=null;
        try {
            is=resource.getInputStream();
            FileOutputStream fos = new FileOutputStream("/D:/tmp/s.exe");
            byte[] b = new byte[1024];
            int length;
            while((length=is.read(b))>0){
                fos.write(b,0,length);
            }
            is.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //================================================================

        Runtime runtime=Runtime.getRuntime();
        BufferedReader stdInput=null;
        BufferedReader stdError=null;
        try {

            Process process = runtime.exec("/D:/tmp/s.exe TCP 192.168.0.1 192.168.0.254 22 50");
            //獲取正常輸出流
            stdInput= new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            //獲取錯誤輸出流
            stdError = new BufferedReader(
                    new InputStreamReader(process.getErrorStream()));
            System.out.println("output of the command:\n");
            String s = null;
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }

            System.out.println("error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (stdInput!=null){
                try {
                    stdInput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (stdError!=null){
                try {
                    stdError.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章