org.apache.commons.net.telnet.TelnetClient Java Examples

The following examples show how to use org.apache.commons.net.telnet.TelnetClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TelnetExample.java    From ExpectIt with Apache License 2.0 7 votes vote down vote up
public static void main(String[] args) throws IOException {
    TelnetClient telnet = new TelnetClient();
    telnet.connect("rainmaker.wunderground.com");


    StringBuilder wholeBuffer = new StringBuilder();
    Expect expect = new ExpectBuilder()
            .withOutput(telnet.getOutputStream())
            .withInputs(telnet.getInputStream())
            .withEchoOutput(wholeBuffer)
            .withEchoInput(wholeBuffer)
            .withExceptionOnFailure()
            .build();

    expect.expect(contains("Press Return to continue"));
    expect.sendLine();
    expect.expect(contains("forecast city code--"));
    expect.sendLine("SAN");
    expect.expect(contains("X to exit:"));
    expect.sendLine();

    String response = wholeBuffer.toString();
    System.out.println(response);

    expect.close();
}
 
Example #2
Source File: SwitchTelnetClientSocket.java    From daq with Apache License 2.0 5 votes vote down vote up
public SwitchTelnetClientSocket(
    String remoteIpAddress, int remotePort, SwitchInterrogator interrogator, boolean debug) {
  this.remoteIpAddress = remoteIpAddress;
  this.remotePort = remotePort;
  this.interrogator = interrogator;
  this.debug = debug;
  telnetClient = new TelnetClient();
  addOptionHandlers();
}
 
Example #3
Source File: NetUtils.java    From Mycat-Balance with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param ip
 * @param port
 * @return
 */
public static boolean isConnectable(String ip, int port)
{
	try
	{
		TelnetClient client = new TelnetClient();
		client.connect(ip, port);
		return true;

	} catch (Exception e)
	{
		return false;
	}

}
 
Example #4
Source File: TelnetClientRule.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
protected void before() throws Throwable {
  client = new TelnetClient() {
    @Override
    protected void _connectAction_() throws IOException {
      super._connectAction_();
      socket = _socket_;
      directOutput = _output_;
      setKeepAlive(false);
    }
  };
}
 
Example #5
Source File: AbstractRefreshWebappMojo.java    From alfresco-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to ping/call the Alfresco Tomcat server and see if it is up (running)
 *
 * @return true if the Alfresco Tomcat server was reachable and up
 */
private boolean ping() {
    try {
        URL alfrescoTomcatUrl = buildFinalUrl("");
        TelnetClient telnetClient = new TelnetClient();
        telnetClient.setDefaultTimeout(500);
        telnetClient.connect(alfrescoTomcatUrl.getHost(), alfrescoTomcatUrl.getPort());
        return true;
    } catch (Exception ex) {
        return false;
    }
}
 
Example #6
Source File: TelnetTermServerTest.java    From vertx-shell with Apache License 2.0 5 votes vote down vote up
@Before
public void before() throws Exception {
  vertx = Vertx.vertx();
  client = new TelnetClient();
  client.addOptionHandler(new EchoOptionHandler(false, false, true, true));
  client.addOptionHandler(new SimpleOptionHandler(0, false, false, true, true));
  client.addOptionHandler(new TerminalTypeOptionHandler("xterm-color", false, false, true, false));
}
 
Example #7
Source File: TelnetClientRule.java    From aesh-readline with Apache License 2.0 5 votes vote down vote up
@Override
protected void before() throws Throwable {
  client = new TelnetClient() {
    @Override
    protected void _connectAction_() throws IOException {
      super._connectAction_();
      socket = _socket_;
      directOutput = _output_;
      setKeepAlive(false);
    }
  };
}
 
Example #8
Source File: DDWRTBinding.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
private void send(TelnetClient client, String data) {
    logger.trace("Sending data ({})...", data);
    try {
        data += "\r\n";
        client.getOutputStream().write(data.getBytes());
        client.getOutputStream().flush();
    } catch (IOException e) {
        logger.warn("Error sending data", e);
    }
}
 
Example #9
Source File: TelnetUtils.java    From sds with Apache License 2.0 5 votes vote down vote up
public static boolean telnet(String host,int port){
    TelnetClient telnetClient = new TelnetClient("vt200");
    //socket延迟时间:5000ms
    telnetClient.setDefaultTimeout(5000);
    try {
        telnetClient.connect(host,port);
        telnetClient.disconnect();
    } catch (IOException e) {
        return false;
    }
    return true;
}
 
Example #10
Source File: DDWRTBinding.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
private String receive(TelnetClient client) {
    StringBuffer strBuffer;
    try {
        strBuffer = new StringBuffer();

        byte[] buf = new byte[4096];
        int len = 0;

        Thread.sleep(750L);

        while ((len = client.getInputStream().read(buf)) != 0) {
            strBuffer.append(new String(buf, 0, len));

            Thread.sleep(750L);

            if (client.getInputStream().available() == 0) {
                break;
            }
        }

        return strBuffer.toString();

    } catch (Exception e) {
        logger.warn("Error receiving data", e);
    }

    return null;
}
 
