0

I have a script using expect to run some commands on machine over SSH. There is a part of script which issues a commands that causes multi-page output that I want to have saved to file.

How can I output only that part of expect script to file, and not have it also put key press prompt to log.

What I have is following

expect "something"
send "command that causes multi-page output with --More-- or (q)uit options\r"

#looping through output so that all pages are shown

log_file -noappend file.log #logging should start now

expect {
    -ex "--More--" {
        send "\r";
        exp_continue;
    }
} #I want to have output of only this part of expect script
# everything since last command before loop is issued to end of loop

log_file #Logging should stop here

send "other commands"

How can I get only that loop output to file, without "--More-- or (q)uit" added to log file.

Log also contains last send before loop, before log_file is set, and first prompt after loop, after log_file is removed, which I can easily strip out after so it is not a big problem.

My main problem is that log file contains --More-- or (q)uit^M ^M in it.

So log looks like this:

actual command output lines I want
...
--More-- or (q)uit^M                  ^M"actual command output line I want"
--More-- or (q)uit^M                  ^M"actual command output line I want"

How can I log the output of the loop to file without having More and quit options appended to log?

2

The pauses on the output of network devices are for the benefit of use of human operators.

When using automation tools, the best approach is using whatever native mechanism/language the device speaks to tell it to disable such pages.

If we are talking about Cisco, those settings are per session and not system wide, so they have to be issued at the beginning of every script that needs the setting.

When dealing with Cisco routers/switches I usually send the command:

terminal length 0

Or in expect parlance

send "terminal lenght 0\r"

PS. Beware the actual terminal length syntax can vary depending on the Cisco OS, for instance Cisco firewall hypervisors cards have a slightly different syntax. However that angle of the question is not on-topic here.

  • It's a ubiquiti switch, it also uses terminal length 0, that solved the problem with More being asked and appended to log, but I can't get show command output logged to file if I remove the loop, with loop I get command before loop and prompt after it logged together with output of show command, which I can trim easily. If I remove the loop, output of show is not logged while other commands are logged. If I put log_file before 'terminal length' it will show 'terminal length' and then next line is prompt after the show command has run, it does not show show command or its output in log. – rAlen Sep 7 '18 at 10:02
  • I would not following you, not sure whether my problem understanding the comment. I would suggest opening another question with the next issue and trying to be more clear. – Rui F Ribeiro Sep 7 '18 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.