Example usage for org.apache.commons.net.telnet TelnetClient getOutputStream

List of usage examples for org.apache.commons.net.telnet TelnetClient getOutputStream

Introduction

In this page you can find the example usage for org.apache.commons.net.telnet TelnetClient getOutputStream.

Prototype

public OutputStream getOutputStream() 

Source Link

Document

Returns the telnet connection output stream.

Usage

From source file:org.crsh.telnet.term.AbstractTelnetTestCase.java

@Before
public final void setUp() throws Exception {

    int port = PORTS.getAndIncrement();

    ////w w  w .  j av a 2  s . co m
    IOHandler handler = new IOHandler();

    //
    SimplePluginDiscovery discovery = new SimplePluginDiscovery();
    discovery.add(new TelnetPlugin());
    discovery.add(handler);

    //
    ctx = new TestPluginLifeCycle(new TelnetPlugin(), handler);
    ctx.setProperty(TelnetPlugin.TELNET_PORT, port);

    //
    ctx.start();

    //
    TelnetClient client = new TelnetClient();
    for (int retry_count = 0; retry_count < CLIENT_CONNECT_RETRY_LIMIT; retry_count++) {
        try {
            client.connect("localhost", port);
            break;
        } catch (IOException e) {
            if (retry_count < CLIENT_CONNECT_RETRY_LIMIT) {
                Thread.sleep(CLIENT_CONNECT_RETRY_SLEEP);
            } else {
                throw e;
            }
        }
    }

    //
    this.out = client.getOutputStream();
    this.in = client.getInputStream();
    this.handler = handler;
    this.client = client;
    this.running = true;
}

From source file:org.mule.transport.telnet.TelnetClientWrapper.java

public TelnetClientWrapper(String host, int port, TelnetMessageDispatcher dispatcher, int waitTime)
        throws IOException {

    TelnetClient client = new TelnetClient();

    TelnetOptionHandler handler = new EchoOptionHandler(true, true, true, true);
    try {/*from   w w  w .j  a  v  a 2  s  .  c o  m*/
        client.addOptionHandler(handler);
    } catch (InvalidTelnetOptionException e) {
        dispatcher.exceptionThrown(e);
    }
    //TODO enable LINEMODE

    client.connect(host, port);

    if (!client.isConnected()) {
        throw new ConnectException("cannot connect : " + host + ":" + port);
    }

    setWaitTime(waitTime);
    this.responseTimeout = dispatcher.getEndpoint().getResponseTimeout();

    this.client = client;
    this.dispatcher = dispatcher;
    this.encoding = dispatcher.getEndpoint().getEncoding();
    exitStatusCommand = dispatcher.getConnector().getExitStatusCommand();

    reader = new BufferedInputStream(client.getInputStream(), 1024);

    writer = new BufferedOutputStream(client.getOutputStream(), 1024);
    client.setSoTimeout(responseTimeout + waitTime);
    client.setReaderThread(true);

    if (logger.isTraceEnabled())
        logger.trace(BeanUtils.describe(this));
}

From source file:org.openhab.binding.ddwrt.internal.DDWRTBinding.java

/**
 * Send line via Telnet to DD-WRT/*from   ww w  .  j  av  a 2  s. c  om*/
 *
 * @param client
 *            the telnet client
 * @param data
 *            the data to send
 */
private static void send(TelnetClient client, String data) {
    try {
        data += "\r\n";
        client.getOutputStream().write(data.getBytes());
        client.getOutputStream().flush();
    } catch (IOException e) {
        logger.warn("Error sending data", e);
    }
}

From source file:org.openremote.controller.protocol.telnet.TelnetCommand.java

/**
 * Write this string to the telnet session.
 * /*w w w  . j a v a2  s .c  om*/
 * @param the string to write
 * @param tc The instance of the TelnetClient
 */
private void sendString(String s, TelnetClient tc) throws Exception {
    OutputStream os = tc.getOutputStream();
    os.write((s + "\n").getBytes());
    logger.info("send: " + s);
    os.flush();
}

From source file:org.owasp.goatdroid.gui.emulator.Emulator.java

static public void sendLocation(String latitude, String longitude) throws SocketException, IOException {

    TelnetClient telnet = new TelnetClient();
    telnet.setDefaultPort(5554);//www  . j  a  va2  s  .  com
    telnet.connect("localhost");
    PrintStream outStream = new PrintStream(telnet.getOutputStream());
    // geo fix takes longitude/latitude, in that order
    outStream.println("geo fix " + longitude + " " + latitude);
    outStream.flush();
}