Example #11
Source File: DDWRTBinding.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Send line via Telnet to DD-WRT
 *
 * @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);
    }
}
 
Example #12
Source File: TelnetClientRule.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
protected void before() throws Throwable {
  client = new TelnetClient() {
    @Override
    protected void _connectAction_() throws IOException {
      super._connectAction_();
      socket = _socket_;
      directOutput = _output_;
      setKeepAlive(false);
    }
  };
}
 
Example #13
Source File: SwitchTelnetClientSocket.java    From daq with Apache License 2.0 5 votes vote down vote up
/**
 * Telnet Client.
 * @param remoteIpAddress switch ip address
 * @param remotePort      telent port
 * @param interrogator    switch specific switch controller
 * @param debug For more verbose output.
 */
public SwitchTelnetClientSocket(
    String remoteIpAddress, int remotePort, SwitchController interrogator, boolean debug) {
  this.remoteIpAddress = remoteIpAddress;
  this.remotePort = remotePort;
  this.interrogator = interrogator;
  this.debug = debug;
  telnetClient = new TelnetClient();
  addOptionHandlers();
}
 
Example #14
Source File: BrokerCommand.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
@Path("telnet")
public Response telnet(@QueryParam("ip") String ip,@QueryParam("port") int port) throws Exception {
    TelnetClient telnetClient = new TelnetClient("vt200");  //指明Telnet终端类型,否则会返回来的数据中文会乱码
    telnetClient.setDefaultTimeout(5000); //socket延迟时间:5000ms
    try {
        telnetClient.connect(ip,port);  //建立一个连接,默认端口是23
    } catch (Exception e) {
        return Responses.error(500,"未存活");
    }
    return Responses.success();
}
 
Example #15
Source File: AbstractTelnet.java    From bistoury with GNU General Public License v3.0 5 votes vote down vote up
public AbstractTelnet(TelnetClient client) throws IOException {
    this.client = client;
    this.in = client.getInputStream();
    this.out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(), Charsets.UTF_8));
    this.version = readVersionUtilPrompt();
    this.writer = new SettedWriter();
    this.resultProcessor = getProcessor(writer);
}
 
Example #16
Source File: FritzboxBinding.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
private void send(TelnetClient client, String data) {
    logger.trace("Sending data ({})...", data);
    try {
        data += "\r\n";
        client.getOutputStream().write(data.getBytes());
        client.getOutputStream().flush();
    } catch (IOException e) {
        logger.warn("Error sending data", e);
    }
}
 
Example #17
Source File: FritzboxBinding.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
private String receive(TelnetClient client) {
    StringBuffer strBuffer;
    try {
        strBuffer = new StringBuffer();

        byte[] buf = new byte[4096];
        int len = 0;

        Thread.sleep(750L);

        while ((len = client.getInputStream().read(buf)) != 0) {
            strBuffer.append(new String(buf, 0, len));

            Thread.sleep(750L);

            if (client.getInputStream().available() == 0) {
                break;
            }
        }

        return strBuffer.toString();

    } catch (Exception e) {
        logger.warn("Error receiving data", e);
    }

    return null;
}
 
Example #18
Source File: FritzboxBinding.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Send line via Telnet to FritzBox
 *
 * @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);
    }
}
 
Example #19
Source File: AbstractTelnetStore.java    From bistoury with GNU General Public License v3.0 5 votes vote down vote up
private TelnetClient forceCreateClient(int pid) throws IOException {
    ArthasEntity arthasEntity = new ArthasEntity(pid);
    arthasEntity.start();
    TelnetClient client = createClient();
    this.arthasEntity = arthasEntity;
    return client;
}
 
Example #20
Source File: AbstractTelnetStore.java    From bistoury with GNU General Public License v3.0 5 votes vote down vote up
private TelnetClient createClient(int pid) throws IOException {
    if (arthasEntity == null || arthasEntity.getPid() != pid) {
        return forceCreateClient(pid);
    } else {
        return createClient();
    }
}
 
Example #21
Source File: AbstractTelnetStore.java    From bistoury with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Telnet tryGetTelnet() throws Exception {
    TelnetClient client = tryGetClient();
    if (client != null) {
        return createTelnet(client, CheckVersion.notCheck);
    }
    return null;
}
 
Example #22
Source File: AbstractTelnetStore.java    From bistoury with GNU General Public License v3.0 5 votes vote down vote up
private TelnetClient tryGetClient() {
    try {
        return createClient();
    } catch (Exception e) {
        return null;
    }
}
 
Example #23
Source File: AbstractTelnetStore.java    From bistoury with GNU General Public License v3.0 5 votes vote down vote up
private Telnet createTelnet(TelnetClient client, CheckVersion checkVersion) throws IOException {
    Telnet telnet = doCreateTelnet(client);
    String version = telnet.getVersion();
    if (checkVersion == CheckVersion.check && versionIllegal(version)) {
        return doWithIllegalVersion(telnet, version);
    } else {
        return telnet;
    }
}
 
