Expect Script Not working with Crontab


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect Script Not working with Crontab
# 1  
Old 04-08-2012
Expect Script Not working with Crontab

I have the following expect script sitting on a Linux box.
===
Code:
#!/usr/bin/expect -f
#
# backup.expect
#
# Expect script to backup a  firewall via a SSH  session
#
#

set firewall    [ lrange $argv 0 0 ]
set username    [ lrange $argv 1 1 ]
set password    [ lrange $argv 2 2 ]
set prompt      [ lrange $argv 3 3 ]
set filename    [ lrange $argv 3 3 ]
match_max 50000

spawn ssh -l $username $firewall
expect "yes/no" { send "yes\r" }
expect "assword:"
send "$password\r"
expect -timeout 10 "word>"
set output [open "$filename" "w"]
set running 1
send "show config running\r"
expect "\n"
expect "\[K"
while { $running  > 0 } {
        expect {
                "\n"    { puts -nonewline $output "$expect_out(buffer)" }
                "lines *-* " { send " " }
                "assword:"   { set running 0 }
                eof             { set running 0 }
                timeout         { set running 0 }
        }
}
send "exit\r"

===

Its entire purpose is to login to a firewall device, do a "show config running", collect the output into a file. When i run it manually, it works flawlessly - but when it is executed VIA crontab i get the following:

===
Code:
Running commands on 10.0.0.1...
spawn ssh -l xxx 10.0.0.1
Password: 
Last login: Sun Apr  1 00:26:41 2012 from xxx
xxx@10.0.0.1> show 
xxx@10.0.0.1> show config 
xxx@10.0.0.1> show config running
WARNING: terminal is not fully functional
-  (press RETURN)Finished running commands on xxx...

===


and when i run the script manually i get this:
===
Code:
Password: 
Last login: Sun Apr  8 05:27:10 2012 from xxx
xxx@10.0.0.1> show config running

config { full config correctly displayed...

===

So my question is, why does crontab output
Code:
xxx@10.0.0.1> show 
xxx@10.0.0.1> show config 
xxx@10.0.0.1> show config running

when manualy it correctly inputs as per my script....:
Code:
xxx@10.0.0.1> show config running


Also, what could "WARNING: terminal is not fully functional" possibly mean? Is my SSH session spawned incorrectly when run VIA cron? By the way both the crontab and manual execution of the script is done through bash, and the same user...... any help is appreciated...... thanks!


-Alagondar

Last edited by Franklin52; 04-08-2012 at 06:33 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 04-08-2012
Sorry, this should never work. There is no terminal context in cron. It is a background process.

Ps Please mention what Operating System and version you are running ("Linux box" is just too vague to be serious).
Just in case anybody on this board knows a dodge.


Quote:
When i run it manually, it works flawlessly
Please post what you typed verbatim and the response (abridged if it is long), and the exact and total response.

Last edited by methyl; 04-08-2012 at 06:01 PM..
# 3  
Old 04-09-2012
Code:
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=11.04
DISTRIB_CODENAME=natty
DISTRIB_DESCRIPTION="Ubuntu 11.04


And to get the script to run i type exactly this:

Code:
expect scriptmentionedabove.expect \$device_ip \$device_user \$device_userpw \$prompt config.cfg

And the response i get is this:
Code:
Password:  
Last login: Sun Apr  8 05:27:10 2012 from xxx 
xxx@10.0.0.1> show config running  config {}
#script then exits and copies config{} to the file "config.cfg"

By the way thank you for your response Smilie I am hoping you have an answer for me Smilie i am able to get another script to use expect with cron and SCP the config out... unfortunately this is not an option with this device.

Moderator's Comments:
Mod Comment Welcome to the UNIX and Linux Forums. Please use code tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-09-2012 at 07:39 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script not working via crontab

Hi, I have written one script which is connecting to the the database and generating one CSV, it is running fine when i ran it manually though it is throwing any warning but CSV is generating and working fine but same script when i have configured in crontab not working and giving error, kindly... (1 Reply)
Discussion started by: ash1234
1 Replies

2. Shell Programming and Scripting

Script not working via crontab

Hi, I have written one script which is connecting to the the database and generating one CSV, it is running fine when i ran it manually though it is throwing any warning but CSV is generating and working fine but same script when i have configured in crontab not working and giving error, kindly... (6 Replies)
Discussion started by: ash12345
6 Replies

3. Shell Programming and Scripting

Expect not working inside my Bash script

I am trying to execute expect command inside by small bash script to login into servers using key authentication method. My script is as follows: #!/bin/bash HOST=$1 /usr/bin/expect -c " spawn ssh -i /root/.ssh/id_rsa root@$HOST expect -exact "Enter... (3 Replies)
Discussion started by: John Wilson
3 Replies

4. Shell Programming and Scripting

scp not working in expect script

Hi All, I run the scp command in shell prompt without issue, but when on expect script as below: #!/usr/bin/expect spawn scp /var/spool/sms/failed.tar.gz abc@10.10.12.2:/home/abc expect "abc@10.10.12.2's password: " send "abcfef\r" exit 0 It looks not working at all and the... (3 Replies)
Discussion started by: elingtey
3 Replies

5. Shell Programming and Scripting

Expect script not working in crontab with minicom

Hi All, I am testing expect script in command prompt without issue, but in crontab it is not working, i check the output error as below: #cat /var/log/testexp.log spawn minicom -C /var/log/minicom1.log No cursor motion capability (cm) AT+COPS=? I am new in scripting, together... (1 Reply)
Discussion started by: elingtey
1 Replies

6. Shell Programming and Scripting

expect script inside shell script not working.

Shell Scipt: temp.sh su - <$username> expect pass.exp Expect script: pass.exp #!/usr/bin/expect -f # Login ####################### expect "Password: " send "<$password>\r" it comes up with Password: but doesnt take password passed throguh file. (2 Replies)
Discussion started by: bhavesh.sapra
2 Replies

7. Shell Programming and Scripting

Cannot get this bash/expect script to run under a crontab

#!/bin/bash # # RAP configuration script # # Usage: ./rap.sh # # Requires: expect, tcl # # Script expects to find a file called rap.csv located in the same directory as the script. If the file is placed # in a different directory, modify the custom entries section to specify the absolute... (8 Replies)
Discussion started by: mrkool
8 Replies

8. Shell Programming and Scripting

ssh is not working while calling through expect shell script

Hi, Please share you experience and way out on below error:--> #!/bin/bash -xv FILE=login.txt + FILE=login.txt CONNECT=sshlogin.exp + CONNECT=sshlogin.exp SERVERNAME=$1 + SERVERNAME=192.168.12.1 MyServer="" + MyServer= MyUser="" + MyUser= MyPassword="" + MyPassword= exec 3<&0 +... (6 Replies)
Discussion started by: manish_1678
6 Replies

9. Shell Programming and Scripting

Expect script on crontab

Hi All, I have an expect script called sftp to transfer using SFTP below : # more sftp #!/usr/local/bin/expect # Initialisation set authFile "/home/ap1030/transfer/.password" # Check the authorisation file exists if {!} { ;# Does file exist send_user "$authFile does not exist;... (6 Replies)
Discussion started by: ap1030
6 Replies

10. Shell Programming and Scripting

Expect script doesn't work under crontab

Hi All, Using Expect script when I run it manually it works. But when I put the entry in crontab, the job is still running after 15 hours. The script was created as root. I don't think it's a permission issue. Any idea? This is what I have under root crontab... 00 18 * * 1-5... (4 Replies)
Discussion started by: samnyc
4 Replies
Login or Register to Ask a Question