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

后端 未结 12 1177
旧巷少年郎
旧巷少年郎 2020-12-16 19:21

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.<

相关标签:
12条回答
  • 2020-12-16 19:40

    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

    0 讨论(0)
  • 2020-12-16 19:49

    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.

    0 讨论(0)
  • 2020-12-16 19:50

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

    0 讨论(0)
  • 2020-12-16 19:51

    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.

    0 讨论(0)
  • 2020-12-16 19:51

    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.

    0 讨论(0)
  • 2020-12-16 19:55

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

    0 讨论(0)
提交回复
热议问题