read output text on calling spawn.send()

638 views Asked by At

I am new to ExpectJ Java programming. I downloaded jar's and able to do few send() and expect() methods. send() would fire a command on console and expect() would identify any prompt's so inputs can be provided. Expect only reads is there are prompts, and not other info. For example, if want to fire, spawn.send("ls") and get list of all file names and so certain action, is that possible?.

Is there way so I can read normal output of spawn.send("ls") for example, without expect which only captures prompts?

1

There are 1 answers

0
baneyu On

You can indeed capture the output stream: It is one of the methods of the ExpectJ.Spawn class I am also very new to Java, but I got the output, however, I am still struggling on getting the prompt recognized as I get extra control characters from Unix so do not trust what comes after the second System.out.println (the sh,expect part) the output works fine, just set it in a variable if you want, or if you use swing, send it to a textarea with a listener.

BTW! if you know how to do the expect without these bloody control characters, 1m,34 [001 and so on, I welcome your input

    import expectj.ExpectJ;
    import expectj.Spawn;

    public class myExpectinator {

        public myExpectinator(){
        }

        public void connect(){  
            try {
                ExpectJ ex = new ExpectJ(10);
                Spawn sh = ex.spawn("10.10.10.10", 22, "name", "password");

                System.out.println(sh.getCurrentStandardOutContents());
                System.out.println(sh.getCurrentStandardErrContents());
                sh.expect("~ $");
                sh.send("ps\n");
                System.out.println(sh.getCurrentStandardOutContents());
                //sh.expectClose();
                sh.stop();
            }
            catch(Exception e) {
                System.out.println(e);           
            }
        }
    }