通过JAVA编写登录远程telnet机器,并且显示远程机器目录。d:\\command.txt"内容如下:telnetusername 
telnetpass 
ls 
 
代码如下:
import org.apache.commons.net.telnet.*;      
  
import java.io.*;      
import java.net.SocketException;      
import java.util.StringTokenizer;      
  
public class TmhTelnet implements Runnable, TelnetNotificationHandler {      
         
    private static TelnetClient tc = null;      
    private InputStream in;      
    private PrintStream out;   
    private Thread tThread;   
    private boolean connected=true;   
    public static void main(String args[]){   
        TmhTelnet telnet=new TmhTelnet ("192.168.200.3",23);   
        telnet.tThread = new Thread(telnet);      
        telnet.tThread.start();   
       
        telnet.write("******");      
        telnet.write("******");   
        telnet.writescript("d:\\command.txt");      
    }      
    public TmhTelnet(String host, int port) {      
        intconnect(host,port);   
    }      
     
    private 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();      
                    if(command!=null)write(command);      
                }      
            }      
        } catch (FileNotFoundException e) {      
            // TODO Auto-generated catch block      
            e.printStackTrace();      
        } catch (IOException e) {      
            // TODO Auto-generated catch block      
            e.printStackTrace();      
        }      
              
              
    }      
    private void write(String command) {      
        try{      
            System.out.println("command:>>>>>>>>>"+command);      
            out.println(command);   
            out.flush();   
            while(!tThread.getState().equals(Thread.State.BLOCKED));   
        }catch(Exception e){      
            e.printStackTrace();      
        }      
    }      
     
    private 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) {      
                // TODO Auto-generated catch block      
                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);      
    }      
     
   
    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());      
        }      
    }      

问题,执行的时候总是提示:
command:>>>>>>>>>******
login: Password: 
不理解为什么?

解决方案 »

  1.   

    以前写过一个,希望对你有帮助
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import org.apache.commons.net.telnet.TelnetClient;public class NetTelnet {
     // Telnet对象
     private TelnetClient telnet = new TelnetClient();
     private InputStream in;
     private PrintStream out;
     // 提示符。具体请telnet到AIX主机查看
     private String prompt = "#";
     // telnet端口
     private String port;
     // 用户
     private String user;
     // 密码
     private String password;
     // IP地址
     private String ip; public NetTelnet(String ip1,String user1,String password1,String port1) {  try {
       this.ip = ip1;
       this.password = password1;
       this.user = user1;
       this.port = port1;
       telnet.connect(ip, Integer.parseInt(port));
       in = telnet.getInputStream();
       out = new PrintStream(telnet.getOutputStream());
       // 登录
       readUntil("login: ");
       write(user);
       readUntil("Password: ");
       write(password);
       readUntil(prompt);
      } catch (Exception e) {
       e.printStackTrace();
      }
     }
     // 读取分析结果
    public String readUntil(String pattern) {
      try {
       char lastChar = pattern.charAt(pattern.length() - 1);
       StringBuffer sb = new StringBuffer();
       InputStreamReader  br = new InputStreamReader(in); 
       char ch = (char) br.read();
       while (true) {
        sb.append(ch);
        if (ch == lastChar) {
         if (sb.toString().endsWith(pattern)) {
          //System.out.println("xxxxxxxxxxxx"+sb.toString());
          return sb.toString();
         }
        }
        ch = (char)br.read();
       }
      } catch (Exception e) {
       e.printStackTrace();
      }
      return null;
     } public void write(String value) {
      try {
       out.println(value);
       out.flush();
      } catch (Exception e) {
       e.printStackTrace();
      }
     }
     // 向目标发送命令字符串
     public String sendCommand(String command) {
      try {
       write(command);
       return readUntil(prompt);
      } catch (Exception e) {
       e.printStackTrace();
      }
      return null;
     }
     // 关闭连接
     public void disconnect() {
      try {
       telnet.disconnect();
      } catch (Exception e) {
       e.printStackTrace();
      }
     } public static void main(String[] args) {
       System.out.println("开始执行telnet......");
       NetTelnet telnet = new NetTelnet("192.168.0.41","root","root123","23");
       System.out.println("开始发送hostname命令");   result = telnet.sendCommand("ls");
       System.out.println(result);
       telnet.disconnect();
     }}