From source file:org.owasp.goatdroid.gui.emulator.Emulator.java

static public void sendSMSToEmulator(String phoneNumber, String message) throws SocketException, IOException {

    TelnetClient telnet = new TelnetClient();
    telnet.setDefaultPort(5554);/* ww  w . ja v a 2 s.  c o  m*/
    telnet.connect("localhost");
    PrintStream outStream = new PrintStream(telnet.getOutputStream());
    outStream.println("sms send " + phoneNumber + " " + message);
    outStream.flush();

}

From source file:org.owasp.goatdroid.gui.emulator.Emulator.java

static public void callEmulator(String phoneNumber) throws SocketException, IOException {

    TelnetClient telnet = new TelnetClient();
    telnet.setDefaultPort(5554);// w  ww .j a va2 s .co  m
    telnet.connect("localhost");
    PrintStream outStream = new PrintStream(telnet.getOutputStream());
    outStream.println("gsm call " + phoneNumber);
    outStream.flush();

}

From source file:org.xerela.net.sim.telnet.TelnetTest.java

/**
 * //from w ww  . j av  a 2s  .  c om
 * @throws Exception
 */
public void testTelnet() throws Exception {
    /*     Start:
     *   +------------+
     * ,-! Send Input !
     * ! +------------+
     * !     | 
     * ! +------------+
     * ! ! Read Input !------. no
     * ! +------------+       \
     * !yes   |                |
     * ! +------------+ no +------------+ yes +------+
     * `-! IsCorrect? !----! IsTooLong? !-----! FAIL !
     *   +------------+    +------------+     +------+
     * 
     */
    IpAddress local = IpAddress.getIpAddress(Util.getLocalHost(), null);

    TelnetClient client = new TelnetClient();
    client.connect(local.getRealAddress());

    RecordingLoader recordingLoader = RecordingLoader.getInstance();
    Configuration config = ConfigurationService.getInstance()
            .findConfigurationFile(ConfigurationService.DEFAULT_CONFIG);

    WorkingConfig wc = config.getDefaultOperationWorkingConfig();

    // create the operation manually first so that we can easily get the records
    RecordingOperation operation = (RecordingOperation) recordingLoader.createOperation(wc, local, local);
    Interaction[] interactions = operation.getInteractions();
    operation.tearDown();

    BufferedInputStream in = new BufferedInputStream(client.getInputStream());
    PrintStream out = new PrintStream(client.getOutputStream(), true);

    byte[] bbuf = new byte[2048];

    CharSequenceBuffer cbuf = new CharSequenceBuffer();
    for (int i = 0; i < interactions.length; i++) {
        Interaction currInteraction = interactions[i];

        String proto = currInteraction.getCliProtocol();
        if (!proto.equals("Telnet")) {
            /*
             * Because the recording might have other protocols we should stop when we encounter one.
             * The recording may not behave properly if we continue as we are.
             * If we got through at least 10 interactions then this test is probably still valid.
             */
            assertTrue(
                    "At least 10 telnet interction should have been handled.  Maybe this test should be run with another recording.",
                    i > 10);
            System.err.println(
                    "Continueing could disrupt the validity of this test.  This test will only support Telnet operations.");
            break;
        }

        // The timeout will be four times the expected time or 4 seconds, whichever is longer.
        Long interactionTime = currInteraction.getEndTime() - currInteraction.getStartTime();
        long time = Math.max((long) (interactionTime * wc.getRateMultiplier()) * 4, 4000);
        long start = System.currentTimeMillis();

        CharSequence input = currInteraction.getCliCommand();
        CharSequence response = currInteraction.getCliResponse();
        if (!input.equals("No input sent") && !input.equals("")) {
            out.println(input);
        }

        cbuf.reset();
        while (true) {
            int len = 0;
            if (in.available() <= 0) {
                try {
                    Thread.sleep(250);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (in.available() <= 0) {
                    if (System.currentTimeMillis() - start > time) {
                        // isTooLong
                        fail("Timeout reached waiting for response for interaction '"
                                + currInteraction.getCliCommand() + "'");
                    }
                    continue;
                }
            }
            len = in.read(bbuf);
            cbuf.write(bbuf, 0, len);

            // isCorrect?
            if (compare(cbuf, response)) {
                break;
            } else if (System.currentTimeMillis() - start > time) {
                // isTooLong
                fail("Timeout reached waiting for response for interaction '" + currInteraction.getCliCommand()
                        + "'");
            }
        }
    }
}