Operating System - HP-UX
1824988 Members
1972 Online
109678 Solutions
New Discussion юеВ

Unable to use SFTP with CRON

 
Jon Singleton_1
New Member

Unable to use SFTP with CRON

Hello,

I am required to run an automated data feed using Secure FTP. To do this I decided to use the Expect Utility(since a password is
required). The Expect script runs fine from the commandline, but will not complete it's run using cron.
We use an HP-UX 11.0 OS, and I was able to get a simple Expect script using FTP(on a test machine) to run through cron, just when I attempt to use SFTP2 I run into problems.

The Expect script that I am running writes to a log file and it appears to break after the password has been entered.

The below lines are where the log file displays the first error that occurs:

remote path: Error: Connection was lost.
remote server failed.

Then, of course, the rest of the script begins failing from there.

I have this script checking to see if a controlling terminal exists, and it does for the commandline run, but does not for the cron job. I am not sure if this is where the problem occurs, and if it is I am unsure how I should set a controlling terminal.

Any ideas, examples of cron/sftp2/expect or recommendations on going in a different direction would be greatly appreciated.

Jay
17 REPLIES 17
Steven E. Protter
Exalted Contributor

Re: Unable to use SFTP with CRON

The answer is, yes you can.

I'm trying to do it but have yet to succeed.

I'm actually trying scp.

The problem relates to getting a common public key on the target and remote system. Then you should be able to work it out.

I have an ftp script that works, but it requires a hardcoded password, so I don't use it much.

It will become a critical issue around here in about two months, so check back.

Steve
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Kellogg Unix Team
Trusted Contributor

Re: Unable to use SFTP with CRON

If you set up ssh with null passphrase, you will be able to sftp w/o being prompted for password. Thus, you won't have to use Expect utility.

I tested out in my test environment and it works just fine. I am using RSA keys for ssh2.

...Manjeet
work is fun ! (my manager is standing behind me!!)
Jon Singleton_1
New Member

Re: Unable to use SFTP with CRON

Hi Manjeet,

Unfortunately, one of my requirements is to use a password for sftp.

Thanks,
Jon Singleton_1
New Member

Re: Unable to use SFTP with CRON

Thanks for taking a look at this issue. For those interested, I was able to get the script to work with cron by setting the path in the expect script.

Jay
Steven E. Protter
Exalted Contributor

Re: Unable to use SFTP with CRON

If you want to use scp for the file copies you need to follow this cookbook. Attached.

In case you want enhanced security, you can exchange public keys.

attached.

P
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Igor Hudak
New Member

Re: Unable to use SFTP with CRON

I would like to use the similar solution.
My status is that script executed from command prompt run OK.
When I execute script from crontab - sftp connection fails.

You wrote, that your script is running with cron after path have been set.
Please, could you specify "setting the path in the expect script".

thank you
Gavin Clarke
Trusted Contributor

Re: Unable to use SFTP with CRON

Hi Igor,

Your chance of a reply from Jon Singleton here is slim.

Jon Singleton
Last contribution date: February 04, 2003

I am not an expect person myself.

export PATH=$PATH:/pathtosftp

might be in there somewhere.

Cheers.
OldSchool
Honored Contributor

Re: Unable to use SFTP with CRON

either what Gavin noted or use:

//expect

instead of

expect

in the scripts

TwoProc
Honored Contributor

Re: Unable to use SFTP with CRON

I've used the program called "expect" to handle that very thing in cases where I didn't want to share a key.
We are the people our parents warned us about --Jimmy Buffett
Igor Hudak
New Member

Re: Unable to use SFTP with CRON


Hi Gavin, Admin,

thank you for your instant replays.

I use full path to expect utility in my script.

I wrote two scenarios:
1st - I am calling
/opt/expect/bin/expect -c ' ...... ' in my sh-script

(SHLIB_PATH, PATH variables were set correctly and exported.)

2nd - there is sh-script calling another script which starts with
#!/opt/expect/bin/expect

The both scenarious work OK, while I run script from the command line.(Doen not matter in which directory I am - no problem to run script from home directory or different one)

The only, problem is the calling script from cron.

there is log from my test SFTP server:

02-01-2006 09:52:20 IP 10.170.134.20 SFTP sftpuser connected
02-01-2006 09:52:20 IP 10.170.134.20 SFTP sftpuser is changing dir to / (D:\FreeFTPD\sftpuser\)
02-01-2006 09:52:20 IP 10.170.134.20 SFTP sftpuser is changing dir to /rm (D:\FreeFTPD\sftpuser\rm\)
02-01-2006 09:52:20 IP 10.170.134.20 SFTP sftpuser is listing /rm (D:\FreeFTPD\sftpuser\rm\)
02-01-2006 09:52:20 IP 10.170.134.20 SFTP sftpuser is listing /rm (D:\FreeFTPD\sftpuser\rm\test.txt)
02-01-2006 09:52:20 IP 10.170.134.20 SFTP sftpuser is listing /rm (D:\FreeFTPD\sftpuser\rm\test.txt)
02-01-2006 09:52:20 IP 10.170.134.20 SFTP sftpuser is downloading /rm/test.txt (D:\FreeFTPD\sftpuser\rm\test.txt)
02-01-2006 09:52:20 IP 10.170.134.20 SFTP sftpuser transfer complete
02-01-2006 09:52:20 IP 10.170.134.20 SFTP sftpuser disconnected

02-01-2006 09:54:01 IP 10.170.134.20 SFTP connection attempt
02-01-2006 09:54:03 IP 10.170.134.20 SFTP sftpuser disconnected

at 9:52:20 - srcipr run fromcommand line
at 9:54:01 - script sun from cron

any idea, please?

thank you

Muthukumar_5
Honored Contributor

Re: Unable to use SFTP with CRON

Can you get cron.log information for this execution? Is there any more debug informations?

--
Muthu

Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Unable to use SFTP with CRON

Refer this for reference to tune problem:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=757938

--
Muthu
Easy to suggest when don't know about the problem!
Gavin Clarke
Trusted Contributor

Re: Unable to use SFTP with CRON

Hi Igor,

There will be something missing from cron's environment settings you will need to set them as part of the calling script. I guess.

The thread Muthu pointed out is suggesting that too.

Cheers.
Igor Hudak
New Member

Re: Unable to use SFTP with CRON


Hello everybody!

Finally, the cron - sftp problem have been solved.

There was "cron - expect - sftp -b cmdFile" problem. The combination above does not work properly.

Solution is to move sftp commands from batch-command file directly to expect script
and do not use sftp -b, when you plan to run script with cron.

thank you

regards
Igor


Gavin Clarke
Trusted Contributor

Re: Unable to use SFTP with CRON

Nice one Igor,

Seems like I lead you down the garden path. I'm sorry about that I certainly didn't intend to.

This will be a good resource for others now that you have posted the answer.

Cheers
Gavin.
walt a
New Member

Re: Unable to use SFTP with CRON

Jay,
After fighting with this for a week I found what works for me at

http://www.linux.org/lessons/interm/c316.html

I added "expect eof" after sending the password.

walt
Steven E. Protter
Exalted Contributor

Re: Unable to use SFTP with CRON

This thread was started in 2002, is over 5 years old and has little of relavence input in the past few years.

Please read ITRC ettiquette guidelines and stop bringing long dead threads to the top of the queue.

Regards,

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com