通过Java将命令发送到telnet时触发循环问题

时间:2019-02-22 10:04:43

标签: java telnet

我一直在构建工具,使我和我的团队能够在我们的测试套件上执行许多日常任务。我们有许多耕种控制器,使我们能够执行诸如存储打开和存储关闭之类的任务,并且有人要求我尝试做一些可以按计划远程执行的操作,这样我们的环境团队就不必搞乱了修复由于使用不当而造成的问题。

该程序的核心不是我自己的代码,因为我对telnet的概念还很陌生,有人为我做了艰苦的工作。

我可以发送命令,直到在打印流中出现某些提示之前,我的问题是出现在处理不同的屏幕提示上,这些提示对于任何一个控制器都可能出现或不出现(有些直到未启用,有些出现打印机问题-所有这些都由控制器上的错误消息和按F键退出或继续操作的F键处理。

我可以使用if循环捕获第一个潜在消息,但就我的一生而言,我不知道如何处理第二个潜在消息。

到目前为止,我已经将代码粘贴到了下面(对于任何明显的不良做法,我们深表歉意。我目前可以随意使用它,而无需任何铅支持,因此如果您愿意提供它,我会征求一些意见:))< / p>

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import org.apache.commons.net.telnet.TelnetClient;

public class Test {

    private TelnetClient telnet = new TelnetClient();
    private InputStream in;
    private PrintStream out;
    private String prompt = ">";
    public String command;
    public String uid = "22222222";
    public String upw = "19731973";
    public String storeOpen = "1";
    static int startStatus;
    static int msgStatus;
    static int enableStatus;
    static int printerStatus;
    int reportStatus;

    public Test(String server, String user, String password, String job) {
        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("Operator ID:");
            write(user);
            Thread.sleep(2000);
            readUntil("Password:");
            write(password);
            Thread.sleep(2000);

            // Advance to a prompt
            readUntil("Operator ID");
            write(uid);
            Thread.sleep(2000);
            readUntil("Password");
            write(upw);
            Thread.sleep(2000);
            readUntil("Store System Application");

            if (job.equalsIgnoreCase("open")) {
                Thread.sleep(2000);
                write(storeOpen);
                readUntil("1. Start of Day Procedures");
                Thread.sleep(2000);
                write(storeOpen);
                readUntil("1. Store Open");
                Thread.sleep(2000);
                write(storeOpen);
                Thread.sleep(2000);
                System.out.println("Store Opening Started");
                startStatus = 1;
            }
            if (readUntil("Store opening already performed - F7 to continue , F3 to quit.") != null) {
                //THIS IF WORKS
                Thread.sleep(2000);
                sendCommand("\033[18~");//F7
                // sendCommand("\033[13~");<-- should quit on this but I'm testing so I want to keep moving
                Thread.sleep(2000);
                System.out.println("STORE OPENING ALREADY PERFORMED, EXITING");
                //telnet.disconnect();
            }
            else if (readUntil("Store opening already performed - F7 to continue , F3 to quit.") == null) {
                System.out.println("Store Opening not already performed, continuing"); //THIS ELSE IF WORKS
            }
            if (readUntil("Some tills not enabled") != null) { //THIS IF FAILS TO TRIGGER
                sendCommand("\033[18~");
                System.out.println("Some tills failed to enable. Go check!");
            }
            else if (readUntil("Some tills not enabled") == null){//THIS IF FAILS TO TRIGGER
                System.out.println("All tills enabled, continuing");
            }
            if (readUntil("Status  : Printed report selection ") != null) {//THIS IF FAILS TO TRIGGER
                sendCommand("\033[13~");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

public static void main(String[] args) throws IOException, InterruptedException {
        // Creates a new rig
        Rigs newRig = new Rigs();
        System.out.println("Please enter the RIG you'd like to connect to:");
        BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
        String rig = sc.readLine();

        // Asks which task we want to perform
        if (newRig.rigs().containsKey(rig.toUpperCase())) {
            System.out.println("Please enter one of the following options: Open or Close");
            BufferedReader job = new BufferedReader(new InputStreamReader(System.in));
            String command = job.readLine();
            // Does the store opening task
            if (command.equalsIgnoreCase("open") || command.equalsIgnoreCase("close")) {
                String ip = newRig.rigs().get(rig.toUpperCase());
                Test telnet = new Test(ip, "111", "111", command);
                System.out.println("Store " + command + " complete for " + rig + ":" + ip);
                telnet.disconnect();
            }
        }
    }
}

0 个答案:

没有答案