SFTP or scp with password in a batch script without using SSH keys and expect script

Dear All,

I have a requirement where I have to SFTP or SCP a file in a batch script. Unfortunately, the destination server setup is such that it doesn't allow for shell command line login. So, I am not able to set up SSH keys. My source server is having issues with Expect. So, unable to use expect scripting too. FTP is not allowed as it is setup for secure transfer only.

In brief, I am looking to automate the file transfer through SFTP or SCP by passing password as argument- probably reading it from a hidden file. Please advise. Thanks.

Andr� Frimberger � Blog Archive � reading SSH password from stdin

Am sorry, but don't understand a lot of it. Can you please simplify this with a simple example of sftp/scp of a file. Sorry again.

Try using sftp to copy authorized_keys to the remote host:

 
sftp user@host
sftp> mkdir .ssh
sftp> chmod 700 .ssh
sftp> cd .ssh
sftp> put /home/user/.ssh/id_rsa.pub authorized_keys

A quick work-around - didn't think of it. Great idea, achenle. I was able to place the public key on the remote machine. Thanks for your help.

---------- Post updated at 10:40 AM ---------- Previous update was at 09:54 AM ----------

Learning from this:
You can't hard-code the password in a script while doing sftp or scp (ofcourse, you can do this after installing utilities like sshpass and expect). Because that is what sftp or scp is about - secured ftp or secured copy!

This is what I understand guys. Please update if I am wrong.

Not just ssh but any sane login method, including su and sudo, are specifically designed to prevent you from doing exactly what you are trying to do, because it is a horrible idea. Don't do it.

Utilities like sshpass get around this by just giving up and allowing you to create the gaping security holes ssh, su, sudo, and many other authentication methods are designed to prevent. Did you know that whenever you pass a plaintext argument into it, it is briefly visible on your system? This is not a bug. This is just what happens when you do this. sshpass cannot prevent it, it can only try and make the time it is visible as short as possible.

1 Like