12

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

derrdji
  • 12,661
  • 21
  • 68
  • 78

12 Answers12

17

Use "sshpass"!

#!/bin/bash
sshpass -p "password" scp -r /some/local/path user@example.com:/some/remote/path
KevinS
  • 8,087
  • 2
  • 17
  • 9
11

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

BZ.
  • 1,928
  • 2
  • 17
  • 26
4

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? ;-)

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
4

Consider using keys, or an external library.

I don't think it's possible otherwise (I hope I'm not wrong), as it imposes automatic brute force intrusion and sniffing of passwords.

There are libraries that can do what you want (use the SFTP protocol, not calling scp), such as libssh.

Again, I highly recommend keys.

LiraNuna
  • 64,916
  • 15
  • 117
  • 140
3

I agree that you should use keys. But expect can automate the interactive aspect of the process IF you want to hardcode your password in a plain-text script file.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
2

Something like this - http://code.google.com/p/enchanter/ ?

cms
  • 5,864
  • 2
  • 28
  • 31
1

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

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • Of multiple solutions in multiple answers, this is the only one worked for me. Copied the contents of my `id_rsa.pub` file to the remote's `~/.ssh/authorized_keys` directory and now I can `scp -i` and no manual password is required! – ysap Jan 28 '22 at 09:21
  • And as a side bonus - no manual password is required for the `ssh` sessions as well (which is kind'a the same thing, but was a nice surprise). When logging in to a remote machine multiple times a day, this is a time saver. – ysap Jan 28 '22 at 09:25
  • I just found out that `scp` works w/o the need for the `-i identityfile`. Looks like ssh takes care of the key matching once both sides are configured correctly. I guess ssh looks at the local `~/.ssh` directory for `id_rsa.pub` by default?? – ysap Jan 28 '22 at 20:30
1

Once you set up ssh-keygen as explained here, you can do

scp -i ~/.ssh/id_rsa /local/path/to/file remote@ip.com:/path/in/remote/server/

where id_rsa is the local key generated in the ssh-keygen setup.

If you want to lessen typing each time, you can modify your .bash_profile file and put

alias remote_scp='scp -i ~/.ssh/id_rsa /local/path/to/file remote@ip.com:/path/in/remote/server/

Then from your terminal do source ~/.bash_profile. Afterwards if you type remote_scp in your terminal it should run the scp command without password.

hi15
  • 2,113
  • 6
  • 28
  • 51
0

If you can mount the directory from a Windows machine (e.g. via AFS, NFS or SMB which my university's Windows labs all did), you can use pscp with the -pw switch.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
0

Why not just use the "-r" option to copy it recursively? Or use rsync instead?

You could also use public key authentication, which requires no help on their end as long as you have an actual user account. See this

kenm
  • 23,127
  • 2
  • 43
  • 62
0

As many have already said that using ssh keys would be the safest and best way. If anyone else is still wondering around and searching for help, in Ubuntu help there is a fast and straight forward way to use ssh keys.

Jonathan Cast
  • 4,569
  • 19
  • 34
Karls
  • 1
0

I found no way to do this on our systems automatically - I ssh into one device, and then ssh to the next device, and on both, I cannot install any other software. So I can only use existing shell script commands. I can either copy all the files of a type with, such as *.png, or one by one in my script file, having to enter the password for each individual file. The one thing I did that was helpful was to type the password into a file, cut it, and be able to paste it to each prompt for each file.

Janin
  • 292
  • 1
  • 2
  • 7