import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; 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; public class TelnetBen implements Runnable, TelnetNotificationHandler { public static TelnetClient tc = null; public InputStream in; public PrintStream out; public Thread tThread; public static void main(String args[]) { TelnetBen iTelnet=new TelnetBen("10.1.3.78", 23); iTelnet.tThread = new Thread(iTelnet); iTelnet.tThread.start(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } iTelnet.write("me"); iTelnet.write("me"); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } iTelnet.writescript("d:/cmd.txt"); } public TelnetBen(String host, int port) { if (!intconnect(host, port)) { System.out.println("telnet失败"); return; } } public void writescript(String filename) { try { if (new File(filename).exists()) { FileReader f = new FileReader(filename); BufferedReader reader = new BufferedReader(f); String command = ""; while (command != null) { command = reader.readLine(); write(command); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void write(String command) { try { System.out.println("command:>>>>>>>>>" + command); out.println(command); out.flush(); } catch (Exception e) { e.printStackTrace(); } } public boolean intconnect(String host, 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(host, 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; } } 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); } /*************************************************************************** * Reader thread. Reads lines from the TelnetClient and echoes them on the * screen. **************************************************************************/ 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()); } } }