2015-10-19 86 views
1

我想創建基於Java的Telnet應用程序,它可以發送telnet命令到某個服務器並捕獲它的響應,只需在命令行中使用Telnet模擬器, 請提供一些教程來創建。基於Java的Telnet應用程序

我看到幾個教程它需要的Java telnet服務器和客戶機,但我的情況下,我只需要發送需要命令和捕獲的telnet輸出變量或寫入文本文件

謝謝。

+0

過寬。有十幾個或更多適用的RFC和幾個現存的實現。 – EJP

回答

1

看看Apache Commons Net TelnetClient。它可以很容易地提供你需要的行爲。您只需獲取TelnetClient實例,調用connect方法並通過Input-和OutputStreams與遠程服務器一起工作。 Here你可以找到一個官方的例子,它是如何使用的,但它有點冗餘,IMO。

下面是從here一個例子:

import org.apache.commons.net.telnet.TelnetClient; 

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

public class AutomatedTelnetClient { 
    private TelnetClient telnet = new TelnetClient(); 
    private InputStream in; 
    private PrintStream out; 
    private String prompt = "%"; 

    public AutomatedTelnetClient(String server, String user, String password) { 
     try { 
      // Connect to the specified server 
      telnet.connect(server, 23); 

      // Get input and output stream references 
      in = telnet.getInputStream(); 
      out = new PrintStream(telnet.getOutputStream()); 

      // Log the user on 
      readUntil("login: "); 
      write(user); 
      readUntil("Password: "); 
      write(password); 

      // Advance to a prompt 
      readUntil(prompt + " "); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void su(String password) { 
     try { 
      write("su"); 
      readUntil("Password: "); 
      write(password); 
      prompt = "#"; 
      readUntil(prompt + " "); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public String readUntil(String pattern) { 
     try { 
      char lastChar = pattern.charAt(pattern.length() - 1); 
      StringBuffer sb = new StringBuffer(); 
      boolean found = false; 
      char ch = (char) in.read(); 
      while (true) { 
       System.out.print(ch); 
       sb.append(ch); 
       if (ch == lastChar) { 
        if (sb.toString().endsWith(pattern)) { 
         return sb.toString(); 
        } 
       } 
       ch = (char) in.read(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    public void write(String value) { 
     try { 
      out.println(value); 
      out.flush(); 
      System.out.println(value); 
     } 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) { 
     try { 
      AutomatedTelnetClient telnet = new AutomatedTelnetClient(
        "myserver", "userId", "Password"); 
      System.out.println("Got Connection..."); 
      telnet.sendCommand("ps -ef "); 
      System.out.println("run command"); 
      telnet.sendCommand("ls "); 
      System.out.println("run command 2"); 
      telnet.disconnect(); 
      System.out.println("DONE"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

謝謝,它工作:) – Kavinda

相關問題