How to Pass Password to Scp

How to pass password to scp?

You can script it with a tool like expect (there are handy bindings too, like Pexpect for Python).

want to pass the password from the shell script for the scp command for transferring the file from one server to another

You can use sshpass for that.

sshpass  -p password scp -r root@$source_host:/$source_path root@$destination_host:/$destination_path

Pass password with sshpass to both servers when scp copying from one server to another

This is not possible.

According to scp manual:

  • -3

    Copies between two remote hosts are transferred through the local host. Without this option the data is copied directly between the two remote hosts. Note that this option disables the progress meter and selects batch mode for the second host, since scp cannot ask for passwords or passphrases for both hosts.

And according to ssh_config manual:

  • BatchMode

    If set to yes, user interaction such as password prompts and host key confirmation requests will be disabled. This option is useful in scripts and other batch jobs where no user is present to interact with ssh(1). The argument must be yes or no (the default).

How to pass in the password in scp using Java ssh JSch and jcabi-ssh

Automating an execution of an interactive command is a sign of a bad design. Anyway...


The scp will prompt your for a password in an interactive session/terminal only. For security reasons, it won't read the password from a plain standard input.

So you have to enable an interactive session/terminal.

In JSch, you do that by calling .setPty:

channel.setPty(true);
channel.connect();

Similar question: Use JSch sudo example and Channel.setPty for running sudo command on remote host.


Another approach is using expect or sshpass tools: How to pass password to scp?



Related Topics



Leave a reply



Submit