import java.io.*;
import org.apache.commons.net.telnet.*;
public class ShellClient {
private static final int TIMEOUT = 10 * 1000;
private final TelnetClient tc;
private final String ip;
private final String hostname;
private final byte[] buffer = new byte[2048];
private InputStream in;
private PrintStream out;
private PrintStream log;
public ShellClient(String ip, String hostname, PrintStream log) {
this.ip = ip;
this.hostname = hostname;
this.tc = createTelnetClient();
this.log = log;
}
private TelnetClient createTelnetClient() {
TelnetClient tc = new TelnetClient();
tc.setConnectTimeout(TIMEOUT);
TerminalTypeOptionHandler ttOpt = new TerminalTypeOptionHandler("VT100", false, false, true, false);
TelnetOptionHandler sizeOpt = new WindowSizeOptionHandler(256, 1024, false, false, true, false);
EchoOptionHandler echoOpt = new EchoOptionHandler(true, true, true, false);
SuppressGAOptionHandler gaOpt = new SuppressGAOptionHandler(true, true, true, true);
try {
tc.addOptionHandler(ttOpt);
tc.addOptionHandler(sizeOpt);
tc.addOptionHandler(echoOpt);
tc.addOptionHandler(gaOpt);
} catch (Throwable e) {
throw ThrowableUtils.unchecked(e);
}
return tc;
}
public void connect() {
try {
tc.connect(ip, 23);
in = tc.getInputStream();
out = new PrintStream(tc.getOutputStream());
} catch (Throwable e) {
throw ThrowableUtils.unchecked(e);
}
}
public void sendCommand(String command) {
try {
out.println(command);
out.flush();
} catch (Exception e) {
resetConnection();
throw ThrowableUtils.unchecked(e);
}
}
public int await(String... prompt) {
return await(null, TIMEOUT, prompt);
}
public int await(int timeout, String... prompt) {
return await(null, timeout, prompt);
}
public int await(StringBuilder sb, String... prompt) {
return await(sb, TIMEOUT, prompt);
}
public int await(StringBuilder sb, int timeout, String... prompts) {
int found = -1;
if (sb == null) {
sb = new StringBuilder();
}
try {
long ts = System.currentTimeMillis();
while (found < 0) {
String s = available();
if (s != null) {
sb.append(s);
for (int i = 0; i < prompts.length; i++) {
if (sb.toString().indexOf(prompts[i]) >= 0) {
found = i;
break;
}
}
} else {
if (System.currentTimeMillis() - ts >= timeout) {
throw new RuntimeException("Timeout for await " + ArrayUtils.toString(prompts) + ", get: \n" + sb.toString());
}
Thread.sleep(100);
}
}
while (true) {
String s = available();
if (s == null) break;
sb.append(s);
}
} catch (Throwable e) {
resetConnection();
throw ThrowableUtils.unchecked(e);
}
return found;
}
private String available() throws IOException {
if (in.available() > 0) {
int n = in.read(buffer);
String s = new String(buffer, 0, n);
log.print(s);
return s;
}
return null;
}
public void autologin(String username, String password) {
await("ogin:");
sendCommand(username);
await("assword:");
sendCommand(password);
await("#");
}
public void resetConnection() {
disconnect();
connect();
}
public void disconnect() {
try {
tc.disconnect();
} catch (Exception e) {
throw ThrowableUtils.unchecked(e);
}
}
public static void main(String[] args) {
ShellClient client = new ShellClient("192.168.0.1", System.out);
client.connect();
client.autologin("admin", "password");
//
client.sendCommand("ls -l");
client.await("#");
//
client.disconnect();
}
}