Save output to file - inside a script ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Save output to file - inside a script ?
# 1  
Old 07-25-2012
Save output to file - inside a script ?

I'm using the following script to check cisco router health and I'd like to save output to a file,

Code:
vty_runcmd.sh > /check/check-cisco-health

script works and output is saved to a file. However using it in crontab file is created but output is not printed inside it. In crontab,

Code:
*/5 * * * * /usr/local/sbin/vty_runcmd.sh > /check/check-cisco-health

Better would be to print the output -red characters- (output comes from reading cisco commands stored into a file) to a file directly from inside the script, can someone help me ?

here it is the script, I think the print should be placed in the red characters side of the script,

Code:
#!/usr/bin/expect --
##########################################################################
# vty_runcmd.exp
# 
#    This is an expect script that will connect to a device using ssh or
#    telnet, then run the specified command.  The output will either be 
#    written to an output file or printed to stdout.
#
# Created by Tim Evens (tim@evensweb.com), 5/2009
# Corrected by Sergio Zavala (sergio.zavala at sidetec.com.mx), 12/2010
#
# Copyright Tim Evens, 2009
#
# HISTORY:
#   Tim Evens	5/21/09	Initial program created
#   Sergio Zavala	12/26/10 Connection return code corrected 
#   Tim Evens	1/05/10	Connection validated
##########################################################################

#++++++++++++++++++++++++++
# Global vars
#++++++++++++++++++++++++++
set timeout 30
# below matches prompts such as "router#", "router>", "router$"
set prompt "\>\ *$|#\ *$|\\$\ *$"


#-----------------------------------------------
# Connect(method, prompt, host, user, password)
#    This function will connect to a host using telnet or ssh.
# 
# RETURNS:
#    zero if successful
#    1 = timeout or invalid hostname or method
#    2 = invalid login
#    3 = timeout waiting for login
#    4 = connection failed to host during expect wait
#    9 = unknown error
#-----------------------------------------------
proc Connect {method host usr pw} {
   set rval 0
   set usr_chk 0
   set pw_chk 0
   set max_checks 4
   global spawn_id
   global timeout
   global prompt
  
   puts "Connecting using $method to $host as user $usr"
   
   # see if we are using ssh
   if { [string compare $method "ssh"] == 0 } {
      set host "$usr@$host"
   }
   
   # Run command and get connected
   set id [spawn $method $host]
   if { $id <= 0 } {
      puts "ERROR: Failed to connect to host\n"
      set rval 1
      
   } else {
      puts "Using Process ID: $id"
   }
   
   # Start the expect/send process to login
   expect {
   
      # Below handles the username prompt
      -nocase -re "name:|^login:" {
         send "$usr\r"
         incr usr_chk;
         
         # continue with expect loop as long as we haven't hit this too many times         
         if { $usr_chk < $max_checks } {
            exp_continue
         } else {
            set rval 2
            puts "ERROR: Login retry failed.  Invalid login username"
         }   

       # Below handles the password prompt
       } -nocase -re "word:" {
         send "$pw\r"
         incr pw_chk;
         
         # continue with expect loop as long as we haven't hit this too many times
         if { $pw_chk < $max_checks } {
            exp_continue        
         } else {
            set rval 2
            puts "ERROR: Login retry failed.  Invalid login password"
         }   
         
      # Below handles the yes/no prompt when SSH first connects to a host   
      } -nocase -re "\(yes/no\)" {
         send "yes\r"
         exp_continue
      
      # Below handles the normal prompt to detect when logged in
      } -nocase -re "$prompt" {
         puts "\nSUCCESS: Logged in and ready to send commands\n"

      # Below is for expect timeout waiting for a                                 
      } timeout {
         puts "ERROR: Connection timeout waiting for login prompt"
         set rval 3
      
      # Below is for when the connect is closed before finishing   
      } eof {
         puts "ERROR: Connection to host failed: $expect_out(buffer)"
         set rval 4
      }
   }
     
   # return with error code
   return $rval
} 
# End of Connect ()

#-----------------------------------------------
# Usage()
#     This function will print the usage
#-----------------------------------------------
proc Usage {} {

   puts "Usage: vty_runcmd.exp <options>"
   puts "\n"
   puts "REQUIRED OPTIONS:"
   puts "   -h <hostname|ip>   = hostname or ip address"
   puts "   -u <username>      = username to login with"
   puts "   -p <password>      = password for login"
   puts "\n"
   puts "Other OPTIONS:"
   puts "   -e <enable password> = Enable password"
   puts "   -t <seconds>         = timeout in seconds"
   puts "   -m <ssh|telnet>      = use either ssh or telnet, default telnet"
   puts "   -f <filename>        = command file, defaults to STDIN"
   puts "\n"
}
# End of Check_ARGS()

