Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to respond to password prompt when using SCP in a shell script?

First of all, I am well aware of that there are many of questions regarding this topic. I have read them, but still could figure out an appropriate answer for my situation.

I would like to scp the entire ~/cs###/assign1 dir from local to school home dir with a shell script. My question is, is there a way in my script to wait for the password prompt, and then simulate key board event to 'type' in my password?


here is a really detailed guide of how to set up the key

like image 810
derrdji Avatar asked Sep 22 '09 20:09

derrdji


People also ask

Can you pass password to scp command?

The SCP command allows you to copy a file or directory from a local system to a remote system, from a remote system to a local system, or between two remote systems from your local system. When you use the SCP command, you will need to provide a remote user's password before transferring a file.

How do I pass a scp password in Powershell?

Step1- create a normal file suppose "fileWithScpPassword" which contains the ssh password for the destination server. Step2- use sshpaas -f followed by password file name and then normal scp command. Save this answer.

How do I bypass scp password?

Summary: Using scp to make remote backups without a passwordCreate a public and private key pair. Install your public key on remote Unix and Linux servers. ssh into your remote servers without a password. Use ssh to run commands on your remote servers without using a password.

What is the correct syntax of the scp command?

The basic syntax is: scp user1@host1:filename user2@host2: where user1@host1 is the source from which you want to copy filename from and user2@host2 is the destination where you want the copy to go to.


4 Answers

You don't say which platform you are using at home and at school. Assuming Linux, Cygwin or OS/X you have several options:

  1. Public key authentication if it hasn't been turned off at the server
  2. ssh-agent and ssh-add to enter your password once per session

For option (1), you would

  1. generate a keypair at home using ssh-keygen, with no passphrase on the private key. Note that omitting a passphrase is probably not a good idea if other people use the same computer, but your objective was to get around having to type in the password.
  2. upload the PUBLIC key to your school account and place it in ~/.ssh/authorized_keys
  3. Use scp with the "-i identityfile" option, where identityfile is the full path to your private key. Or, add an entry to .ssh/config (see the man pages)

For the second option, ssh-agent allows you to cache your password in a local process one time per session. You set an expiration time

like image 184
Jim Garrison Avatar answered Oct 18 '22 20:10

Jim Garrison


Use "sshpass"!

#!/bin/bash
sshpass -p "password" scp -r /some/local/path [email protected]:/some/remote/path
like image 16
KevinS Avatar answered Oct 24 '22 01:10

KevinS


Are ssh keys not allowed? That would be a better solution.

like image 11
BZ. Avatar answered Oct 23 '22 23:10

BZ.


I don't think you can easily do that. What you can do is using public key authentication instead.

Something along these lines

ssh-keygen -t rsa
ssh school mkdir .ssh/
cat ~/.ssh/id_rsa.pub | ssh school "cat >>.ssh/authorized_keys"

(or dsa).

But hey, it's not serverfault, is it? ;-)

like image 4
Michael Krelin - hacker Avatar answered Oct 24 '22 00:10

Michael Krelin - hacker