如何在 telnet 会话期间迭代队列? [复制]

Posted

技术标签:

【中文标题】如何在 telnet 会话期间迭代队列? [复制]【英文标题】:How to iterate a queue during a telnet session? [duplicate] 【发布时间】:2020-09-02 01:51:19 【问题描述】:

在将 Tcl 脚本改编为 Java 并使用实现 Java expectexpectit API 后,我试图整理以下用法:

package expectit;

import java.util.logging.Logger;
import static net.sf.expectit.matcher.Matchers.contains;
import static net.sf.expectit.matcher.Matchers.eof;
import java.io.IOException;
import static java.lang.System.exit;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.TimeUnit;

public class Triple 

    private final static Logger log = Logger.getLogger(Triple.class.getName());
    private Queue q = new LinkedList();

    void telnet() throws IOException 

        q.add("nyc");
        q.add("lax");
        q.add("chi");

        Process process = Runtime.getRuntime().exec("telnet rainmaker.wunderground.com");

        StringBuilder wholeBuffer = new StringBuilder();
        net.sf.expectit.Expect expect = new net.sf.expectit.ExpectBuilder()
                .withOutput(process.getOutputStream())
                .withInputs(process.getInputStream())
                .withEchoOutput(wholeBuffer)
                .withEchoInput(wholeBuffer)
                .withExceptionOnFailure()
                .build();

        expect.expect(contains("Press Return to continue"));
        expect.sendLine();
        expect.expect(contains("forecast city code--"));
        expect.sendLine(q.remove().toString());

        ////////////////
        expect.expect(contains("Press Return to continue"));
        expect.sendLine("m");

        expect.expect(contains("Selection:"));
        expect.sendLine("1");

        expect.expect(contains("Enter 3-letter city code:"));
        expect.sendLine(q.remove().toString());

        /////
        expect.expect(contains("Press Return to continue"));
        expect.sendLine("m");

        expect.expect(contains("Selection:"));
        expect.sendLine("1");

        expect.expect(contains("Enter 3-letter city code:"));
        expect.sendLine(q.remove().toString());

        /////
        expect.expect(contains("Press Return to continue"));
        expect.sendLine("x");

        String response = wholeBuffer.toString();
        System.out.println(response);

        expect.close();

    


显然,queue 中的城市被删除了三个“部分”。

对于这个例子,queue 只是三个城市。

但如果有 99 个呢?

【问题讨论】:

【参考方案1】:

当然对替代解决方案感兴趣:

package expectit;

import java.util.logging.Logger;
import static net.sf.expectit.matcher.Matchers.contains;
import static net.sf.expectit.matcher.Matchers.eof;
import java.io.IOException;
import static java.lang.System.exit;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.TimeUnit;

public class Triple 

    private final static Logger log = Logger.getLogger(Triple.class.getName());
    private Queue cities = new LinkedList();

    public Triple() 
        cities.add("nyc");
        cities.add("lax");
        cities.add("chi");
    

    private void foo(net.sf.expectit.Expect expect, String city) throws IOException 
        expect.expect(contains("Press Return to continue"));
        expect.sendLine("m");

        expect.expect(contains("Selection:"));
        expect.sendLine("1");

        expect.expect(contains("Enter 3-letter city code:"));
        expect.sendLine(city.toUpperCase());
    

    void telnet() throws IOException 

        Process process = Runtime.getRuntime().exec("telnet rainmaker.wunderground.com");

        StringBuilder wholeBuffer = new StringBuilder();
        net.sf.expectit.Expect expect = new net.sf.expectit.ExpectBuilder()
                .withOutput(process.getOutputStream())
                .withInputs(process.getInputStream())
                .withEchoOutput(wholeBuffer)
                .withEchoInput(wholeBuffer)
                .withExceptionOnFailure()
                .build();

        expect.expect(contains("Press Return to continue"));
        expect.sendLine();
        expect.expect(contains("forecast city code--"));
        expect.sendLine(cities.remove().toString());


        String city = null;
        Iterator<String> iterator = cities.iterator();
        while (iterator.hasNext())   
            city = iterator.next().toString();
            foo(expect, city);
            foo(expect, city);
        

        /////
        expect.expect(contains("Press Return to continue"));
        expect.sendLine("x");

        String response = wholeBuffer.toString();
        System.out.println(response);

        expect.close();

    


只是传递expect 似乎有点尴尬。

可能expect 和城市的Queue 都应该是实例变量,或者不是——我有不同的范围...

【讨论】:

以上是关于如何在 telnet 会话期间迭代队列? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

如何在“每次”迭代期间附加列表时保持列表的长度不变

如何检查 Net::Telnet 会话是不是仍在 Perl 中?

如何在 C# 程序中打开 Telnet 会话并以编程方式发送命令和接收响应?

在列表迭代期间从 java.util.List 中删除元素时引发 ConcurrentModificationException? [复制]

命令行下 telnet 结果如何输出

如何使用 Expect 自动化 telnet 会话?