Unable to run tcl script via crontab(buffer error)

Unanswered Question
Oct 31st, 2013
User Badges:

Hi All


I am correcrtly executing the script below from my linux server. However, it seems that once I try to run it via crontab (correct permisions have been set etc) I get the following error:


can't read "expect_out(buffer)": no such variable

    while executing

"set output $expect_out(buffer)"

    ("foreach" body line 6)

    invoked from within

"foreach cmd $commands {

        send "$cmd\r"

        expect -re $prompt

        #remove zeros from the cmd filename

        regsub -all {[ \r\t\n]+} ..."

    ("foreach" body line 20)

    invoked from within

"foreach device $device_list {

# Set each device's log file to be the name of the device...

    set file_name $device

# we initiate the SSH connection


Crontab is:

0 4 * * *          /usr/bin/expect  /<path of the script>/<script name>.exp


Script below:

#!/usr/bin/expect


# Here, we specify all our commands in a list, that we will issue one

# by one at a later time.

set commands [list "show ip route summary" "show interfaces" "show arp" "show ip ospf neighbor"  "show ip eigrp neighbor" "show cdp neighbors"]


# Set the date.

set date [timestamp -format %C%y%m%d]

# This variable is for a file called switches.txt that has the hostname/IP

# of all of the routers you are collecting information from.tepad

set device_list [read [open "/<path>/switches.txt"]]

# Specify the username and password, as well as what we expect the routers'

# prompt to be.

set pass "*******"

#set prompt "#"

set prompt {([#>]) ?$}

# This command tells expect not to echo the output to the console.

exp_log_user 0

#log_user 1

# We loop through each device in our list, one by one...

foreach device $device_list {

# Set each device's log file to be the name of the device...

    set file_name $device

# we initiate the SSH connection

    eval spawn ssh $device -l ********

    match_max [expr 32 * 1024]


#If we see a message asking about the device's host key, accept it.

    interact -o -nobuffer -re "assword: $" return

    send "$pass\r"


# Loop through each command that we specified earlier.

    expect -re $prompt

    send "term len 0\r"

    expect -re $prompt


    foreach cmd $commands {

        send "$cmd\r"

        expect -re $prompt

        #remove zeros from the cmd filename

        regsub -all {[ \r\t\n]+} $cmd "" correct_cmd

        set output $expect_out(buffer)

        set fd [open /<path>/$file_name-$date-$correct_cmd.txt w]

        puts $fd $output

        close $fd

    }

    expect -re $prompt

    send "exit\r"

    exp_close -i $spawn_id

    exp_wait



}


Not sure why this is the case. Anyone can recommend a solution?


Thanks

Pantelis

  • 1
  • 2
  • 3
  • 4
  • 5
Overall Rating: 4.2 (5 ratings)
Loading.
Joe Clarke Sat, 11/02/2013 - 10:36
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

Could it be that you need to fully-qualified path to ssh?  I'd think it would fail earlier, though.  You may want to re-enable user logging and exp_internal to see what expect is doing when run from within cron.

pantelis1 Sun, 11/03/2013 - 04:31
User Badges:

hI Joseph


I have already done that and the problem is below:


can't read "expect_out(buffer)": no such variable as per my initial post


Thanks

Pantelis

Joe Clarke Sun, 11/03/2013 - 10:02
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

But if you enable more debugging, you will see the what is being sent/received to/from the device.  If this variable isn't being set, there is something with the match criteria.  What is the full output when you have log_user and exp_internal enabled?

pantelis1 Mon, 11/04/2013 - 00:14
User Badges:

Hi Joseph


Even after enabling log_user 1 I am still not able to get any errors (to be honest I am not getting the buffer error either this time ). The relevant config is :

I have also edited the crontab to output the file as per below:


Crontab is:

0 4 * * *          /usr/bin/expect  //.tcl > //output.txt


# This command tells expect not to echo the output to the console.

#exp_log_user 0

log_user 1

# We loop through each device in our list, one by one...

I have also tried

      

# This command tells expect not to echo the output to the console.

exp_log_user

1

log_user 1

# We loop through each device in our list, one by one...

in both cases the the script runs via crontab it does nothing.The output on the relevant log file is :

"spawn ssh x.x.x.x.x -l ****"

Joe Clarke Mon, 11/04/2013 - 12:13
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

Can you try fully-qualifying the ssh path.  For example:


spawn /usr/bin/ssh x.x.x.x -l ****

pantelis1 Tue, 11/05/2013 - 04:39
User Badges:

It performs exactly the same but this time produces :

spawn /usr/bin/ssh x.x.x.x -l ****

Joe Clarke Tue, 11/05/2013 - 12:23
User Badges:
  • Cisco Employee,
  • Hall of Fame,

    Founding Member

Sorry then.  I'm out of ideas.  I'm not seeing the debug I'd expect.  Maybe there's a problem with your cron implementation.  Could also be an environment variable you need to set or not set...

Maher Abdelshkour Wed, 12/18/2013 - 07:25
User Badges:
  • Silver, 250 points or more

Hello Pantelis,

You need to declare the whole of expect_out global rather than just one element.


Thank you!

pantelis1 Thu, 12/19/2013 - 03:44
User Badges:

Hi Maher


Could you please elaborate on this?

Thanks

Pantelis

Maher Abdelshkour Thu, 12/19/2013 - 06:17
User Badges:
  • Silver, 250 points or more

Pantelis,


Use the curly braces to avoid substitution.


1. interacting with a program on your local machine:

#!/usr/bin/expect -d

spawn "/bin/bash"

send "term len 0\r"

expect -re eof

puts $expect_out(buffer)



2. interacting with a remote program over ssh:

!/usr/bin/expect -d


spawn ssh [email protected]

set prompt ":|#|\\\$"

interact -o -nobuffer -re $prompt return

send "mypassword\r"

interact -o -nobuffer -re $prompt return

send $cmd

send "exit\r"

expect eof

puts $expect_out(buffer)




Hope this helps

Regards!

Actions

This Discussion

Related Content