使用java实现telnet-client工具分享
本文导语: telnet-client太费尽了,比ssh-client费尽的多,搞了一天,凑合能用,还得改。org.apache.commons.net.telnet.TelnetClien --使用了apache的commons-net包,commons-net-3.0.1-bin.zip。 代码如下:package org.sl.util;import org.apache.commons.net.telnet.TelnetClient;import j...
telnet-client太费尽了,比ssh-client费尽的多,搞了一天,凑合能用,还得改。
org.apache.commons.net.telnet.TelnetClien --使用了apache的commons-net包,commons-net-3.0.1-bin.zip。
package org.sl.util;
import org.apache.commons.net.telnet.TelnetClient;
import java.io.*;
import java.nio.ByteBuffer;
public class TelnetUtil {
String charset = null;
byte[] buff = new byte[2048];
TelnetClient telnetClient = new TelnetClient();
BufferedReader telnetReader = null;
BufferedWriter telnetWirter = null;
InputStream telnetIn = null;
OutputStream telnetOut = null;
public TelnetUtil() {
telnetClient = new TelnetClient();
}
/**
* 连接至服务器
* @param ip
* @param port
* @throws UnsupportedEncodingException
* @throws IOException
*/
public void connect(String ip, int port) throws UnsupportedEncodingException,IOException {
telnetClient.connect(ip,port);
setIOStream();
}
/**
* 连接至服务器
* @param ip
* @throws UnsupportedEncodingException
* @throws IOException
*/
public void connect(String ip) throws UnsupportedEncodingException,IOException {
telnetClient.connect(ip);
setIOStream();
}
void setIOStream() throws UnsupportedEncodingException {
telnetIn = telnetClient.getInputStream();
telnetOut = telnetClient.getOutputStream();
if(null==charset){
telnetReader = new BufferedReader(new InputStreamReader(telnetIn));
telnetWirter = new BufferedWriter(new OutputStreamWriter(telnetOut));
}else{
telnetReader = new BufferedReader(new InputStreamReader(telnetIn, charset));
telnetWirter = new BufferedWriter(new OutputStreamWriter(telnetOut, charset));
}
}
/**
* 登录
* @param user
* @param passwd
* @return 是否登录成功.
* @throws IOException
*/
public boolean login(String user,String passwd) throws IOException {
String read = readString();
for(int i=0; ; i++){
if(-1==read.indexOf("login")){
read = readString();
}else{
break;
}
}
writeText(user);
read = readString();
for(int i=0; ; i++){
if(-1==read.indexOf("Password")){
read = readString();
}else{
break;
}
}
writeText(passwd);
for(;;){
read = readString();
//System.out.println("last:"+read);
if(-1!=read.indexOf("Last")){
return true;
}else if(-1!=read.indexOf("incorrect")){
return false;
}
}
}
/**
* 这是一个测试方法,随便写。
* @throws IOException
*/
public void show() throws IOException {
// System.out.println(readString());
// System.out.println(readString());
// ByteBuffer tmp = ByteBuffer.allocate(1024);
// byte[] buff = new byte[1024];
// while(telnetIn.available()>0){
// int readLen = readBytes(buff,0,1024);
// tmp.put(buff,0,readLen);
// }
// System.out.println(new String(tmp.array()));
System.out.println("1 "+readString());
System.out.println("2 "+readString());
System.out.println("3 "+readString());
writeText("root");
System.out.println("4 " + readString());
writeText("123456");
System.out.println("5 "+readString());
// System.out.println("6 "+readString());
// System.out.println("7 "+readString());
}
public int readBytes(byte[] buff, int offset, int len) throws IOException {
return telnetIn.read(buff,offset,len);
}
/**
* 读取字符串
* 相当于readByte()转为字符串
* @return
* @throws IOException
*/
public String readString() throws IOException {
int readLen = readBytes(this.buff, 0, this.buff.length);
if(0