Operating System - HP-UX
1820594 Members
1432 Online
109626 Solutions
New Discussion юеВ

Automating SFTP via Scripting through Cron

 
SOLVED
Go to solution
Jason Mecucci
Occasional Advisor

Automating SFTP via Scripting through Cron

We have a lot of automated FTP scripts, but we are just not moving to SFTP so that we aren't transfering clear text passwords since this FTP server is on a more senstive network.

Does anyone have any good documents on how to script the sftp process, or if you could just give me an example of a sftp script where you logged in with a username and password and did a get or a put.

Thanks in advance,
Jason
6 REPLIES 6
Steven E. Protter
Exalted Contributor

Re: Automating SFTP via Scripting through Cron

automated sftp scripting will work just like automated ftp scripting.

The ascii binary command needs to be removed

sftp user@hostname >> EOF
password
cd
get *
put *
EOF

This method is insecure.

It hard codes a password to a file. It is not necessary.

sftp comes with a command called scp.

scp works like rcp but its secure. You can exchange public keys(see attached doc) and do transfers as follows:

scp * targethost://directory

Secure, no passwords hardcoded into scripts.

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
John Poff
Honored Contributor

Re: Automating SFTP via Scripting through Cron

Hi,

You can use the '-b' option with sftp to specify a batchfile of commands for it to use. I would suggest configuring your sftp/ssh so that it doesn't require usernames or passwords (non-interactive authentication), so that you don't have them inside of your batch file.

JP
Sridhar Bhaskarla
Honored Contributor

Re: Automating SFTP via Scripting through Cron

Hi Jason,

For 'sftp' all you have to do is to setup public/private key authentication. For ex., if you are sftping as user1 on sys1 to user2 on sys2, then do

user1@sys1:ssh-keygen -t dsa -N ""

user1@sys1: ll .ssh/id_dsa.pub
Copy id_dsa.pub file onto sys2 into /tmp dir
user2@sys2: mkdir -p .ssh
user2@sys2:cd .ssh
user2@sys2:cat /tmp/id_dsa.pub >> authorized_Keys

user1@sys1:sftp user2@sys2 << EOF
cd /somewhere
put somefiles\*
quit
EOF

Above shouldn't prompt for a password. If you are going to hardcode username and password, then even sftp will be unsecure though not as bad as ftp.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Jason Mecucci
Occasional Advisor

Re: Automating SFTP via Scripting through Cron

How to stop shell script using SFTP from being in an interactive mode? For eg, with FTP you can use FTP ├в in?

Thanks for all the support so far!
Ja
Sridhar Bhaskarla
Honored Contributor
Solution

Re: Automating SFTP via Scripting through Cron

Hi Jason,

What I do is to specify only publickey authentication. So, if it fails, it will not go keyboard interactive. For ex.,

sftp -o "PreferredAuthentications publickey" << EOF
...
..
EOF

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Jason Mecucci
Occasional Advisor

Re: Automating SFTP via Scripting through Cron

thanks guys, you've been a lot of help!