-
Notifications
You must be signed in to change notification settings - Fork 1
/
HostRequest.java
88 lines (55 loc) · 1.69 KB
/
HostRequest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.IOException;
public class HostRequest extends Main {
private static final String IPADDRESS_PATTERN = "([01]?\\d\\d?|2[0-4]\\d|25[0-5])" + "\\.([01]?\\d\\d?|25[0-5])" + "\\.([01]?\\d\\d?|25[0-5])" + "\\.([01]?\\d\\d?|25[0-5])";
private static Pattern pattern = Pattern.compile(IPADDRESS_PATTERN);
private static Matcher matcher;
String list_output = "";
public List<String> host(String url) {
String domainName = url;
String command = "host -t a " + domainName;
String stats = executeCommand(command);
List<String> list = getIpAddress(stats);
if (list.size() > 0) {
for (String ip : list) {
list_output = ip + "\n";
}
} else {
System.out.println("No Address found.\n");
}
return list;
}
public String executeCommand(String command) {
StringBuffer stats = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!=null) {
stats.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return stats.toString();
}
public List<String> getIpAddress(String msg) {
List<String> ipList = new ArrayList<String>();
if (msg == null || msg.equals(""))
return ipList;
matcher = pattern.matcher(msg);
while (matcher.find()) {
ipList.add(matcher.group(0));
}
return ipList;
}
}