Expect script on crontab


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect script on crontab
# 1  
Old 12-10-2004
Expect script on crontab

Hi All,

I have an expect script called sftp to transfer using SFTP below :

[root@prod:/root]
# more sftp
#!/usr/local/bin/expect

# Initialisation
set authFile "/home/ap1030/transfer/.password"

# Check the authorisation file exists
if {![file exists $authFile]} { ;# Does file exist
send_user "$authFile does not exist; aborting\n"
exit 1
}
set fileFD [open $authFile r] ;# Open the auth file
gets $fileFD authLine ;# Read in 1 line to authLine
close $fileFD

# Transfer file(s)
spawn /opt/openssh/bin/sftp -b /home/ap1030/transfer/batchFile importve@www.importve.com
expect "password:"
send "$authLine\n";
interact

When I run this script manually from command line, it was running fine. But when I put on crontab, it was NOT working. I believe this is related to "interact" command on the last line. It always get stuck after password. How can I overcome this problem? Pls help.

Below is my crontab entry and the log file. Pls help.

05 15 * * * /usr/local/bin/expect -f /home/ap1030/transfer/sftp > /tmp/sftp 2>1&

[importve@sihp8026:/home/ap1030/transfer]
$ more /tmp/sftp
spawn /opt/openssh/bin/sftp -b /home/ap1030/transfer/batchFile importve@www.importve.com
Connecting to www.importve.com...
importve@www.importve.com's password:

Thanks and Best Regards,
Dewa
# 2  
Old 12-10-2004
Re: Expect script on crontab

Quote:

$ more /tmp/sftp
spawn /opt/openssh/bin/sftp -b /home/ap1030/transfer/batchFile importve@www.importve.com
Connecting to www.importve.com...
importve@www.importve.com's password:

From your log it shows that ... cron was fired ...
but is it asking the passwd there ???

Is it not asking the passwd when u ran on the cmd line as "root" ???

Is crontab created by root or some other user ??
# 3  
Old 12-13-2004
ap1030, Please see the rules. Do not cross-post questions. This will not safeguard a speedier response.

I have deleted the duplicate post from the "Dummies" forum.

Thanks,
ZB
# 4  
Old 04-28-2006
I am also having the same problem like AP1030. How come the problem is not solved in 3 years time?
# 5  
Old 08-07-2008
It's 2008 now and I'm also having the same issue. Does anyone have an idea?
# 6  
Old 10-03-2008
Running things from cron is always interesting. What user is the cron job running as?
The environment that you are used to as a normal user doesn't exist for the cron job so you have to figure out what is missing and add it to the script. Making this change won't make a visible difference when you run it from your login but will make a world of
differenct to the cron job.
# 7  
Old 06-04-2009
Is the interact command needed in your script? Since it is running in cron there is no shell or user for it to interact with and probably wont work. Maybe you should have it do something similar to this:

# Transfer file(s)
set timeout 600
spawn /opt/openssh/bin/sftp -b /home/ap1030/transfer/batchFile importve@www.importve.com
expect "password:"
send "$authLine\n";
expect "sftp>"
send "bye\r"
expect eof
exit



I cannot remember what the sftp prompt looks like. So you may need to adjust the "sftp>" to SFTP> or whatever it actually is. You can verify the prompt by doing it manually.

Also, the timeout should be set high enough to ensure that your file transfers before it times out. 600 is 10 minutes. If it will take longer to transfer the file then you should adjust this accordingly.

Also, not sure if \n will work when sending the authline. If it does not work then you could just set the password directly (for trouble-shooting purposes since you may not want to leave the password in the script) such as in the following:


# Transfer file(s)
set timeout 600
set sftppw "mypassword"
spawn /opt/openssh/bin/sftp importve@www.importve.com
expect "password:"
send "$sftppw\r";
expect "sftp>"
send "bye\r"
expect eof
exit
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Expect script returning string following a found expect.

I'm fairly new to scripting so this might not be possible. I am using Expect with Cisco switches and need to capture the string after finding the expect request. For example, when I issue "show version" on a Nexus switch, I'm looking to capture the current firmware version: #show version ... (0 Replies)
Discussion started by: IBGaryA
0 Replies

2. 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

3. Programming

Calling another expect script inside an expect script

I have an expect script called remote that I want to call from inside my expect script called sudoers.push, here is the code that is causing me issues: set REMOTE "/root/scripts/remote" ... log_user 1 send_user "Executing remote script as $user...\n" send_user "Command to execute is: $REMOTE... (1 Reply)
Discussion started by: brettski
1 Replies

4. Shell Programming and Scripting

Shell script using expect to login to couple of remote servers and read "crontab -l"

I need a shell script using expect to login to couple of remote servers and read "crontab -l -u <username>" & "cat /etc/rc.local" & "df -h" and able to create output into a file saved locally with hostname.crontab & hostname.rc.local & disk.status. I can supply a file as list of hostname or IP... (4 Replies)
Discussion started by: jaipsharma
4 Replies

5. Programming

Calling expect script inside another expect

Hi, Am very new to expect scripting.. Can You please suggest me how to call an expect script inside another expect script.. I tried with spawn /usr/bin/ksh send "expect main.exp\r" expect $root_prompt and spawn /usr/bin/ksh send "main.exp\r" expect $root_prompt Both... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

6. 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

7. Shell Programming and Scripting

Expect Script Not working with Crontab

I have the following expect script sitting on a Linux box. === #!/usr/bin/expect -f # # backup.expect # # Expect script to backup a firewall via a SSH session # # set firewall set username set password set prompt set filename match_max 50000 spawn ssh -l... (2 Replies)
Discussion started by: alagondar
2 Replies

8. Shell Programming and Scripting

Need help with Expect script for Cisco IPS Sensors, Expect sleep and quoting

This Expect script provides expect with a list of IP addresses to Cisco IPS sensors and commands to configure Cisco IPS sensors. The user, password, IP addresses, prompt regex, etc. have been anonymized. In general this script will log into the sensors and send commands successfully but there are... (1 Reply)
Discussion started by: genewolfe
1 Replies

9. 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

10. Shell Programming and Scripting

strange expect script behavior, or am i misunderstanding expect scripting?

Hello to all...this is my first post (so please go easy). :) I feel pretty solid at expect scripting, but I'm running into an issue that I'm not able to wrap my head around. I wrote a script that is a little advanced for logging into a remote Linux machine and changing text in a file using sed.... (2 Replies)
Discussion started by: v1k0d3n
2 Replies
Login or Register to Ask a Question