telnet client java,apache commons-net TelnetClient 例子

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值