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