Forum Discussion
Anonymous
5 years ago22 minutes ago, grantae said:Not sure what cli.expect() I should use for the line following cli.send("exit\n")
Once you've done what you need to do to log off the device a simple "cli.expectClose()" is all that's needed. At that point, you don't need to wait for anything else before closing the SSH connection. I added some comments to your code.
Something to understand: cli.before() grabs everything between the most recent cli.expect() and the previous cli.expect(). Since you had "cli.expect("[screen is terminating]");" before the cli.before(), it was trying to grab the data after it had already exited the buffer. Does that make sense? Your cli.before() needs to be right after you know the config has been completely received.
import com.santaba.agent.groovyapi.expect.Expect host = hostProps.get("system.hostname"); user = hostProps.get("config.user"); pass = hostProps.get("config.pass"); prompt = /.+\#\s/ //<---this may be problematic as it might match on the # and everything before it. You might need to be more specific. cli=Expect.open(host, user, pass); cli.expect(prompt); cli.send("terminal length 0\n"); cli.expect(prompt); cli.send("show running-config\n"); //cli.expect(/Current configuration.*\n/); //<--If the previous command ends at the prompt, then just expect the prompt. cli.expect(prompt) cli.send("exit\n"); //cli.expect("[screen is terminating]"); //You've already exited, no need to wait for anything before closing the connection. You probably wouldn't get it anyway. config=cli.before(); //println(cli.before()) should be just fine. However, it might include "show running-config" on the first line, which is technically not part of the config. config = config.replaceAll(/ntp clock-period \d+/,"ntp clock-period "); //<--- Not sure ntp clock period is different every time you show the config. This removes the difference so that LM doesn't alert you that the config has been changed. There are better ways to do this. Just create an exception in the configcheck. cli.expectClose(); println config;
Related Content
- 2 years ago
- 3 months ago
- 2 years ago