#-----------------------------------------------
# main() 
# 
# RETURNS:
#    0 if successful
#    1 if invalid arg passed
#    2 not enough args (required args not met)
#-----------------------------------------------
   set rval 0
   set hostname ""
   set username ""
   set password ""
   set enable_pw ""
   set cmdfile ""
   set method "telnet"

   # Loop through the command line args   
   for {set n 0} {$n < $argc} {incr n} {
   
      set arg [lindex $argv $n]
      
      # Check the args
      if { [string compare $arg "-u"] == 0} {
         if { $n < $n+1 } {
            incr n
            set username [lindex $argv $n]
         } else {
            set rval 1
            puts "ERROR: Missing ARG for $arg\n"
         }
    
      } elseif { [string compare $arg "-p"] == 0} {
         if { $n < $n+1 } {
            incr n
            set password [lindex $argv $n]
         } else {
            set rval 1
            puts "ERROR: Missing ARG for $arg\n"
         }
         
      } elseif { [string compare $arg "-h"] == 0} {
         if { $n < $n+1 } {
            incr n
            set hostname [lindex $argv $n]
         } else {
            set rval 1
            puts "ERROR: Missing ARG for $arg\n"
         }

      } elseif { [string compare $arg "-m"] == 0} {
         if { $n < $n+1 } {
            incr n
            set method [lindex $argv $n]

         } else {
            set rval 1
            puts "ERROR: Missing ARG for $arg\n"
         }
         
      } elseif { [string compare $arg "-t"] == 0} {
         if { $n < $n+1 } {
            incr n
            set timeout [lindex $argv $n]
         } else {
            set rval 1
            puts "ERROR: Missing ARG for $arg\n"
         }
 
      } elseif { [string compare $arg "-f"] == 0} {
         if { $n < $n+1 } {
            incr n
            set cmdfile [lindex $argv $n]
         } else {
            set rval 1
            puts "ERROR: Missing ARG for $arg\n"
         }
         
      } elseif { [string compare $arg "-e"] == 0} {
         if { $n < $n+1 } {
            incr n
            set enable_pw [lindex $argv $n]
         } else {
            set rval 1
            puts "ERROR: Missing ARG for $arg\n"
         }
      }
   }
   # End of arg check
        
   # make sure we found the amount of args expected   
   if { [llength $hostname] > 0 && [llength $method] > 0 && 
          [llength $username] > 0 && [llength $password] > 0 } {
      # Print out the args found
      puts "hostname = $hostname, user = $username, pw = $password, method = $method"
      
   } else {
      set rval 2
      puts "ERROR: Missing required args, must have -h, -u, -p\n"
      Usage
   }
   
   # ------------------
   # Now that we have the correct ARGS and we know what to do, lets proceed to
   #     connect, run the commands, then exit.
   # ------------------
   
   # make sure we have not encountered any errors
   if { $rval <= 0 } {
   
      if { [llength $cmdfile] <= 0 } {
         puts "Enter the send text (type 'end' on last line to finish):"
         expect_user -nocase -re "(.*)\nend\n"
         set send_text $expect_out(1,string)
         
      } else {
         puts "Using $cmdfile for send text"
         # set cmdfile_fd [open $cmdfile r]
         if { [catch {set cmdfile_fd [open $cmdfile r]} err_msg] } {
            puts stderr "Could not open $cmdfile for reading\n$err_msg"
            exit 1
         }
         
         # read in the file info - warning there is a limit on the size
         set send_text [read $cmdfile_fd 10000]
         
         # close open file
         close $cmdfile_fd      
      }

      # connect and check return status before proceeding
      if { [Connect "$method" "$hostname" "$username" "$password"] > 0 } {
         # stop here,  no need to print an error since Connect func does that
         exit 1
      }

      
      # If we have an enable password, lets try to send it
      if { [llength $enable_pw] > 0} {
         puts "***Using enable mode"
         send "enable\r"
         expect {
            -timeout 3
            # Below handles the password prompt
            -nocase -re "word:" {
               send "$enable_pw\r"
               exp_continue
      
            # Below handles the normal prompt to detect when logged in
            } -re "#\ *$" {
               puts "--SUCCESS on enable mode--"

            # Below is for expect timeout waiting for a                                 
            } timeout {
               puts "ERROR: Enable password timeout"
               set rval 3
      
            # Below is for when the connect is closed before finishing   
            } eof {
               puts "ERROR: Connection to host failed: $expect_out(buffer)"
               set rval 4
            }         
         }
      }
      
      # Loop through the send_text and send one line at a time         
      foreach line [split $send_text \n] {
         # Make sure to exclude empty lines
         if { [llength $line] > 0 } {           
            send "$line\r"
            
            # Start the expect/send process to login
            expect {            
               # Below handles the yes/no prompts  
               -nocase -re "\(yes/no\)" {
                  send "yes\r"
                  exp_continue
               
               # Below handles the y/n prompts  
               } -nocase -re "\(yes/no\)" {
                  send "yes\r"
                  exp_continue

               # Below handles the y/n prompts  
               } -nocase -re "--more--" {
                  send " "
                  exp_continue
                                          
               # Below handles the normal prompt to detect when logged in
               } -nocase -re "$prompt" {
                  puts "\n--SUCCESS for normal login prompt--\n"
               }
            }
                
         }
      }
      
      # Now that we are done, send an exit
      puts "*** Finished with script"
      send "exit\r"
      sleep 1
   }

