使用 shell 脚本自动传输 scp 文件 [英] Automate scp file transfer using a shell script
问题描述
我的 unix 系统的一个目录中有一些文件.有没有办法编写一个 shellscript,通过 scp 将所有这些文件传输到指定的远程系统.我将在脚本中指定密码,这样我就不必为每个文件都输入密码.
I have some n number of files in a directory on my unix system. Is there a way to write a shellscript that will transfer all those files via scp to a specified remote system. I'll specify the password within the script, so that I don't have to enter it for each file.
推荐答案
使用 SSH 密钥代替在 shell 脚本中硬编码密码,它更容易和安全.
Instead of hardcoding password in a shell script, use SSH keys, its easier and secure.
$ scp -i ~/.ssh/id_rsa *.derp devops@myserver.org:/path/to/target/directory/
假设你的私钥在 ~/.ssh/id_rsa
并且你想发送的文件可以用 *.derp
assuming your private key is at ~/.ssh/id_rsa
and the files you want to send can be filtered with *.derp
生成公钥/私钥对:
$ ssh-keygen -t rsa
上面会生成2个文件,~/.ssh/id_rsa
(私钥)和~/.ssh/id_rsa.pub
(公钥)
The above will generate 2 files, ~/.ssh/id_rsa
(private key) and ~/.ssh/id_rsa.pub
(public key)
设置 SSH 密钥以供使用(一次性任务):复制~/.ssh/id_rsa.pub
的内容,在myserver.org
中新粘贴一行~devops/.ssh/authorized_keys
> 服务器.如果 ~devops/.ssh/authorized_keys
不存在,请随意创建.
To setup the SSH keys for usage (one time task) :
Copy the contents of ~/.ssh/id_rsa.pub
and paste in a new line of ~devops/.ssh/authorized_keys
in myserver.org
server. If ~devops/.ssh/authorized_keys
doesn't exist, feel free to create it.
在此处提供了清晰的操作指南.
A lucid how-to guide is available here.
这篇关于使用 shell 脚本自动传输 scp 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!