2012-09-28 19 views
0

我想在Java中使用Expect的等價物。 這是一個簡單的代碼:ExpectJ for Telnet

public class TelnetJExpect 
{ 
    @Test 
    public void telnetTest() 
    { 
     ExpectJ expectinator = new ExpectJ(5); 
     try 
     { 
      Spawn shell = expectinator.spawn("172.17.80.161", 23); 
      System.out.println("\nExit: " + shell.getCurrentStandardOutContents()); 
      shell.stop(); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
      assertTrue(false); 
     } 
    } 
} 

其結果是,我收到的垃圾:

ÿýÿý ÿý#ÿý' 

退出:

ÿýÿý ÿý#ÿý' 

不過,我得到連接,當我使用telnet從命令行。

請幫忙。

回答

0

試試這個

ExpectJ ex = new ExpectJ(50); 

    //org.apache.commons.net.telnet.TelnetClient 
    TelnetClient telnetClient = new TelnetClient(); 
    telnetClient.connect("192.168.56.101"); 

    /* 
    * add this constructor to TelnetSpawn 
    * 
    * public TelnetSpawn(InputStream in, OutputStream out) throws IOException { 
     this.m_socket = null; 
     m_fromSocket = in; 
     m_toSocket = out; 
    */ 
    TelnetSpawn telnetSpawn = new TelnetSpawn(telnetClient.getInputStream(), telnetClient.getOutputStream()); 
    Spawn spawn = ex.spawn(telnetSpawn); 
    try{ 
     //provide username and password here 
     spawn.interact(); 
    }catch(NullPointerException npe){ 
     //ignore, nasty expectj bug 
    }