How to copy files from local machine to server using SSH file transfer protocol? How to copy files from local machine to server using SSH file transfer protocol? shell shell

How to copy files from local machine to server using SSH file transfer protocol?


Whether you spell it 'SECURE' or 'secure,' we can't read your mind and tell what you want to secure against. So I'll give a basic recipe and tell you what it's good for. This probably should all move to superuser.

  1. learn basic bourne shell. A tutorial for that can't fit into a stackoverflow answer.
  2. run ssh-keygen to make an ssh key pair. Since you want to run scp from cron, you can't use a passphrase, AFAIK. Which means that you have to be quite sure that the machine you are copying from is safe from intruders.
  3. copy the public key from the ssh key pair to your .ssh/authorized_keys on the target machine.
  4. prove that this works by running 'ssh target-machine' and successfully logging on sans-password.

Now, you can make a shell script that uses the scp command to do the actual copies. Start from:

#!/bin/shscp PATHNAME_OVER_HERE target-host:/PATHNAME_OVER_THERE

This is secure against basic password spying and against randoms connecting to target-host with telnet. It is not secure if the source system is not secure, and I cannot vouch for the security of ssh protocol, though it certainly is widely used.


How to copy files from local machine to server using SSH file transfer protocol?

Use scp.

I want to do it in a cron job.

The main issue with using scp in a cron job is this: where do you get your credentials?Putting your password in the clear is not a good idea. A better idea is to have an ssh-agent process running on your machine. To find an appropriate ssh-agent you can run this script:

#!/bin/shfor i in $SSH_AUTH_SOCK /tmp/ssh*/agent*do  if [ -r "$i" -a -w "$i" ]; then    case `SSH_AUTH_SOCK="$i" ssh-add -l` in      *:*:*) echo "$i"; exit 0 ;;    esac  fidoneexit 1

If the script succeeds, you get a value you can put into the SSH_AUTH_SOCK environment variable before running scp.

When you bring up the client, you should present your credentials by launching ssh-agent and running ssh-add.