| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| examples.TelnetClientExample |
|
|
| 1 | /* |
|
| 2 | * Copyright 2003-2004 The Apache Software Foundation |
|
| 3 | * |
|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 5 | * you may not use this file except in compliance with the License. |
|
| 6 | * You may obtain a copy of the License at |
|
| 7 | * |
|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 9 | * |
|
| 10 | * Unless required by applicable law or agreed to in writing, software |
|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 13 | * See the License for the specific language governing permissions and |
|
| 14 | * limitations under the License. |
|
| 15 | */ |
|
| 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 | /*** |
|
| 33 | * This is a simple example of use of TelnetClient. |
|
| 34 | * An external option handler (SimpleTelnetOptionHandler) is used. |
|
| 35 | * Initial configuration requested by TelnetClient will be: |
|
| 36 | * WILL ECHO, WILL SUPPRESS-GA, DO SUPPRESS-GA. |
|
| 37 | * VT100 terminal type will be subnegotiated. |
|
| 38 | * <p> |
|
| 39 | * Also, use of the sendAYT(), getLocalOptionState(), getRemoteOptionState() |
|
| 40 | * is demonstrated. |
|
| 41 | * When connected, type AYT to send an AYT command to the server and see |
|
| 42 | * the result. |
|
| 43 | * Type OPT to see a report of the state of the first 25 options. |
|
| 44 | * <p> |
|
| 45 | * @author Bruno D'Avanzo |
|
| 46 | ***/ |
|
| 47 | 0 | public class TelnetClientExample implements Runnable, TelnetNotificationHandler |
| 48 | { |
|
| 49 | 0 | static TelnetClient tc = null; |
| 50 | ||
| 51 | /*** |
|
| 52 | * Main for the TelnetClientExample. |
|
| 53 | ***/ |
|
| 54 | public static void main(String[] args) throws IOException |
|
| 55 | { |
|
| 56 | 0 | FileOutputStream fout = null; |
| 57 | ||
| 58 | 0 | if(args.length < 1) |
| 59 | { |
|
| 60 | 0 | System.err.println("Usage: TelnetClientExample1 <remote-ip> [<remote-port>]"); |
| 61 | 0 | System.exit(1); |
| 62 | } |
|
| 63 | ||
| 64 | 0 | String remoteip = args[0]; |
| 65 | ||
| 66 | int remoteport; |
|
| 67 | ||
| 68 | 0 | if (args.length > 1) |
| 69 | { |
|
| 70 | 0 | remoteport = (new Integer(args[1])).intValue(); |
| 71 | } |
|
| 72 | else |
|
| 73 | { |
|
| 74 | 0 | remoteport = 23; |
| 75 | } |
|
| 76 | ||
| 77 | try |
|
| 78 | { |
|
| 79 | 0 | fout = new FileOutputStream ("spy.log", true); |
| 80 | } |
|
| 81 | 0 | catch (Exception e) |
| 82 | { |
|
| 83 | 0 | System.err.println( |
| 84 | "Exception while opening the spy file: " |
|
| 85 | + e.getMessage()); |
|
| 86 | 0 | } |
| 87 | ||
| 88 | 0 | tc = new TelnetClient(); |
| 89 | ||
| 90 | 0 | TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler("VT100", false, false, true, false); |
| 91 | 0 | EchoOptionHandler echoopt = new EchoOptionHandler(true, false, class="keyword">true, false); |
| 92 | 0 | SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true, class="keyword">true, class="keyword">true, class="keyword">true); |
| 93 | ||
| 94 | try |
|
| 95 | { |
|
| 96 | 0 | tc.addOptionHandler(ttopt); |
| 97 | 0 | tc.addOptionHandler(echoopt); |
| 98 | 0 | tc.addOptionHandler(gaopt); |
| 99 | } |
|
| 100 | 0 | catch (InvalidTelnetOptionException e) |
| 101 | { |
|
| 102 | 0 | System.err.println("Error registering option handlers: " + e.getMessage()); |
| 103 | 0 | } |
| 104 | ||
| 105 | while (true) |
|
| 106 | { |
|
| 107 | 0 | boolean end_loop = false; |
| 108 | try |
|
| 109 | { |
|
| 110 | 0 | tc.connect(remoteip, remoteport); |
| 111 | ||
| 112 | ||
| 113 | 0 | Thread reader = new Thread (class="keyword">new TelnetClientExample()); |
| 114 | 0 | tc.registerNotifHandler(new TelnetClientExample()); |
| 115 | 0 | System.out.println("TelnetClientExample"); |
| 116 | 0 | System.out.println("Type AYT to send an AYT telnet command"); |
| 117 | 0 | System.out.println("Type OPT to print a report of status of options (0-24)"); |
| 118 | 0 | System.out.println("Type REGISTER to register a new SimpleOptionHandler"); |
| 119 | 0 | System.out.println("Type UNREGISTER to unregister an OptionHandler"); |
| 120 | 0 | System.out.println("Type SPY to register the spy (connect to port 3333 to spy)"); |
| 121 | 0 | System.out.println("Type UNSPY to stop spying the connection"); |
| 122 | ||
| 123 | 0 | reader.start(); |
| 124 | 0 | OutputStream outstr = tc.getOutputStream(); |
| 125 | ||
| 126 | 0 | byte[] buff = new byte[1024]; |
| 127 | 0 | int ret_read = 0; |
| 128 | ||
| 129 | do |
|
| 130 | { |
|
| 131 | try |
|
| 132 | { |
|
| 133 | 0 | ret_read = System.in.read(buff); |
| 134 | 0 | if(ret_read > 0) |
| 135 | { |
|
| 136 | 0 | if((new String(buff, 0, ret_read)).startsWith("AYT")) |
| 137 | { |
|
| 138 | try |
|
| 139 | { |
|
| 140 | 0 | System.out.println("Sending AYT"); |
| 141 | ||
| 142 | 0 | System.out.println("AYT response:" + tc.sendAYT(5000)); |
| 143 | } |
|
| 144 | 0 | catch (Exception e) |
| 145 | { |
|
| 146 | 0 | System.err.println("Exception waiting AYT response: " + e.getMessage()); |
| 147 | 0 | } |
| 148 | } |
|
| 149 | 0 | else if((new String(buff, 0, ret_read)).startsWith("OPT")) |
| 150 | { |
|
| 151 | 0 | System.out.println("Status of options:"); |
| 152 | 0 | for(int ii=0; ii<25; ii++) |
| 153 | 0 | System.out.println("Local Option " + ii + ":" + tc.getLocalOptionState(ii) + " Remote Option " + ii + ":" + tc.getRemoteOptionState(ii)); |
| 154 | } |
|
| 155 | 0 | else if((new String(buff, 0, ret_read)).startsWith("REGISTER")) |
| 156 | { |
|
| 157 | 0 | StringTokenizer st = new StringTokenizer(class="keyword">new String(buff)); |
| 158 | try |
|
| 159 | { |
|
| 160 | 0 | st.nextToken(); |
| 161 | 0 | int opcode = (new Integer(st.nextToken())).class="keyword">intValue(); |
| 162 | 0 | boolean initlocal = (new Boolean(st.nextToken())).class="keyword">booleanValue(); |
| 163 | 0 | boolean initremote = (new Boolean(st.nextToken())).class="keyword">booleanValue(); |
| 164 | 0 | boolean acceptlocal = (new Boolean(st.nextToken())).class="keyword">booleanValue(); |
| 165 | 0 | boolean acceptremote = (new Boolean(st.nextToken())).class="keyword">booleanValue(); |
| 166 | 0 | SimpleOptionHandler opthand = new SimpleOptionHandler(opcode, initlocal, initremote, |
| 167 | acceptlocal, acceptremote); |
|
| 168 | 0 | tc.addOptionHandler(opthand); |
| 169 | } |
|
| 170 | 0 | catch (Exception e) |
| 171 | { |
|
| 172 | 0 | if(e instanceof InvalidTelnetOptionException) |
| 173 | { |
|
| 174 | 0 | System.err.println("Error registering option: " + e.getMessage()); |
| 175 | } |
|
| 176 | else |
|
| 177 | { |
|
| 178 | 0 | System.err.println("Invalid REGISTER command."); |
| 179 | 0 | System.err.println("Use REGISTER optcode initlocal initremote acceptlocal acceptremote"); |
| 180 | 0 | System.err.println("(optcode is an integer.)"); |
| 181 | 0 | System.err.println("(initlocal, initremote, acceptlocal, acceptremote are boolean)"); |
| 182 | } |
|
| 183 | 0 | } |
| 184 | } |
|
| 185 | 0 | else if((new String(buff, 0, ret_read)).startsWith("UNREGISTER")) |
| 186 | { |
|
| 187 | 0 | StringTokenizer st = new StringTokenizer(class="keyword">new String(buff)); |
| 188 | try |
|
| 189 | { |
|
| 190 | 0 | st.nextToken(); |
| 191 | 0 | int opcode = (new Integer(st.nextToken())).class="keyword">intValue(); |
| 192 | 0 | tc.deleteOptionHandler(opcode); |
| 193 | } |
|
| 194 | 0 | catch (Exception e) |
| 195 | { |
|
| 196 | 0 | if(e instanceof InvalidTelnetOptionException) |
| 197 | { |
|
| 198 | 0 | System.err.println("Error unregistering option: " + e.getMessage()); |
| 199 | } |
|
| 200 | else |
|
| 201 | { |
|
| 202 | 0 | System.err.println("Invalid UNREGISTER command."); |
| 203 | 0 | System.err.println("Use UNREGISTER optcode"); |
| 204 | 0 | System.err.println("(optcode is an integer)"); |
| 205 | } |
|
| 206 | 0 | } |
| 207 | } |
|
| 208 | 0 | else if((new String(buff, 0, ret_read)).startsWith("SPY")) |
| 209 | { |
|
| 210 | try |
|
| 211 | { |
|
| 212 | 0 | tc.registerSpyStream(fout); |
| 213 | } |
|
| 214 | 0 | catch (Exception e) |
| 215 | { |
|
| 216 | 0 | System.err.println("Error registering the spy"); |
| 217 | 0 | } |
| 218 | } |
|
| 219 | 0 | else if((new String(buff, 0, ret_read)).startsWith("UNSPY")) |
| 220 | { |
|
| 221 | 0 | tc.stopSpyStream(); |
| 222 | } |
|
| 223 | else |
|
| 224 | { |
|
| 225 | try |
|
| 226 | { |
|
| 227 | 0 | outstr.write(buff, 0 , ret_read); |
| 228 | 0 | outstr.flush(); |
| 229 | } |
|
| 230 | 0 | catch (Exception e) |
| 231 | { |
|
| 232 | 0 | end_loop = true; |
| 233 | 0 | } |
| 234 | } |
|
| 235 | } |
|
| 236 | } |
|
| 237 | 0 | catch (Exception e) |
| 238 | { |
|
| 239 | 0 | System.err.println("Exception while reading keyboard:" + e.getMessage()); |
| 240 | 0 | end_loop = true; |
| 241 | 0 | } |
| 242 | } |
|
| 243 | 0 | while((ret_read > 0) && (end_loop == false)); |
| 244 | ||
| 245 | try |
|
| 246 | { |
|
| 247 | 0 | tc.disconnect(); |
| 248 | } |
|
| 249 | 0 | catch (Exception e) |
| 250 | { |
|
| 251 | 0 | System.err.println("Exception while connecting:" + e.getMessage()); |
| 252 | 0 | } |
| 253 | } |
|
| 254 | 0 | catch (Exception e) |
| 255 | { |
|
| 256 | 0 | System.err.println("Exception while connecting:" + e.getMessage()); |
| 257 | 0 | System.exit(1); |
| 258 | 0 | } |
| 259 | } |
|
| 260 | } |
|
| 261 | ||
| 262 | ||
| 263 | /*** |
|
| 264 | * Callback method called when TelnetClient receives an option |
|
| 265 | * negotiation command. |
|
| 266 | * <p> |
|
| 267 | * @param negotiation_code - type of negotiation command received |
|
| 268 | * (RECEIVED_DO, RECEIVED_DONT, RECEIVED_WILL, RECEIVED_WONT) |
|
| 269 | * <p> |
|
| 270 | * @param option_code - code of the option negotiated |
|
| 271 | * <p> |
|
| 272 | ***/ |
|
| 273 | public void receivedNegotiation(int negotiation_code, class="keyword">int option_code) |
|
| 274 | { |
|
| 275 | 0 | String command = null; |
| 276 | 0 | if(negotiation_code == TelnetNotclass="keyword">ificationHandler.RECEIVED_DO) |
| 277 | { |
|
| 278 | 0 | command = "DO"; |
| 279 | } |
|
| 280 | 0 | else if(negotiation_code == TelnetNotclass="keyword">ificationHandler.RECEIVED_DONT) |
| 281 | { |
|
| 282 | 0 | command = "DONT"; |
| 283 | } |
|
| 284 | 0 | else if(negotiation_code == TelnetNotclass="keyword">ificationHandler.RECEIVED_WILL) |
| 285 | { |
|
| 286 | 0 | command = "WILL"; |
| 287 | } |
|
| 288 | 0 | else if(negotiation_code == TelnetNotclass="keyword">ificationHandler.RECEIVED_WONT) |
| 289 | { |
|
| 290 | 0 | command = "WONT"; |
| 291 | } |
|
| 292 | 0 | System.out.println("Received " + command + " for option code " + option_code); |
| 293 | 0 | } |
| 294 | ||
| 295 | /*** |
|
| 296 | * Reader thread. |
|
| 297 | * Reads lines from the TelnetClient and echoes them |
|
| 298 | * on the screen. |
|
| 299 | ***/ |
|
| 300 | public void run() |
|
| 301 | { |
|
| 302 | 0 | InputStream instr = tc.getInputStream(); |
| 303 | ||
| 304 | try |
|
| 305 | { |
|
| 306 | 0 | byte[] buff = new byte[1024]; |
| 307 | 0 | int ret_read = 0; |
| 308 | ||
| 309 | do |
|
| 310 | { |
|
| 311 | 0 | ret_read = instr.read(buff); |
| 312 | 0 | if(ret_read > 0) |
| 313 | { |
|
| 314 | 0 | System.out.print(new String(buff, 0, ret_read)); |
| 315 | } |
|
| 316 | } |
|
| 317 | 0 | while (ret_read >= 0); |
| 318 | } |
|
| 319 | 0 | catch (Exception e) |
| 320 | { |
|
| 321 | 0 | System.err.println("Exception while reading socket:" + e.getMessage()); |
| 322 | 0 | } |
| 323 | ||
| 324 | try |
|
| 325 | { |
|
| 326 | 0 | tc.disconnect(); |
| 327 | } |
|
| 328 | 0 | catch (Exception e) |
| 329 | { |
|
| 330 | 0 | System.err.println("Exception while closing telnet:" + e.getMessage()); |
| 331 | 0 | } |
| 332 | 0 | } |
| 333 | } |
|
| 334 |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |