1
2
3
4
5
6
7
8
9
10
11 import java.io.BufferedWriter;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.OutputStream;
15 import java.io.OutputStreamWriter;
16 import java.io.PrintWriter;
17 import java.net.Inet4Address;
18 import java.net.InetAddress;
19 import java.net.Socket;
20
21 public class Whois {
22 public final static int WHOIS_PORT = 43;
23 public static boolean TRACE = false;
24
25 public static void main(String[] args) throws Exception {
26 if (args.length < 1) {
27 System.out.println("Copyright 1999-2005 by Java Service Network Community, KOREA.");
28 System.out.println("All rights reserved. http://www.javaservice.net, lwy@javaservice.com");
29 System.out.println();
30 System.out.println("Usage: java Whois <ip>");
31 System.out.println("Ex: java Whois 128.38.12.6");
32 System.out.println();
33 System.exit(1);
34 }
35
36 if ("true".equals( System.getProperty("trace")))
37 TRACE = true;
38 System.out.println(whois(args[0]));
39
40 System.out.println("Whois got result successfully.\n");
41 System.out.println("Copyright 1999-2005 by Java Service Network Community, KOREA.");
42 System.out.println("All rights reserved. http://www.javaservice.net, lwy@javaservice.com");
43 }
44
45 public static String whois(String ip) throws Exception {
46 if (ip == null) {
47 throw new NullPointerException("ip address should not be null.");
48 }
49
50 InetAddress inetAddr = InetAddress.getByName(ip);
51 ip = inetAddr.getHostAddress();
52
53 if (ip.startsWith("10.") ||
54 ip.startsWith("172.16.") ||
55 ip.startsWith("192.168.")) {
56 return "Warning : Private network IP address.";
57 }
58 else if (ip.startsWith("127.")) {
59 return "Warning : Loopback Address.";
60 }
61 else if (ip.startsWith("224.") || ip.startsWith("239.")) {
62 return "Warning : Multicast Address.";
63 }
64 else if (ip.startsWith("240.") || ip.startsWith("255.")) {
65 return "Warning : Experimental Address.";
66 }
67
68 try {
69 return whoisSend("whois.nic.or.kr", ip, "기관");
70 }
71 catch (Exception e) {
72 try {
73 String msg = whoisSend("ws.arin.net", ip, "OrgName");
74
75 if (msg == null) {
76 return "";
77 }
78 else if (msg.indexOf("reserved for special purposes") != -1) {
79 return "reserved for special purposes";
80 }
81 else if (msg.indexOf("APNIC") != -1) {
82 msg = whoisSend("whois.apnic.net", ip, "netname:");
83 }
84
85 return msg;
86 }
87 catch (Exception e2) {
88 throw e2;
89 }
90 }
91 }
92
93 private static String whoisSend(String Server, String ipAddr, String return_ok_str) throws Exception {
94 Socket s = new Socket(Server, WHOIS_PORT);
95 StringBuffer buf = new StringBuffer();
96 String msg = null;
97
98 InputStream in = null;
99 OutputStream out = null;
100
101 try {
102 in = s.getInputStream();
103 out = s.getOutputStream();
104
105 PrintWriter pw =
106 new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
107
108 pw.println(ipAddr);
109 pw.flush();
110
111 String line = null;
112 while ((line = read_line(in)) != null) {
113 if (TRACE) {
114 System.err.println(line);
115 }
116
117 buf.append(line);
118 buf.append("\n");
119 }
120
121 msg = buf.toString().trim();
122
123 if (msg.length() == 0) {
124 throw new Exception("unexcepted header data format");
125 }
126
127 }
128 finally {
129 if (in != null)
130 try {
131 in.close();
132 }
133 catch (Exception e) {
134 }
135 if (out != null)
136 try {
137 out.close();
138 }
139 catch (Exception e) {
140 }
141 if ( s != null )
142 try {
143 s.close();
144 }
145 catch (Exception e) {
146 }
147 }
148
149 int x = msg.indexOf(return_ok_str);
150
151 if (x == -1) {
152 throw new Exception(msg);
153 }
154
155 return getline(msg.substring(x));
156
157 }
158
159 public static String getline(String s) {
160 if (s == null)
161 return null;
162
163 StringBuffer buf = new StringBuffer();
164 for (int i = 0; i < s.length(); i++) {
165 int x = s.indexOf( '\n');
166 if (x == -1) {
167 buf.append(s);
168 break;
169 }
170 else if (x == s.length() - 1) {
171 buf.append(s);
172 break;
173 }
174 else {
175 String tmpLine = s.substring(0, x + 1);
176 if (tmpLine.trim().length() == 0) {
177 break;
178 }
179
180 buf.append(tmpLine);
181 s = s.substring(x + 1);
182 }
183 }
184
185 return buf.toString();
186 }
187
188
189
190
191
192
193
194
195
196
197
198 private static String read_line(InputStream in) throws Exception {
199 java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
200 boolean eof = false;
201 while (true) {
202 int b = in.read();
203 if (b == -1) {
204 eof = true;
205 break;
206 }
207 if (b != '\r' && b != '\n') {
208 bout.write( (byte) b );
209 }
210 if (b == '\n') {
211 break;
212 }
213 }
214 bout.flush();
215 if (eof && bout.size() == 0) {
216 return null;
217 }
218
219 return bout.toString();
220 }
221 }
참조 : http://www.javaservice.net/~java/bbs/read.cgi?m=devtip&b=javatip&c=r_p&n=1121054302&k=Whois&d=tb#1121054302
CategoryJava
ZbmonWiki: Whois.java (2005-11-07 13:51:51에 zbmon가(이) 마지막으로 수정)