Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to disable echo when sending a terminal command using apache-commons-net TelnetClient
    primarykey
    data
    text
    <p>So, I have this class that uses the <a href="http://commons.apache.org/net/api-3.0.1/org/apache/commons/net/telnet/TelnetClient.html"><code>org.apache.commons.net.telnet.TelnetClient</code></a> class. It attempts to send commands and read the response.</p> <pre><code>public class AutomatedTelnetClient { private TelnetClient telnet = new TelnetClient(); private InputStream in; private PrintStream out; private String prompt = "$"; public AutomatedTelnetClient(String server, String user, String password) { try { EchoOptionHandler echoopt = new EchoOptionHandler(false, false, false, false); telnet.addOptionHandler(echoopt); // Connect to the specified server telnet.connect(server, 23); // Get input and output stream references in = telnet.getInputStream(); out = new PrintStream(telnet.getOutputStream()); // Log the user on readUntil("login: "); write(user); readUntil("Password: "); write(password); // Advance to a prompt readUntil(prompt + " "); } catch (Exception e) { e.printStackTrace(); } } public void su(String password) { try { write("su"); readUntil("Password: "); write(password); prompt = "#"; readUntil(prompt + " "); } catch (Exception e) { e.printStackTrace(); } } public String readUntil(String pattern) { try { char lastChar = pattern.charAt(pattern.length() - 1); StringBuffer sb = new StringBuffer(); boolean found = false; char ch = (char) in.read(); while (true) { System.out.print(ch); sb.append(ch); if (ch == lastChar) { if (sb.toString().endsWith(pattern)) { return sb.toString(); } } ch = (char) in.read(); } } catch (Exception e) { e.printStackTrace(); } return null; } public void write(String value) { try { out.println(value); out.flush(); System.out.println(value); } catch (Exception e) { e.printStackTrace(); } } public String sendCommand(String command) { try { write(command); return readUntil(prompt + " "); } catch (Exception e) { e.printStackTrace(); } return null; } public void disconnect() { try { telnet.disconnect(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { try { AutomatedTelnetClient telnet = new AutomatedTelnetClient( "...", "...", "..."); System.out.println("Got Connection..."); System.out.println("run command"); telnet.sendCommand("ls "); telnet.sendCommand("cd netConf"); telnet.sendCommand("ls "); telnet.sendCommand("cd lanSetup"); telnet.sendCommand("ls "); telnet.sendCommand("cd dhcpd"); telnet.sendCommand("show interface 2"); telnet.disconnect(); System.out.println("DONE"); } catch (Exception e) { e.printStackTrace(); } } } </code></pre> <p>Trouble is, when I send a command, the response is prefaced with an echo of sorts. For instance, when I send the <code>ls</code> command, this is the response I read</p> <pre><code>[mls .... </code></pre> <p>I've tried searching on <a href="http://mail-archives.apache.org/mod_mbox/commons-user/200807.mbox/%3C487BD4F0.10604@visible-assets.com%3E">how to disable echo</a>, but no one seems to have the answer. So, I've decided to ask this community. Does anyone know how to disable this echo?</p> <p><strong>EDIT</strong></p> <p>Looking at the source code of <a href="http://www.java2s.com/Open-Source/Java-Document/Net/Apache-commons-net-1.4.1/org/apache/commons/net/telnet/EchoOptionHandler.java.htm#EchoOptionHandler"><code>EchoOptionHandler</code></a> is confusing me. Why are the sub-negotiation methods only returning <code>null</code>?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload