欢迎使用CSDN-markdown编辑器

来源:互联网 发布:做标准件网络销售需要 编辑:程序博客网 时间:2024/06/14 13:27

键盘第一次写的博客

host.list

package test;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class Hostlist {    public Set<String> getHost(String hostPath, String hostlistPath) throws Exception {        StringBuffer s = null;        Set<String> host = new HashSet<String>();        Set<String> hostSuccess = new HashSet<String>();        Set<String> hostFail = new HashSet<String>();        String encoding = "utf-8";        File file = new File(hostPath);        if (file.isFile() && file.exists()) { //            InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);//            BufferedReader bufferedReader = new BufferedReader(read);            String lineTxt = null;            Set<String> hostSet = new HashSet<String>();            while ((lineTxt = bufferedReader.readLine()) != null) {                String[] tmp = lineTxt.split("[+]");                for (String a : tmp) {                    System.out.println(" tmp :" + a);                }                if (tmp.length > 2) {                    if (tmp[1].toUpperCase().equals("HOST")) {                        host.add(tmp[0]);                        hostSet.add(tmp[0]);                    }                }            }            // storage at text            FileWriter fileWriter = new FileWriter(hostlistPath);            for (String string : hostSet) {                fileWriter.write(string + "\n");            }            fileWriter.flush();            fileWriter.close();            read.close();        } else {            System.out.println("找不到指定的文件");        }        return host;    }    public Set<String> getActiveHost(String path, Set<String> set) throws Exception {        Set<String> host = new HashSet<String>();        SAXReader sax = new SAXReader();// 创建一个SAXReader对象        File xmlFile = new File(path);// 根据指定的路径创建file对象        Document document = sax.read(xmlFile);// 获取document对象,如果文档无节点,则会抛出Exception提前结束        Element root = document.getRootElement();// 获取根节点        List<Element> childElements = root.elements();        for (Element child : childElements) {            if (child.getName().equals("host")) {                List<Element> elementList = child.elements();                int lenth = elementList.size();                Element ele;                for (int i = 0; i < lenth; i++) {                    ele = elementList.get(lenth - i - 1);                    if (ele.getName().equals("hostnames") && (ele.elements().size() != 0)) {                        List<Element> s = ele.elements();                        host.add(s.get(0).attributeValue("name"));                        set.remove(s.get(0).attributeValue("name"));                        break;                    } else if (ele.getName().equals("address")) {                        if ((ele.attributeValue("addr")).indexOf(":") == (-1))                            host.add(ele.attributeValue("addr"));                        set.remove(ele.attributeValue("addr"));                    }                }            }        }        return host;    }    public boolean removeFailHost(Set<String> host, String source, String last) {        String encoding = "utf-8";        try {            File file = new File(source);            if (file.isFile() && file.exists()) {                BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));                // File f2 = new File(last);                FileWriter write = new FileWriter(last);                String lineTxt = null;                Set<String> hostSet = new HashSet<String>();                while ((lineTxt = reader.readLine()) != null) {                    String[] tmp = lineTxt.split("[+]");                    if (host.contains(tmp[0])) {                        System.out.println(lineTxt);                        lineTxt = lineTxt + "\r\n";                    }                    write.write(lineTxt);                }                write.close();                reader.close();            }        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return true;    }    public Map<String, Set<String>> removeFailHost2(Set<String> host, String source, String last) {        String encoding = "utf-8";        Map<String, Set<String>> map = new HashMap<String, Set<String>>();        try {            File file = new File(source);            if (file.isFile() && file.exists()) {                BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));                // File f2 = new File(last);                String lineTxt = null;                Set<String> hostSet = new HashSet<String>();                while ((lineTxt = reader.readLine()) != null) {                    String[] tmp = lineTxt.split("[+]");                    if (host.contains(tmp[0])) {                        System.out.println(lineTxt);                        lineTxt = lineTxt + "\r\n";                        continue;                    }                    setHost(map, tmp[2], tmp[0]);                }                reader.close();            }        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return map;    }    public Map<String, Set<String>> setHost(Map<String, Set<String>> map, String port, String host) {        Set<String> tmp = new HashSet<String>();        if (map.get(port) != null) {            tmp = map.get(port);        }        tmp.add(host);        map.put(port, tmp);        return null;    }    public static void copyFile(String file1, String file2) throws IOException {        FileInputStream fis = new FileInputStream(file1);        FileOutputStream fos = new FileOutputStream(file2);        int temp;        while ((temp = fis.read()) != -1) {            fos.write(temp);        }        fis.close();        fos.close();        System.out.println("从" + file1 + "到" + file2);    }}

main

package test;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import java.util.regex.Matcher;import java.util.regex.Pattern;import org.junit.Test;public class main {    public String hostlist; // host list directory    public StringBuffer getRun(String dir, String command) {        StringBuffer stringBuffer = new StringBuffer();        try {            Process process = Runtime.getRuntime().exec(dir + " " + command);            System.out.println("please wait ...");            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));            String line = null;            while ((line = reader.readLine()) != null) {                stringBuffer.append(line + "\n");            }        }        catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return stringBuffer;    }    @Test    public void testName() throws Exception {        long start = System.currentTimeMillis();        StringBuffer s = null;        Set<String> host = new HashSet<String>();        Set<String> hostSuccess = new HashSet<String>();        Set<String> hostFail = new HashSet<String>();        String hostlistPath = "config/hostlist";        String host_result = " config/host_result.xml";        Hostlist hostlist = new Hostlist();        host = hostlist.getHost("config/host.txt", "config/hostlist");        long mid = System.currentTimeMillis();        s = getRun("C:\\Program Files (x86)\\Nmap\\nmap.exe",                "-sn --open -iL " + hostlistPath + " --no-stylesheet -oX  " + host_result);        boolean a = host.contains("qq.com");        System.out.println(a);        hostSuccess = hostlist.getActiveHost("config/host_result.xml", host);        long end = System.currentTimeMillis();        a = host.contains("qq.com");        System.out.println(a);        System.out.println("start : " + start + " | mid:" + mid + " |end:" + end);        // s = getRun("C:\\Program Files (x86)\\Nmap\\nmap.exe", " -sP -iL "+);        System.out.print(s);        start = System.currentTimeMillis();        Map<String, Set<String>> map = hostlist.removeFailHost2(host, "config/host.txt", "config/hostSuccess");        end = System.currentTimeMillis();        System.out.println("start : " + start + " |  |end:" + end + "   result  " + (start - end));        Iterator<Entry<String, Set<String>>> iter = map.entrySet().iterator();        while (iter.hasNext()) {            Map.Entry entry = (Map.Entry) iter.next();            String key = (String) entry.getKey();            Set<String> val = map.get(key);            System.out.println("key : " + entry.getKey());            System.out.println("val : "+val);            s = getRun("C:\\Program Files (x86)\\Nmap\\nmap.exe","");            FileWriter write = new FileWriter("config/"+key);            for(String tmp : val){                write.write(tmp+"\r\n");            }            write.close();//          for (String tmp : val) {//              System.out.println("host : "+ host);//          }        }    }    @Test    public void test() {    }}
0 0
原创粉丝点击