1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package examples.telnet;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.io.FileOutputStream;
24 import java.util.StringTokenizer;
25
26 import org.apache.commons.net.telnet.TelnetClient;
27 import org.apache.commons.net.telnet.TelnetNotificationHandler;
28 import org.apache.commons.net.telnet.SimpleOptionHandler;
29 import org.apache.commons.net.telnet.EchoOptionHandler;
30 import org.apache.commons.net.telnet.TerminalTypeOptionHandler;
31 import org.apache.commons.net.telnet.SuppressGAOptionHandler;
32 import org.apache.commons.net.telnet.InvalidTelnetOptionException;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class TelnetClientExample implements Runnable, TelnetNotificationHandler
51 {
52 static TelnetClient tc = null;
53
54
55
56
57 public static void main(String[] args) throws Exception
58 {
59 FileOutputStream fout = null;
60
61 if(args.length < 1)
62 {
63 System.err.println("Usage: TelnetClientExample1 <remote-ip> [<remote-port>]");
64 System.exit(1);
65 }
66
67 String remoteip = args[0];
68
69 int remoteport;
70
71 if (args.length > 1)
72 {
73 remoteport = (new Integer(args[1])).intValue();
74 }
75 else
76 {
77 remoteport = 23;
78 }
79
80 try
81 {
82 fout = new FileOutputStream ("spy.log", true);
83 }
84 catch (IOException e)
85 {
86 System.err.println(
87 "Exception while opening the spy file: "
88 + e.getMessage());
89 }
90
91 tc = new TelnetClient();
92
93 TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler("VT100", false, false, true, false);
94 EchoOptionHandler echoopt = new EchoOptionHandler(true, false, true, false);
95 SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true, true, true, true);
96
97 try
98 {
99 tc.addOptionHandler(ttopt);
100 tc.addOptionHandler(echoopt);
101 tc.addOptionHandler(gaopt);
102 }
103 catch (InvalidTelnetOptionException e)
104 {
105 System.err.println("Error registering option handlers: " + e.getMessage());
106 }
107
108 while (true)
109 {
110 boolean end_loop = false;
111 try
112 {
113 tc.connect(remoteip, remoteport);
114
115
116 Thread reader = new Thread (new TelnetClientExample());
117 tc.registerNotifHandler(new TelnetClientExample());
118 System.out.println("TelnetClientExample");
119 System.out.println("Type AYT to send an AYT telnet command");
120 System.out.println("Type OPT to print a report of status of options (0-24)");
121 System.out.println("Type REGISTER to register a new SimpleOptionHandler");
122 System.out.println("Type UNREGISTER to unregister an OptionHandler");
123 System.out.println("Type SPY to register the spy (connect to port 3333 to spy)");
124 System.out.println("Type UNSPY to stop spying the connection");
125
126 reader.start();
127 OutputStream outstr = tc.getOutputStream();
128
129 byte[] buff = new byte[1024];
130 int ret_read = 0;
131
132 do
133 {
134 try
135 {
136 ret_read = System.in.read(buff);
137 if(ret_read > 0)
138 {
139 if((new String(buff, 0, ret_read)).startsWith("AYT"))
140 {
141 try
142 {
143 System.out.println("Sending AYT");
144
145 System.out.println("AYT response:" + tc.sendAYT(5000));
146 }
147 catch (IOException e)
148 {
149 System.err.println("Exception waiting AYT response: " + e.getMessage());
150 }
151 }
152 else if((new String(buff, 0, ret_read)).startsWith("OPT"))
153 {
154 System.out.println("Status of options:");
155 for(int ii=0; ii<25; ii++) {
156 System.out.println("Local Option " + ii + ":" + tc.getLocalOptionState(ii) + " Remote Option " + ii + ":" + tc.getRemoteOptionState(ii));
157 }
158 }
159 else if((new String(buff, 0, ret_read)).startsWith("REGISTER"))
160 {
161 StringTokenizer st = new StringTokenizer(new String(buff));
162 try
163 {
164 st.nextToken();
165 int opcode = Integer.parseInt(st.nextToken());
166 boolean initlocal = Boolean.parseBoolean(st.nextToken());
167 boolean initremote = Boolean.parseBoolean(st.nextToken());
168 boolean acceptlocal = Boolean.parseBoolean(st.nextToken());
169 boolean acceptremote = Boolean.parseBoolean(st.nextToken());
170 SimpleOptionHandler opthand = new SimpleOptionHandler(opcode, initlocal, initremote,
171 acceptlocal, acceptremote);
172 tc.addOptionHandler(opthand);
173 }
174 catch (Exception e)
175 {
176 if(e instanceof InvalidTelnetOptionException)
177 {
178 System.err.println("Error registering option: " + e.getMessage());
179 }
180 else
181 {
182 System.err.println("Invalid REGISTER command.");
183 System.err.println("Use REGISTER optcode initlocal initremote acceptlocal acceptremote");
184 System.err.println("(optcode is an integer.)");
185 System.err.println("(initlocal, initremote, acceptlocal, acceptremote are boolean)");
186 }
187 }
188 }
189 else if((new String(buff, 0, ret_read)).startsWith("UNREGISTER"))
190 {
191 StringTokenizer st = new StringTokenizer(new String(buff));
192 try
193 {
194 st.nextToken();
195 int opcode = (new Integer(st.nextToken())).intValue();
196 tc.deleteOptionHandler(opcode);
197 }
198 catch (Exception e)
199 {
200 if(e instanceof InvalidTelnetOptionException)
201 {
202 System.err.println("Error unregistering option: " + e.getMessage());
203 }
204 else
205 {
206 System.err.println("Invalid UNREGISTER command.");
207 System.err.println("Use UNREGISTER optcode");
208 System.err.println("(optcode is an integer)");
209 }
210 }
211 }
212 else if((new String(buff, 0, ret_read)).startsWith("SPY"))
213 {
214 tc.registerSpyStream(fout);
215 }
216 else if((new String(buff, 0, ret_read)).startsWith("UNSPY"))
217 {
218 tc.stopSpyStream();
219 }
220 else
221 {
222 try
223 {
224 outstr.write(buff, 0 , ret_read);
225 outstr.flush();
226 }
227 catch (IOException e)
228 {
229 end_loop = true;
230 }
231 }
232 }
233 }
234 catch (IOException e)
235 {
236 System.err.println("Exception while reading keyboard:" + e.getMessage());
237 end_loop = true;
238 }
239 }
240 while((ret_read > 0) && (end_loop == false));
241
242 try
243 {
244 tc.disconnect();
245 }
246 catch (IOException e)
247 {
248 System.err.println("Exception while connecting:" + e.getMessage());
249 }
250 }
251 catch (IOException e)
252 {
253 System.err.println("Exception while connecting:" + e.getMessage());
254 System.exit(1);
255 }
256 }
257 }
258
259
260
261
262
263
264
265
266
267
268
269
270
271 public void receivedNegotiation(int negotiation_code, int option_code)
272 {
273 String command = null;
274 if(negotiation_code == TelnetNotificationHandler.RECEIVED_DO)
275 {
276 command = "DO";
277 }
278 else if(negotiation_code == TelnetNotificationHandler.RECEIVED_DONT)
279 {
280 command = "DONT";
281 }
282 else if(negotiation_code == TelnetNotificationHandler.RECEIVED_WILL)
283 {
284 command = "WILL";
285 }
286 else if(negotiation_code == TelnetNotificationHandler.RECEIVED_WONT)
287 {
288 command = "WONT";
289 }
290 System.out.println("Received " + command + " for option code " + option_code);
291 }
292
293
294
295
296
297
298
299 public void run()
300 {
301 InputStream instr = tc.getInputStream();
302
303 try
304 {
305 byte[] buff = new byte[1024];
306 int ret_read = 0;
307
308 do
309 {
310 ret_read = instr.read(buff);
311 if(ret_read > 0)
312 {
313 System.out.print(new String(buff, 0, ret_read));
314 }
315 }
316 while (ret_read >= 0);
317 }
318 catch (IOException e)
319 {
320 System.err.println("Exception while reading socket:" + e.getMessage());
321 }
322
323 try
324 {
325 tc.disconnect();
326 }
327 catch (IOException e)
328 {
329 System.err.println("Exception while closing telnet:" + e.getMessage());
330 }
331 }
332 }
333