commons-net 的用法.

注意要点:参考官方的demo,而不要采用大多数网上的demo,访问交换机会在登录那块堵塞.所以要实现Runnable,如果要显示反馈的话实现TelnetNotificationHandler 接口.

package com.sobey.telnet.service;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;

import org.apache.commons.net.telnet.EchoOptionHandler;
import org.apache.commons.net.telnet.SuppressGAOptionHandler;
import org.apache.commons.net.telnet.TelnetClient;
import org.apache.commons.net.telnet.TelnetNotificationHandler;
import org.apache.commons.net.telnet.TerminalTypeOptionHandler;
import org.springframework.stereotype.Service;

@Service
public class TelnetService implements Runnable, TelnetNotificationHandler {

public TelnetService() {
super();
}

private static TelnetClient tc = null;
private InputStream in;
private PrintStream out;

@Override
public void receivedNegotiation(int negotiation_code, int option_code) {
String command = null;
if (negotiation_code == TelnetNotificationHandler.RECEIVED_DO) {
command = "DO";
} else if (negotiation_code == TelnetNotificationHandler.RECEIVED_DONT) {
command = "DONT";
} else if (negotiation_code == TelnetNotificationHandler.RECEIVED_WILL) {
command = "WILL";
} else if (negotiation_code == TelnetNotificationHandler.RECEIVED_WONT) {
command = "WONT";
}
System.out.println("Received " + command + " for option code " + option_code);
}

public void execCommand(String ip, String username, String password, String command) {
Thread t = new Thread(new TelnetService(ip, 23, username, password, command));
t.start();
}

@Override
public void run() {
InputStream instr = tc.getInputStream();
try {
byte[] buff = new byte[1024];
int ret_read = 0;
do {
ret_read = instr.read(buff);
if (ret_read > 0) {
System.out.print(new String(buff, 0, ret_read));
}
} while (ret_read >= 0);
} catch (Exception e) {
System.err.println("Exception while reading socket:" + e.getMessage());
}
try {
tc.disconnect();
} catch (Exception e) {
System.err.println("Exception while closing telnet:" + e.getMessage());
}
}

public TelnetService(String ip, int port, String username, String password, String command) {
if (intconnect(ip, port)) {
write(username);
write(password);
write(command);
}
}

private void write(String command) {
try {
out.println(command);
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}

private boolean intconnect(String ip, int port) {
try {
tc = new TelnetClient();
TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler("VT100", false, false, true, false);
EchoOptionHandler echoopt = new EchoOptionHandler(true, false, true, false);
SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true, true, true, true);

tc.addOptionHandler(ttopt);
tc.addOptionHandler(echoopt);
tc.addOptionHandler(gaopt);
tc.connect(ip, port);
in = tc.getInputStream();
out = new PrintStream(tc.getOutputStream());
return true;
} catch (Exception e) {
e.printStackTrace();
try {
tc.disconnect();
} catch (IOException e1) {
e1.printStackTrace();
}
return false;
}
}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值