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)
}