<p>I know it is not recommended, but is it at all possible to pass the user's password to scp?</p>

<p>I'd like to copy a file via scp as part of a batch job and the receiving server does, of course, need a password and, no, I cannot easily change that to key-based authentication.</p>

[{'answer_id': 50102, 'author': 'Pat Notz', 'author_id': 825, 'author_profile': 'https://Stackoverflow.com/users/825', 'pm_score': 6, 'selected': True, 'text': '<p>You can script it with a tool like <a href="http://expect.sourceforge.net/" rel="noreferrer">expect</a> (there are handy bindings too, like <a href="http://www.noah.org/wiki/Pexpect" rel="noreferrer">Pexpect</a> for Python).  </p>\n'}, {'answer_id': 50113, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 3, 'selected': False, 'text': '<p>Here is an example of how you do it with <code>expect</code> tool:</p>\n<pre class="lang-perl prettyprint-override"><code>sub copyover {\n    $scp = Expect-&gt;spawn(&quot;/usr/bin/scp ${srcpath}/$file $who:${destpath}/$file&quot;);\n    $scp-&gt;expect(30,&quot;ssword: &quot;) || die &quot;Never got password prompt from $dest:$!\\n&quot;;\n    print $scp \'password\' . &quot;\\n&quot;;\n    $scp-&gt;expect(30,&quot;-re&quot;,\'$\\s\') || die &quot;Never got prompt from parent system:$!\\n&quot;;\n    $scp-&gt;soft_close();\n    return;\n}\n</code></pre>\n'}, {'answer_id': 70299, 'author': 'rjray', 'author_id': 6421, 'author_profile': 'https://Stackoverflow.com/users/6421', 'pm_score': -1, 'selected': False, 'text': "<p>An alternative would be add the public half of the user's key to the authorized-keys file on the target system. On the system you are initiating the transfer from, you can run an ssh-agent daemon and add the private half of the key to the agent. The batch job can then be configured to use the agent to get the private key, rather than prompting for the key's password.</p>\n\n<p>This should be do-able on either a UNIX/Linux system or on Windows platform using pageant and pscp.</p>\n"}, {'answer_id': 4310068, 'author': 'gWay', 'author_id': 524607, 'author_profile': 'https://Stackoverflow.com/users/524607', 'pm_score': 6, 'selected': False, 'text': '<p>If you are connecting to the server from Windows, the Putty version of scp ("pscp") lets you pass the password with the <code>-pw</code> parameter.</p>\n\n<p><a href="http://the.earth.li/~sgtatham/putty/0.67/htmldoc/Chapter5.html#pscp" rel="noreferrer">This is mentioned in the documentation here.</a></p>\n'}, {'answer_id': 9386143, 'author': 'mustafaturan', 'author_id': 960702, 'author_profile': 'https://Stackoverflow.com/users/960702', 'pm_score': 6, 'selected': False, 'text': '<p>just generate a ssh key like:</p>\n\n<pre><code>ssh-keygen -t rsa -C "your_email@youremail.com"\n</code></pre>\n\n<p>copy the content of <code>~/.ssh/id_rsa.pub</code>\nand lastly add it to the remote machines <code>~/.ssh/authorized_keys</code></p>\n\n<p>make sure remote machine have the permissions <code>0700 for ~./ssh folder</code> and <code>0600 for ~/.ssh/authorized_keys</code></p>\n'}, {'answer_id': 13955428, 'author': 'KevinS', 'author_id': 905853, 'author_profile': 'https://Stackoverflow.com/users/905853', 'pm_score': 9, 'selected': False, 'text': '<p>Use <a href="http://sshpass.sourceforge.net/" rel="noreferrer">sshpass</a>:</p>\n\n<pre><code>sshpass -p "password" scp -r user@example.com:/some/remote/path /some/local/path\n</code></pre>\n\n<p>or so the password does not show in the bash history</p>\n\n<pre><code>sshpass -f "/path/to/passwordfile" scp -r user@example.com:/some/remote/path /some/local/path\n</code></pre>\n\n<p>The above copies contents of path from the remote host to your local. </p>\n\n<p><strong>Install :</strong></p>\n\n<ul>\n<li>ubuntu/debian\n\n<ul>\n<li><code>apt install sshpass</code></li>\n</ul></li>\n<li>centos/fedora\n\n<ul>\n<li><code>yum install sshpass</code></li>\n</ul></li>\n<li>mac w/ macports\n\n<ul>\n<li><code>port install sshpass</code></li>\n</ul></li>\n<li>mac w/ brew\n\n<ul>\n<li><code>brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb</code></li>\n</ul></li>\n</ul>\n'}, {'answer_id': 14361319, 'author': 'Prabhjeet', 'author_id': 1955593, 'author_profile': 'https://Stackoverflow.com/users/1955593', 'pm_score': 5, 'selected': False, 'text': '<p>You can use the \'expect\' script on unix/terminal</p>\n\n<p>For example create  \'test.exp\' :</p>\n\n<pre><code>#!/usr/bin/expect\n        spawn scp  /usr/bin/file.txt root@&lt;ServerLocation&gt;:/home\n        set pass "Your_Password"\n        expect {\n        password: {send "$pass\\r"; exp_continue}\n                  }\n</code></pre>\n\n<p>run the script </p>\n\n<pre><code>expect test.exp \n</code></pre>\n\n<p>I hope that helps. </p>\n'}, {'answer_id': 22007042, 'author': 'valk', 'author_id': 400745, 'author_profile': 'https://Stackoverflow.com/users/400745', 'pm_score': -1, 'selected': False, 'text': '<p>I found this really helpful answer <a href="https://askubuntu.com/questions/44059/progress-bar-in-scp-command">here</a>.</p>\n\n<pre><code>rsync -r -v --progress -e ssh user@remote-system:/address/to/remote/file /home/user/\n</code></pre>\n\n<p>Not only you can pass there the password, but also it will show the progress bar when copying. Really awesome.</p>\n'}, {'answer_id': 22551406, 'author': 'Kevin Chui', 'author_id': 3230321, 'author_profile': 'https://Stackoverflow.com/users/3230321', 'pm_score': 0, 'selected': False, 'text': '<ol>\n<li><p>make sure you have "expect" tool before, if not, do it</p>\n\n<p><code># apt-get install expect</code></p></li>\n<li><p>create the a script file with following content. (# vi /root/scriptfile)</p>\n\n<p><code>spawn scp /path_from/file_name user_name_here@to_host_name:/path_to</code></p>\n\n<p><code>expect "password:"</code></p>\n\n<p><code>send put_password_here\\n;</code></p>\n\n<p><code>interact</code></p></li>\n<li><p>execute the script file with "expect" tool</p>\n\n<p><code># expect /root/scriptfile</code></p></li>\n</ol>\n'}, {'answer_id': 30221357, 'author': 'galushka', 'author_id': 4896860, 'author_profile': 'https://Stackoverflow.com/users/4896860', 'pm_score': 3, 'selected': False, 'text': '<p>Nobody mentioned it, but <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html" rel="nofollow noreferrer">Putty scp</a> (pscp) has a -pw option for password.</p>\n\n<p>Documentation can be found here:  <a href="https://the.earth.li/~sgtatham/putty/0.67/htmldoc/Chapter5.html#pscp" rel="nofollow noreferrer">https://the.earth.li/~sgtatham/putty/0.67/htmldoc/Chapter5.html#pscp</a></p>\n'}, {'answer_id': 40738243, 'author': 'Shen JI', 'author_id': 6477230, 'author_profile': 'https://Stackoverflow.com/users/6477230', 'pm_score': 3, 'selected': False, 'text': '<p>You may use <code>ssh-copy-id</code> to add ssh key:</p>\n\n<pre><code>$which ssh-copy-id #check whether it exists\n</code></pre>\n\n<p>If exists:</p>\n\n<pre><code>ssh-copy-id  "user@remote-system"\n</code></pre>\n'}, {'answer_id': 46843996, 'author': 'hi15', 'author_id': 3587282, 'author_profile': 'https://Stackoverflow.com/users/3587282', 'pm_score': 3, 'selected': False, 'text': "<p>Once you set up <code>ssh-keygen</code> as explained above, you can do </p>\n\n<p><code>scp -i ~/.ssh/id_rsa /local/path/to/file remote@ip.com:/path/in/remote/server/</code></p>\n\n<p>If you want to lessen typing each time, you can modify your <code>.bash_profile</code> file and put</p>\n\n<pre><code>alias remote_scp='scp -i ~/.ssh/id_rsa /local/path/to/file remote@ip.com:/path/in/remote/server/\n</code></pre>\n\n<p>Then from your terminal do <code>source ~/.bash_profile</code>. Afterwards if you type <code>remote_scp</code> in your terminal it should run the <code>scp</code> command without password.</p>\n"}, {'answer_id': 47328516, 'author': 'MBerg', 'author_id': 8951145, 'author_profile': 'https://Stackoverflow.com/users/8951145', 'pm_score': 5, 'selected': False, 'text': '<p>curl can be used as a alternative to scp to copy a file and it supports a password on the commandline. </p>\n\n<pre><code>curl --insecure --user username:password -T /path/to/sourcefile sftp://desthost/path/\n</code></pre>\n'}, {'answer_id': 57807535, 'author': 'Yash', 'author_id': 9401270, 'author_profile': 'https://Stackoverflow.com/users/9401270', 'pm_score': -1, 'selected': False, 'text': '<p>All the solutions mentioned above can work only if you the app installed or you should have the admin rights to install except or sshpass.</p>\n\n<p>I found this very useful link to simply start the scp in Background.</p>\n\n<pre><code>$ nohup scp file_to_copy user@server:/path/to/copy/the/file &gt; nohup.out 2&gt;&amp;1\n</code></pre>\n\n<p><a href="https://charmyin.github.io/scp/2014/10/07/run-scp-in-background/" rel="nofollow noreferrer">https://charmyin.github.io/scp/2014/10/07/run-scp-in-background/</a></p>\n'}, {'answer_id': 59252427, 'author': 'JohnMudd', 'author_id': 487992, 'author_profile': 'https://Stackoverflow.com/users/487992', 'pm_score': 2, 'selected': False, 'text': '<p>Here\'s a poor man\'s Linux/Python/Expect-like example based on this blog post: <a href="https://blog.ropnop.com/upgrading-simple-shells-to-fully-interactive-ttys/#method1pythonptymodule" rel="nofollow noreferrer">Upgrading simple shells to fully interactive \nTTYs</a>. I needed this for old machines where I can\'t install Expect or add modules to Python.</p>\n\n<p>Code:</p>\n\n<pre><code>(\n    echo \'scp jmudd@mysite.com:./install.sh .\'\n    sleep 5\n    echo \'scp-passwd\'\n    sleep 5\n    echo \'exit\'\n) |\n\npython -c \'import pty; pty.spawn("/usr/bin/bash")\'\n</code></pre>\n\n<p>Output:</p>\n\n<pre><code>scp jmudd@mysite.com:install.sh .\nbash-4.2$ scp jmudd@mysite.com:install.sh .\nPassword: \ninstall.sh                                 100%   15KB 236.2KB/s   00:00    \nbash-4.2$ exit\nexit\n</code></pre>\n'}, {'answer_id': 63877892, 'author': 'Finesse', 'author_id': 1118709, 'author_profile': 'https://Stackoverflow.com/users/1118709', 'pm_score': 2, 'selected': False, 'text': '<ol>\n<li>Make sure password authentication is enabled on the target server. If it runs Ubuntu, then open <code>/etc/ssh/sshd_config</code> on the server, find lines <code>PasswordAuthentication=no</code> and comment all them out (put <code>#</code> at the start of the line), save the file and run <code>sudo systemctl restart ssh</code> to apply the configuration. If there is no such line then you\'re done.</li>\n<li>Add <code>-o PreferredAuthentications=&quot;password&quot;</code> to your <code>scp</code> command, e.g.:\n<pre class="lang-sh prettyprint-override"><code>scp -o PreferredAuthentications=&quot;password&quot; /path/to/file user@server:/destination/directory\n</code></pre>\n</li>\n</ol>\n'}, {'answer_id': 65111598, 'author': 'Sachin Rastogi', 'author_id': 8230674, 'author_profile': 'https://Stackoverflow.com/users/8230674', 'pm_score': -1, 'selected': False, 'text': '<p>In case if you observe a strict host key check error then use <code>-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null</code> options.</p>\n<p>The complete example is as follows\n<code>sshpass -p &quot;password&quot; scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@domain-name.com:/tmp/from/psoutput /tmp/to/psoutput</code></p>\n'}, {'answer_id': 67064479, 'author': 'Sudheer Mareddy', 'author_id': 8228506, 'author_profile': 'https://Stackoverflow.com/users/8228506', 'pm_score': 0, 'selected': False, 'text': '<p>copy files from one server to other server ( on scripts)</p>\n<p>Install putty on ubuntu or other Linux machines. putty comes with pscp. we can copy files with pscp.</p>\n<pre><code>apt-get update\napt-get install putty \necho n |  pscp -pw &quot;Password@1234&quot; -r user_name@source_server_IP:/copy_file_path/files /path_to_copy/files\n</code></pre>\n<p>For more options see pscp help.</p>\n'}, {'answer_id': 68999260, 'author': 'vishal sahu', 'author_id': 6306383, 'author_profile': 'https://Stackoverflow.com/users/6306383', 'pm_score': -1, 'selected': False, 'text': '<p>You can use below steps. This works for me!</p>\n<p><strong>Step1-</strong>\ncreate a normal file suppose &quot;<strong>fileWithScpPassword</strong>&quot; which contains the ssh password for the destination server.</p>\n<p><strong>Step2-</strong> use <strong>sshpaas -f</strong> followed by password file name and then normal scp command.</p>\n<p><strong>sshpass -f &quot;fileWithScpPassword&quot; scp  /filePathToUpload user@ip:/destinationPath/</strong></p>\n'}, {'answer_id': 72019549, 'author': 'Rakesh Raushan', 'author_id': 8422153, 'author_profile': 'https://Stackoverflow.com/users/8422153', 'pm_score': -1, 'selected': False, 'text': "<p>One easy way I do this:</p>\n<p>Use the same scp cmd as you use with ssh keys i.e</p>\n<p><code>scp -C -i &lt;path_to opens sshkey&gt; &lt;'local file_path'&gt; user@&lt;ip_address_VM&gt;: &lt;'remote file_path’&gt;</code></p>\n<p>for transferring file from local to remote</p>\n<p>but instead of providing the correct &lt;path_to_opensshkey&gt;, use some garbage path. Due to wrong key path you will be asked for password instead and you can simply pass the password now to get the work done!</p>\n"}]
['https://Stackoverflow.com/questions/50096', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2992/']