2

Hope you're all well!

I've recently begun putting together a little utility for a few our of network engineers to use. It's function is to retrieve circuit information which is then parsed and outputted in a nice format. Of course, part of this involves an SSH connection to the box required and running the command to generate the desired output.

Here's the script I've currently got. It works fine, runs the desired command, sends a few blank spaces to prompt the system to display more circuits, and then finishes off by exiting. All of this is being logged into a file with a name identical to the box's hostname. Great.

However my issue is apparent when I read the file produced and see that it includes a ton of data, including the command I ran and unnecessary stats provided on connection. I'm only looking for the logging to begin when I issue the command, and for it to cut off afterwards. As I'm not familiar with expect, I'd really appreciate any guidance.

PS. Apologies if I've done anything stupid; I'm pretty new to the language itself and the support out there isn't that great.

Thanks.

set ip [lindex $argv 0]
set hostname [lindex $argv 1]
set timeout 10
set user ""
set password ""

# Spawning the ssh session
spawn ssh $ip -l $user

# Awaiting prompt for password
expect "$user@$ip's password:"
sleep 1;
# Awaiting prompt
send "$password\n"
sleep 2
log_file -noappend $hostname;
send "terminal length 0\n"
sleep 1
send "show int desc\n"
sleep 5
send "exit\n"
interact
exit
Hugh
  • 193
  • 1
  • 1
  • 11
  • Add `expect` statement after each `send` command and use `sleep` if absolute necessary. By saying, it has all output, you meant the logging started from the start of ssh login itself? – Dinesh Nov 05 '14 at 14:16

1 Answers1

7

You can control the output by means of placing the log_file in your desired place.

When you want to start logging, you can add the line as (which you already have in your code)

log_file my_log_file.log

Whatever printed in console will be logged after these command execution in to the file named my_log_file.log. By default, if the file is already present in the location, it will be appended. If you add the flag -noappend, then it will overwrite the existing file.

log_file -noappend fresh.log

If you want to stop the logging, then simply give the log_file without any arguments as such

log_file

From this point, whatever output generated will not be saved.

For example, you are logging to the some switch and giving some credentials and executing some commands can be something like as follows,

spawn ssh some_ip
expect some_thing
#Login code here
log_file output.log; # Start logging
#some actions here
log_file; # stopping logging here.
# some more actions
# end

You are sending the spaces for multiple times. Instead, you can set the terminal length to 0 as follows,

send "term len 0"
expect "prompt"

This will avoid the overhead of the sending it multiple times. Also, in your case, the spaces will be sent to the switch very fast, since there is nothing to expect. If you are still interested to do it without 'term len 0' , then at least you can put the code in a loop, like as follows,

for { set i 0 } { $i < 10 } { incr i } { 
  send " "
  expect "something"
}

But, this way of doing is not advisable, since there is a possibility of need to send more than 10 spaces, then this will fail.

Dinesh
  • 16,014
  • 23
  • 80
  • 122
  • 1
    Sadly it would seem that your suggestion about creating the log file right before the command so it starts logging accordingly didn't work. Instead the script seems to log straight off of the bat, recording everything. Perhaps it's all stored and then dumped the minute I state a name for the log file? – Hugh Nov 05 '14 at 11:01
  • Wondering why. Can you paste the current code in your question and remove the old code ? – Dinesh Nov 05 '14 at 11:09
  • Hi Dinesh. I will do so in the next 20 minutes. – Hugh Nov 05 '14 at 12:23
  • I've updated it now Dinesh; let me know what you think. Script is still producing an output that includes text from before the show interface description command - which is undesired. – Hugh Nov 05 '14 at 12:46