1830899 Members
2897 Online
110017 Solutions
New Discussion

ssh/sftp

 
Raymond Ford
Advisor

ssh/sftp

I would like to write a script that will sftp a file from one server to another. Exmaple: I will ssh from master/host to slave/server. Then want to do an sftp of a file from the slave/server to the master/host. I want to set up a cron file to have this run automatically. Any suggestions?
I'm an apprentice among journeymen who spend a lot of time on the virtual road.
3 REPLIES 3
Denver Osborn
Honored Contributor

Re: ssh/sftp

Rather than scripting with sftp use scp and set prefered auth to use public key 1st.


your script would use scp similar to...

scp /local_path/file username@remote:/remote_path


You'd also have to setup the user's ~/.ssh/authorized_keys file and keep the passphrase empty. This would make it so that the cron job could run without user interaction.

-denver
Steven E. Protter
Exalted Contributor

Re: ssh/sftp

This would be the same as a regular ftp script.

Here is my cut.

If you exchange public keys, you can use scp for one line command copies between any servers running openssh.

Attaching a doc:

sftp usrname@hostname << END

put filename
bye
END

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
generic_1
Respected Contributor

Re: ssh/sftp

generate your ssh keys on both systems. Then copy your public to the oher server in the .ssh/authorized_keys2 file.
Note you will need to coonect manually each directions if that is what you intend once and answer yes to add the hostnames to the known hosts.
This way you can sftp without a password, assuming you do not have a passphrase.
You can get fancier specify a passphase and set up the keys agent to pass the passphrase for you. In the long run this might be more secure.

next set up a cron to scp the file
file as the correct user you are using. If you run the job as root and send the file as a user you will have more to do in the sense you will need to specify the key location with the -i scp option and put the keys in root. This is a bit more overhead than you may want to deal with.
scp hosta:/tmp/yourfile hostb:/yourdirectory.