Forum Discussion

Lewis_Beard's avatar
2 years ago

Expect, cli, and garbage/prompt/command info

I have a custom datasource I'm working on just as a self-teaching thing, and I seem to understand it fine, I use groovy and ssh and expect to get a file system listing to generate (useless, educational) instances based on the files in the ssh user's login directory. And then the datapoint just some other stuff that doesnt matter for the purpose of this post. BUT my issue is that on the expect where I'm just generating a list of files to "fake" instances, I'm getting some garbage from the .before() call. I will list out an example of my instances, where you can see the first 2 lines contain garbage from the command I ran, and maybe part of the prompt or terminal characters. And then the rest is perfect. Then I'll post some code.

But I'm just wondering if this is common? Every time I've used expect and etc in groovy in LM I get the same problem.

Sample instances generated with the top 2 lines being "noise":

> [m /usr/bin/ls -1
[m
myfile1.txt
myfile2.txt
agent.txt
local.txt
misc
quality.txt
transfer.txt

As you can see, I'm getting the end of my prompt and the command I sent as one garbage line, and then a line with some sort of terminal character maybe, as garbage in my first 2 fake instances.
So I'm trying to figure out how to eliminate these without having to explicitly check for whatever [m is. But maybe I will have to.

Here is the code, my hope is that there is just something fundamental I can do, something simple, to NOT have the command and terminal stuff I'm sending be part of the results of .before().

Thanks for any insights!

CODE:

import com.santaba.agent.groovyapi.expect.Expect;
hostname = hostProps.get("system.hostname");
userid = hostProps.get("ssh.user");
passwd = hostProps.get("ssh.pass");
prompt = /hardcodedmyuser@myserver.blah.blah:~/ //this is the regex that identifies my prompt
cmd = "/usr/bin/ls -1\n"

conn = Expect.open(hostname, userid, passwd);
conn.expect(prompt);
ignore = conn.before();

conn.send(cmd);
conn.expect(prompt);
cmd_output = conn.before();

conn.send("exit\n");
conn.expectClose();

cmd_output.eachLine
{ line ->
    line.replaceAll("(?:\\n|\\r)", "");
    instance = line + "##" + line;
    println(instance)
}
  • Anonymous's avatar
    Anonymous
    On 12/27/2022 at 10:09 AM, Lewis Beard said:

    > [m /usr/bin/ls -1

    This part is expected, since .before() returns everything from the last time the prompt was detected. This includes any commands you send. So, you'll almost always need to exclude the first line from any .before() result.

    Did i understand right that "[m" is supposed to be part of your prompt? If so, I'd work on your prompt variable to make sure that it's recognized by expect as part of your prompt. That should clear things up. That said, i'm not sure if "[m" is coming across as two characters or some sort of escaped special character.

    On 12/27/2022 at 10:09 AM, Lewis Beard said:

    Here is the code, my hope is that there is just something fundamental I can do, something simple, to NOT have the command and terminal stuff I'm sending be part of the results of .before().

    The thing is that .before() grabs from the combined text stream built from both your input and also the various pipes provided by the host (which pipes is somehow configurable in your conn object). There may be a way of getting just the sdtout pipe from the host, but that's getting into depth of expect that I'm not familiar with. I've always just ignored the first line of the output knowing it contained my command.