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

后端 未结 12 1147
旧巷少年郎
旧巷少年郎 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:33

    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.

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

    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.

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

    Use "sshpass"!

    #!/bin/bash
    sshpass -p "password" scp -r /some/local/path user@example.com:/some/remote/path
    
    0 讨论(0)
  • 2020-12-16 19:37

    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

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

    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.

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

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

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