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

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

Introduction

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

Prototype

public TelnetClient() 

Source Link

Document

Default TelnetClient constructor, sets terminal-type VT100 .

Usage

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

@Before
public final void setUp() throws Exception {

    int port = PORTS.getAndIncrement();

    ////from  w  ww. j a  v a  2 s . com
    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.eclipse.linuxtools.sequoyah.device.network.telnet.TelnetWrapper.java

/**
 * Constructor// w  w  w.  jav  a 2 s. co  m
 */
public TelnetWrapper() {
    if (null == this.client) {
        this.client = new TelnetClient();
        try {
            this.client.addOptionHandler(new TerminalTypeOptionHandler("VT100", false, false, true, false)); //$NON-NLS-1$
            this.client.addOptionHandler(new SuppressGAOptionHandler(true, true, true, true));
            this.client.addOptionHandler(new EchoOptionHandler(false, false, false, false));
        } catch (InvalidTelnetOptionException itoe) {
        }
        // logger = LinuxToolsPlugin.getLogger();
    }
}

From source file:org.eclipse.osee.ote.core.test.shells.TelnetShell.java

public TelnetShell(String ipAddress, int port, boolean start) throws SocketException, IOException {
    telnet = new TelnetClient();
    telnet.registerNotifHandler(this);
    SuppressGAOptionHandler sgaOpt = new SuppressGAOptionHandler(true, true, true, true);
    TerminalTypeOptionHandler ttOpt = new TerminalTypeOptionHandler("VT100", false, false, true, false);
    EchoOptionHandler eOPt = new EchoOptionHandler(true, false, true, false);
    try {//from   w ww .j  a  va  2s .  co m
        telnet.addOptionHandler(ttOpt);
        telnet.addOptionHandler(sgaOpt);
        telnet.addOptionHandler(eOPt);
    } catch (InvalidTelnetOptionException e) {
        throw new IllegalStateException("invalid telnet options", e);
    }
    telnet.connect(ipAddress, port);
    try {
        Thread.sleep(1500);
    } catch (InterruptedException ex) {
        ex.printStackTrace();
    }

    /*
     * try { if (!telnet.sendAYT(5000)) { throw new SocketException("server appears to be in use"); } } catch
     * (IllegalArgumentException ex) { ex.printStackTrace(); } catch (InterruptedException ex) { ex.printStackTrace();
     * }
     */
    //printOptionStates();
    in = telnet.getInputStream();
    out = telnet.getOutputStream();
    if (start) {
        inputBuffer = new InputBufferThread(in);
        inputBuffer.start();

    }
}

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 {//  w w w.j av  a 2s.  c om
        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.mule.transport.telnet.TelnetMessageDispatcher.java

@Override
public RetryContext validateConnection(RetryContext retryContext) {

    try {// w ww .  j  a va2  s  .co m
        String host = getConnector().getHost();
        int port = getConnector().getPort();
        TelnetClient client = new TelnetClient();
        client.connect(host, port);
        boolean b = client.sendAYT(2000);

        if (b) {
            retryContext.setOk();
        } else {
            retryContext.setFailed(new IOException("sendAYT is false."));
        }
    } catch (Exception e) {
        retryContext.setFailed(e);
    }
    return retryContext;
}

From source file:org.opendaylight.snmp4sdn.internal.ExpectHandler.java

public ExpectHandler(String sw_ipAddr, String username_prompt, String password_prompt, String username,
        String password) throws Exception {
    if (isDummy)/* w  w w  .ja  v a  2  s.  c  om*/
        return;
    this.sw_ipAddr = new String(sw_ipAddr);
    this.username_prompt = new String(username_prompt);
    this.password_prompt = new String(password_prompt);
    this.username = new String(username);
    this.password = new String(password);
    System.out.println("NexusCLIHandler() sw_ipaddr:" + sw_ipAddr);
    client = new TelnetClient();
    //client.connect(sw_ipAddr);
    //read();
    //write(username.getBytes());
    //client.disconnect();
    TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler("VT100", false, false, true, true);
    EchoOptionHandler echoopt = new EchoOptionHandler(true, false, true, false);
    SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(false, false, false, false);
    client.addOptionHandler(ttopt);
    client.addOptionHandler(echoopt);
    client.addOptionHandler(gaopt);

    client.connect(sw_ipAddr, 23);
    //System.out.println("after login"); 
    //writer = new OutputStreamWriter(os); 
    out = new PrintStream(client.getOutputStream());
    telnetLogin();
    InputStream is = new FromNetASCIIInputStream(client.getInputStream()); // null until client connected
    //OutputStream os = new ToNetASCIIOutputStream( client.getOutputStream() );
    reader = new InputStreamReader(is);

    //executeNexus("show vlan"); 
    //sendCommand("show vlan");
    //disconnect();
}

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

@Override
protected void execute() {

    logger.trace("execute");
    if (password == null) {
        return;/*from ww  w.j  ava 2s  .c o  m*/
    } else if (StringUtils.isBlank(password)) {
        logger.error("Password mustn't be empty!");
        return;
    }

    try {
        TelnetClient client = null;

        for (DDWRTBindingProvider provider : providers) {
            for (String item : provider.getItemNames()) {
                String query = null;

                String type = provider.getType(item);
                if (queryMap.containsKey(type)) {
                    if (type.startsWith(DDWRTBindingProvider.TYPE_ROUTER_TYPE)) {
                        query = queryMap.get(type);
                    } else if (type.startsWith(DDWRTBindingProvider.TYPE_WLAN_24) && !interface_24.isEmpty()) {
                        query = queryMap.get(type) + " " + interface_24 + " | grep UP";
                    } else if (type.startsWith(DDWRTBindingProvider.TYPE_WLAN_50) && !interface_50.isEmpty()) {
                        query = queryMap.get(type) + " " + interface_50 + " | grep UP";
                    } else if (type.startsWith(DDWRTBindingProvider.TYPE_WLAN_GUEST)
                            && !interface_guest.isEmpty()) {
                        query = queryMap.get(type) + " " + interface_guest + " | grep UP";
                    }
                } else {
                    continue;
                }
                if (query == null) {
                    continue;
                }
                logger.trace("execute query ({})  ({}) ({})", query, ip, username);
                if (client == null) {
                    client = new TelnetClient();
                    client.connect(ip);
                    if (username != null) {
                        receive(client);
                        send(client, username);
                    }
                    receive(client);
                    send(client, password);
                    receive(client);
                }

                send(client, query);

                String answer = receive(client);
                String[] lines = answer.split("\r\n");

                if (lines.length >= 2) {
                    answer = lines[1].trim();
                }

                Class<? extends Item> itemType = provider.getItemType(item);

                State state = null;

                if (itemType.isAssignableFrom(SwitchItem.class)) {
                    if (lines.length > 2) {
                        if (lines[1].contains("UP")) {
                            state = OnOffType.ON;
                        } else {
                            state = OnOffType.OFF;
                        }
                    } else {
                        state = OnOffType.OFF;
                    }
                } else if (itemType.isAssignableFrom(NumberItem.class)) {
                    state = new DecimalType(answer);
                } else if (itemType.isAssignableFrom(StringItem.class)) {
                    state = new StringType(answer);
                }

                if (state != null) {
                    eventPublisher.postUpdate(item, state);
                }

            }
        }
        if (client != null) {
            client.disconnect();
        }
    } catch (Exception e) {
        logger.warn("Could not get item state ", e);
    }

}

From source file:org.openhab.binding.denon.internal.DenonListener.java

public DenonListener(DenonConnectionProperties connection, DenonUpdateReceivedCallback callback) {
    logger.debug("Denon listener created");
    this.tc = new TelnetClient();
    this.connection = connection;
    this.callback = callback;
}

From source file:org.openhab.binding.fritzbox.internal.FritzboxBinding.java

@Override
protected void execute() {

    if (password == null)
        return;// w ww.  j  a va2 s .  c o m
    else if (password.trim().isEmpty())
        return;

    try {
        TelnetClient client = null;

        for (FritzboxBindingProvider provider : providers) {
            for (String item : provider.getItemNames()) {
                String query = null;

                String type = provider.getType(item);
                if (queryMap.containsKey(type)) {
                    query = queryMap.get(type);
                } else if (type.startsWith("tam")) {
                    query = "ctlmgr_ctl r tam settings/" + type.toUpperCase() + "/Active";
                } else if (type.startsWith("query")) {
                    query = type.substring(type.indexOf(":") + 1).trim();
                } else
                    continue;

                if (client == null) {
                    client = new TelnetClient();
                    client.connect(ip);
                    if (username != null) {
                        receive(client);
                        send(client, username);
                    }
                    receive(client);
                    send(client, password);
                    receive(client);
                }

                send(client, query);

                String answer = receive(client);
                String[] lines = answer.split("\r\n");

                if (lines.length >= 2) {
                    answer = lines[1].trim();
                }

                Class<? extends Item> itemType = provider.getItemType(item);

                org.openhab.core.types.State state = null;

                if (itemType.isAssignableFrom(SwitchItem.class)) {
                    if (answer.equals("1"))
                        state = OnOffType.ON;
                    else
                        state = OnOffType.OFF;
                } else if (itemType.isAssignableFrom(NumberItem.class)) {
                    state = new DecimalType(answer);
                } else if (itemType.isAssignableFrom(StringItem.class)) {
                    state = new StringType(answer);
                }

                if (state != null)
                    eventPublisher.postUpdate(item, state);

            }
        }
        if (client != null)
            client.disconnect();
    } catch (Exception e) {
        logger.warn("Could not get item state ", e);
    }

}

From source file:org.openhab.binding.froeling.internal.net.TelnetSession.java

public TelnetSession() {
    this.telnetClient = new TelnetClient();
}