Java Code Examples for org.apache.commons.net.telnet.TelnetClient#connect()
The following examples show how to use
org.apache.commons.net.telnet.TelnetClient#connect() .
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 | 6 votes |
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: BrokerCommand.java From joyqueue with Apache License 2.0 | 5 votes |
@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 3
Source File: TelnetUtils.java From sds with Apache License 2.0 | 5 votes |
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 4
Source File: NetUtils.java From Mycat-Balance with Apache License 2.0 | 5 votes |
/** * * @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 5
Source File: AbstractRefreshWebappMojo.java From alfresco-sdk with Apache License 2.0 | 5 votes |
/** * 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: AbstractTelnetStore.java From bistoury with GNU General Public License v3.0 | 4 votes |
private TelnetClient createClient() throws IOException { TelnetClient client = new TelnetClient(); client.setConnectTimeout(TelnetConstants.TELNET_CONNECT_TIMEOUT); client.connect(TelnetConstants.TELNET_CONNECTION_IP, TelnetConstants.TELNET_CONNECTION_PORT); return client; }
Example 7
Source File: DDWRTBinding.java From openhab1-addons with Eclipse Public License 2.0 | 4 votes |
@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 8
Source File: FritzboxBinding.java From openhab1-addons with Eclipse Public License 2.0 | 4 votes |
@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); } }