Forum Discussion

Joe_Williams's avatar
Joe_Williams
Icon for Professor rankProfessor
6 years ago

Custom ConfigSource Issues

I am having to write a custom configsource for Steelhead Riverbeds. There isn't a way to sftp a file off so I am having to write an expect script in groovy. I am trying to cobble together something using the Cisco Generic RunningConfig as a stepping stone, but I just can't get it off the ground. If I run it via the ConfigSources page with Test Script I get this.

Quote

Failed to execute the script - null
java.nio.channels.ClosedByInterruptException
	at java.nio.channels.spi.AbstractInterruptibleChannel.end(AbstractInterruptibleChannel.java:202)
	at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:407)
	at sun.nio.ch.SourceChannelImpl.read(SourceChannelImpl.java:113)

 

If I run it via collector debug and !groovy I get

Quote

$ !groovy
Waiting... This may take up to 10 minutes.
Waiting... This may take up to 10 minutes.
End of stream reached, no match found
java.io.IOException: End of stream reached, no match found
    at com.santaba.agent.groovyapi.expect.expectj.Spawn._expect2(Spawn.java:595)
    at com.santaba.agent.groovyapi.expect.expectj.Spawn._expect2(Spawn.java:511)
    at com.santaba.agent.groovyapi.expect.expectj.Spawn.expect(Spawn.java:647)
    at com.santaba.agent.groovyapi.expect.expectj.Spawn.expect(Spawn.java:617)
    at com.santaba.agent.groovyapi.expect.Expect.expect(Expect.java:193)
    at com.santaba.agent.groovyapi.expect.Expect$expect$1.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at Script69.run(Script69.groovy:22)
    at com.santaba.agent.util.GroovyScriptShell.execute(GroovyScriptShell.java:127)
    at com.santaba.agent.util.GroovyScriptExecutor.execute(GroovyScriptExecutor.java:119)
    at com.santaba.agent.debugger.GroovyTask._executeInAgent(GroovyTask.java:186)
    at com.santaba.agent.debugger.GroovyTask._handle(GroovyTask.java:165)
    at com.santaba.agent.debugger.DebugTask.run(DebugTask.java:106)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

 

Any help would appreciated.

Oh yeah the formatting of the cli.

Quote

servername >
servername > enable
servername # show configuration
##
## Other IP configuration
##

 

And what I have so far.

import com.santaba.agent.groovyapi.expect.Expect;

host = hostProps.get("system.hostname");
user = hostProps.get("config.user");
pass = hostProps.get("config.pass");

// open an ssh connection and wait for the prompt
cli=Expect.open(host, user, pass);
cli.expect("#");

// ensure the page-by-page view doesn't foul the config output
cli.send("terminal length 0\n");
cli.expect("#");

// display the config
cli.send("show configuration\n");
cli.expect("##\n")

// logout from the device
cli.send("exit\n");
cli.expect("#exit");

// collect the output
config=cli.before();

// close the ssh connection handle then print the config
cli.expectClose();
println config;

And now what I have so far

  • And of course I kept plugging away and go it. It isn't pretty but it works.

    // import the logicmonitor expect helper class
    import com.santaba.agent.groovyapi.expect.Expect;
    
    // get the hostname and credentials from the device property table
    hostname = hostProps.get("system.hostname");
    userid = hostProps.get("config.user");
    passwd = hostProps.get("config.pass");
    
    // initiate an ssh connection to the host using the provided credentials
    ssh_connection = Expect.open(hostname, userid, passwd);
    
    // wait for the cli prompt, which indicates we've connected
    ssh_connection.expect("> ");
    
    // send a command to show the tomcat log file size, along with the newline [enter] character
    ssh_connection.send("enable\n");
    
    // wait for the cli prompt to return, which indicates the command has completed
    ssh_connection.expect("# ");
    
    ssh_connection.send("terminal length 0\n");
    ssh_connection.expect("# ");
    
    // capture all the text up to the expected string. this should look something like
    ssh_connection.send("show configuration\n");
    ssh_connection.expect(" # ");
    cmd_output = ssh_connection.before();
    
    // now that we've capture the data we care about lets exit from the cli
    ssh_connection.send("exit\n");
    
    // wait until the external process finishes then close the connection
    ssh_connection.expectClose();
    
    println cmd_output;