用JAVA实现Telnet;采用TelnetProtocolHandler实现;但是批处理命令时会出现时序不一致的情况!请大侠们看看如果保证输入输出一致!
package com.zte.ums.xgw.telnet.control;import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.util.Vector;import java.awt.Dimension;import de.mud.telnet.ScriptHandler;
import de.mud.telnet.TelnetProtocolHandler;
public class TelnetWrapper extends TelnetProtocolHandler
{ public TelnetWrapper()
{
scriptHandler = new ScriptHandler();
port = 23;
script = new Vector();
prompt = null;
} public void connect(String host, int port) throws IOException
{
try
{
socket = new Socket(host, port);
in = socket.getInputStream();
out = socket.getOutputStream();
InputStreamReader isr = new InputStreamReader(in);
encoding = isr.getEncoding(); OutputStreamWriter osr = new OutputStreamWriter(out);
encoding = osr.getEncoding(); reset();
}
catch(Exception e)
{
System.err.println("TelnetWrapper: " + e);
disconnect();
throw(IOException)e;
}
} public void disconnect() throws IOException
{
if(socket != null)
{
socket.close();
}
} public void notifyEndOfRecord()
{
} public void login(String user, String pwd) throws IOException
{
waitfor("Username:");
send(user);
waitfor("Password:");
send(pwd);
} public void setPrompt(String prompt)
{
this.prompt = prompt;
} public String send(String cmd) throws IOException
{
write((cmd + "\n").getBytes());
if(prompt != null)
{
return waitfor(prompt);
}
else
{
return null;
}
} public String waitfor(String match) throws IOException
{
scriptHandler.setup(match);
byte b[] = new byte[256];
int n = 0;
String ret = "";
while(n >= 0)
{
n = read(b);
if(n > 0)
{
ret = ret + new String(b, 0, n);
if(scriptHandler.match(b, n))
{
return ret;
}
}
}
return null;
} public int read(byte b[]) throws IOException
{
int n = negotiate(b);
if(n > 0)
{
return n;
}
for(; n <= 0; n = negotiate(b))
{
do
{
n = negotiate(b);
if(n > 0)
{
return n;
}
} while(n == 0);
n = in.read(b);
// n = objectin.read(b);
if(n < 0)
{
return n;
}
inputfeed(b, n);
} return n;
} public void write(byte b[]) throws IOException
{
out.write(b);
} public void write(String s) throws IOException
{
if(encoding != null)
{
try
{
write(s.getBytes(encoding));
}
catch(UnsupportedEncodingException uee)
{
System.err.println("Error sending string: " + s + "\n\t Reason: UnsupportedEncodingException: "
+ encoding);
return;
}
}
else
{
write(s.getBytes());
}
} public String outStrFromByte(byte[] b,String endStr)
{
StringBuffer outStrBuff = new StringBuffer();
for(int i=0 ;i<b.length;i++)
{
if(outStrBuff.toString().endsWith(endStr))
{
return outStrBuff.toString();
}
if(b[i]==0)
{
return outStrBuff.toString();
}
outStrBuff.append((char)b[i]);
}
return outStrBuff.toString();
}

public String outStrFromByte(byte[] b)
{
StringBuffer outStrBuff = new StringBuffer();
for(int i=0 ;i<b.length;i++)
{
if(b[i]==0)
{
return outStrBuff.toString();
}
outStrBuff.append((char)b[i]);
}
return outStrBuff.toString();
}
public static void main( String[] args ) { 
try {  TelnetWrapper telnet = new TelnetWrapper(); 
telnet.connect("192.168.25.248",23);
byte[] b = new byte[256];
telnet.read(b);
String result = telnet.outStrFromByte(b,"Username:");
System.out.print(result);
// telnet.login("zte", "zte");
telnet.write("zte");
telnet.write("\r\n");
b = new byte[256];
telnet.read(b);
result = telnet.outStrFromByte(b,"Password:");
System.out.print(result); telnet.write("zte");
telnet.write("\r\n");
b = new byte[256];
telnet.read(b);
System.out.print(telnet.outStrFromByte(b)); telnet.write("?\r\n");
// telnet.write("\r\n");
b = new byte[256];
telnet.read(b);
System.out.print(telnet.outStrFromByte(b));// logger.info("发送命令结束"); 
telnet.disconnect(); 
}catch( Exception e ) { 
e.printStackTrace(); 

}  public String getTerminalType()
{
return "dumb";
} public Dimension getWindowSize()
{
return new Dimension(80, 24);
} public void setLocalEcho(boolean flag)
{
} protected ScriptHandler scriptHandler;
protected InputStream in;
protected OutputStream out; protected String encoding = null; public Socket socket;
protected String host;
protected int port;
protected Vector script;
private String prompt;
}