Example #24
Source File: AbstractTelnetStore.java    From bistoury with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Telnet getTelnet(int pid) throws Exception {
    int illegalVersionCount = 0;
    while (illegalVersionCount < MAX_ILLEGAL_VERSION_COUNT) {
        try {
            TelnetClient client = doGetTelnet(pid);
            return createTelnet(client, CheckVersion.check);
        } catch (IllegalVersionException e) {
            sleepSec(3);
            illegalVersionCount++;
        }
    }
    logger.error("illegal version can not resolved");
    throw new IllegalVersionException();
}
 
Example #25
Source File: IPModemDriver.java    From smslib-v3 with Apache License 2.0 4 votes vote down vote up
TelnetClient getTc()
{
	return this.tc;
}
 
Example #26
Source File: FritzboxBinding.java    From openhab1-addons with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void run() {
    try {
        TelnetClient client = new TelnetClient();
        client.connect(ip);

        int state = 0;
        if (command == OnOffType.ON) {
            state = 1;
        }

        String cmdString = null;
        if (commandMap.containsKey(type)) {
            cmdString = commandMap.get(type) + " " + state;
        } else if (type.startsWith("tam")) {
            cmdString = "ctlmgr_ctl w tam settings/" + type.toUpperCase() + "/Active " + state;
        } else if (type.startsWith("cmd")) {
            int on = type.indexOf("ON=");
            int off = type.indexOf("OFF=");
            if (state == 0) {
                cmdString = type.substring(off + 4, on < off ? type.length() : on);
            } else {
                cmdString = type.substring(on + 3, off < on ? type.length() : off);
            }
            cmdString = cmdString.trim();
        }

        /*
         * This is a approach with receive/send in serial way. This
         * could be done via a sperate thread but for just sending one
         * command it is not necessary
         */
        if (username != null) {
            receive(client); // user:
            send(client, username);
        }
        receive(client); // password:
        send(client, password);
        receive(client); // welcome text
        send(client, cmdString);
        Thread.sleep(1000L); // response not needed - may be interesting
                             // for reading status
        client.disconnect();

    } catch (Exception e) {
        logger.warn("Error processing command", e);
    }
}
 
Example #27
Source File: DDWRTBinding.java    From openhab1-addons with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void run() {
    try {
        TelnetClient client = new TelnetClient();
        logger.trace("TelnetCommandThread IP ({})", ip);
        client.connect(ip);

        String state = null;
        if (command == OnOffType.ON) {
            state = "up";
        } else {
            state = "down";
        }

        String cmdString = null;
        if (commandMap.containsKey(type)) {
            if (type.startsWith(DDWRTBindingProvider.TYPE_ROUTER_TYPE)) {
                cmdString = commandMap.get(type);
            } else if (type.startsWith(DDWRTBindingProvider.TYPE_WLAN_24) && !interface_24.isEmpty()) {
                cmdString = commandMap.get(type) + " " + interface_24 + " " + state;
            } else if (type.startsWith(DDWRTBindingProvider.TYPE_WLAN_50) && !interface_50.isEmpty()) {
                cmdString = commandMap.get(type) + " " + interface_50 + " " + state;
            } else if (type.startsWith(DDWRTBindingProvider.TYPE_WLAN_GUEST) && !interface_guest.isEmpty()) {
                cmdString = commandMap.get(type) + " " + interface_guest + " " + state;
            }
        }
        if (cmdString == null) {
            return;
        }
        logger.trace("TelnetCommandThread command ({})", cmdString);

        /*
         * This is a approach with receive/send in serial way. This
         * could be done via a sperate thread but for just sending one
         * command it is not necessary
         */
        logger.trace("TelnetCommandThread Username ({})", username);
        if (username != null) {
            receive(client); // user:
            send(client, username);
        }
        receive(client); // password:
        send(client, password);
        receive(client); // welcome text
        send(client, cmdString);
        Thread.sleep(1000L); // response not needed - may be interesting
        // for reading status

        // There is a DD-WRT problem on restarting of virtual networks. So we have to restart the lan service.
        if (type.startsWith(DDWRTBindingProvider.TYPE_WLAN_GUEST) && !interface_guest.isEmpty()
                && command == OnOffType.ON) {
            cmdString = "stopservice lan && startservice lan";
            send(client, cmdString);
            Thread.sleep(25000L); // response not needed but time for restarting
        }

        logger.trace("TelnetCommandThread ok send");
        client.disconnect();

    } catch (Exception e) {
        logger.warn("Error processing command", e);
    }
}
 
Example #28
Source File: NormalTelnetStore.java    From bistoury with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Telnet doCreateTelnet(TelnetClient client) throws IOException {
    return new NormalTelnet(client);
}
 
Example #29
Source File: TelnetConnection.java    From Android-Telnet-Client with Apache License 2.0 4 votes vote down vote up
public TelnetClient getConnection(){
    return client;
}
 
Example #30
Source File: TelnetConnection.java    From Android-Telnet-Client with Apache License 2.0 4 votes vote down vote up
public TelnetConnection(String ip, int port) throws IOException{
	SERVER_IP = ip;
	SERVERPORT = port;
	client = new TelnetClient();
}