## END OF SCRIPT #############################################################

# 2  
Old 07-25-2012
If this is connecting to telnet, expect may be your best option, but if you're using ssh, you could use keys and avoid having to use expect at all while making your scripts far simpler in the bargain.

If it doesn't work in crontab, that's probably a problem with your environment as per our FAQ.
# 3  
Old 07-25-2012
thanks to reply my post,

yes I'm using ssh

don't know how to use keys
# 4  
Old 07-26-2012
Google 'passwordless ssh'. There's tutorials all over the internet.
# 5  
Old 07-26-2012
ok

as regard crontab I could solve the problem. Script now works but storing plain text password into the script is not so well done.

I'm not sure passwordless ssh can be used on cisco router/firewall.. at least I couldnt find anything..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to save output of echo and awk to a file

Hi, I am generating a YAML file from a hosts file, but am having trouble saving it to a new file. hosts file 127.0.0.1 localhost 192.168.1.2 host1 192.168.1.3 host2 192.168.1.4 host3 192.168.1.5 host4 YAML file $ echo 'host_entries:' && awk '{printf " %s:\n ip:... (3 Replies)
Discussion started by: sand1234
3 Replies

2. Shell Programming and Scripting

Save value from output of Corestat and save in a list for each core

I am trying to modify the "corestat v1.1" code which is in Perl.The typical output of this code is below: Core Utilization CoreId %Usr %Sys %Total ------ ----- ----- ------ 5 4.91 0.01 4.92 6 0.06 ... (0 Replies)
Discussion started by: Zam_1234
0 Replies

3. Shell Programming and Scripting

How to save sorted content of a inside the same file?

Hi All, When i use sort Test, here is the output: $ sort Test a b b c d e f g h i But the contents in the file remain unsorted, how to do that? ]$ cat Test g i (6 Replies)
Discussion started by: chandrakanth
6 Replies

4. Shell Programming and Scripting

How to save the 'nmon' output to a text file with a script?

Hi all, I want to do an Unix Script to save the 'nmon' output on a text file and I don't know how to do it. I need a Script for each monitoring and also one to multiple monitorings. Thanks (6 Replies)
Discussion started by: Javi1990
6 Replies

5. Shell Programming and Scripting

Write Awk output to a file , inside script.

Hi, Can anyone please help me with this issue. I have a Awk command which take file as input, and provides the output having multiple lines, its working in command mode, but not if i plug it in script. #!/bin/ksh infile=a.txt outfile=b.txt awk ' BEGIN{ FS=OFS="|";ORS = "\n";... (1 Reply)
Discussion started by: sp999
1 Replies

6. Shell Programming and Scripting

Save cURL verbose output to file or do it like browser "save as.."

hi there ! i have exactly the same problem like this guy here https://www.unix.com/shell-programming-scripting/127668-getting-curl-output-verbose-file.html i am not able to save the curl verbose output.. the sollution in this thread (redirecting stderr to a file) does not work for me.... (0 Replies)
Discussion started by: crabmeat
0 Replies

7. Shell Programming and Scripting

how to redirect the output of a grep command to a file inside a shell script

hi, i wat to get the output of a grep command in a file. but when i am trying out the same grep command in the unix prompt its working fine.. i am getting the output properly.. but when i am writing the same command inside my shell script , its just creating a new output file with no contents... (11 Replies)
Discussion started by: kripssmart
11 Replies

8. UNIX for Dummies Questions & Answers

how to get the output of a grep command to a file inside a shell script

hi, i wat to get the output of a grep command in a file. but when i am trying out the same grep command in the unix prompt its working fine.. i am getting the output properly.. but when i am writing the same command inside my shell script , its just creating a new output file with no contents... (1 Reply)
Discussion started by: kripssmart
1 Replies

9. Solaris

terminal output - save to file?

I have a window open on my ultra 10 - a terminal window connecting to a server. Is there any way I can log all output to this window to a log file on my ultra 10 ? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question

Featured Tech Videos