如何使用 Bash 脚本将文件复制到远程计算机?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16585813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:27:03  来源:igfitidea点击:

How to copy files to remote computer using Bash script?

bashfilesystemscredentialsusername

提问by Alex1987

I have a script that needs to copy a file to a remote computer:

我有一个需要将文件复制到远程计算机的脚本:

cp -R "${DEST_FOLDER}" "${SRC_FOLDER}"

How can I do it when the remote computer requires user and password for access?

当远程计算机需要用户和密码才能访问时,我该怎么做?

How do I login to this computer with a bash script?

如何使用 bash 脚本登录到这台计算机?

Thanks

谢谢

回答by kamituel

Bash itself will not let you access remote host (obviously), but you could use SSH:

Bash 本身不会让您访问远程主机(显然),但您可以使用 SSH:

Step 1:On your local PC generate key to perform password-free authentication later

第一步:在本地PC上生成密钥,稍后进行免密码认证

$ ssh-keygen

It will ask you to enter passphrase. If you want your bash script to be fully non-interactive, you can opt not to use any password.

它会要求您输入密码。如果您希望 bash 脚本完全非交互式,您可以选择不使用任何密码。

Step 2:Copy you public key to the remote host:

第 2 步:将您的公钥复制到远程主机:

$ ssh-copy-id -i ~/.ssh/id_rsa.pub user@remote-host

Step 3:Use scpto copy files:

第3步:使用scp复制文件:

$ scp -r local_file user@remote-host:/remote_dest_dir/