【问题标题】:ExpectJ for Telnet用于 Telnet 的 ExpectJ
【发布时间】:2012-09-28 01:48:11
【问题描述】:

我想在 Java 中使用等价的 Expect。 这是一个简单的代码:

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 时,我得到了连接。

请帮忙。

【问题讨论】:

    标签: java telnet expectj


    【解决方案1】:

    试试这个

        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
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-17
      • 2011-07-22
      • 1970-01-01
      • 2011-06-11
      • 2021-02-21
      • 2011-08-06
      相关资源
      最近更新 更多