如何使用SSH文件传输协议将文件从本地计算机复制到服务器?

发布于 2024-09-01 14:33:54 字数 395 浏览 3 评论 0原文

我有一台本地使用的 Ubuntu 9.10 台式机。我正在托管提供商上设置服务器。该服务器将运行 Ubuntu 服务器 LTS 的最小版本(仅 LAMP 和电子邮件服务器,没有 GUI)。

我想编写一个脚本(安排为 cron 作业),它允许我将本地文件上传到服务器上。出于安全原因,我想使用 [SFTP][1]。

我是 shell 脚本的新手 - 但我想 shell 脚本是执行此操作的方法(除非我弄错了)。

任何人都可以向我提供有关如何编写此类脚本的初始指示,以便安全地将本地文件上传到服务器吗?

理想情况下,我想在传输之前压缩文件(以节省带宽)

[1]:http://SSH 文件传输协议

I have an Ubuntu 9.10 desktop machine which I use locally. I am setting up a server on a hosting provider. The server will run a very minimal version of Ubuntu server LTS (only LAMP and email server no GUI).

I want to write a script (scheduled as a cron job) that will allow me to upload local files onto the server. I want to use [SFTP][1], for security reasons.

I am new to shell scripting - but I guess shell scripting is the way to do this (unless I am mistaken).

Can anyone provide me with the initial pointers on how to go about writing such a script, in order to SECURELY upload local files to the server?

Ideally, I would like to compress the files before the transfer (to save on bandwidth)

[1]: http://SSH file transfer protocol

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二货你真萌 2024-09-08 14:33:54

无论您将其拼写为“SECURE”还是“secure”,我们都无法读懂您的想法并告诉您要防范什么。所以我会给出一个基本的食谱并告诉你它的好处。这可能应该全部转移到超级用户。

  1. 学习基本的 bourne shell。相关教程无法融入 stackoverflow 答案中。
  2. 运行 ssh-keygen 来生成 ssh 密钥对。由于您想从 cron 运行 scp,因此您不能使用密码,AFAIK。这意味着您必须非常确定要复制的机器不会受到入侵者的攻击。
  3. 将公钥从 ssh 密钥对复制到目标计算机上的 .ssh/authorized_keys 中。
  4. 通过运行“ssh target-machine”并成功登录无密码来证明这是有效的。

现在,您可以创建一个使用 scp 命令执行实际复制的 shell 脚本。从以下位置开始:

#!/bin/sh
scp PATHNAME_OVER_HERE target-host:/PATHNAME_OVER_THERE

这可以防止基本密码间谍活动以及通过 telnet 随机连接到目标主机的情况。如果源系统不安全,它也不安全,我不能保证 ssh 协议的安全性,尽管它确实被广泛使用。

Whether you spell it 'SECURE' or 'secure,' we can't read your mind and tell what you want to secure against. So I'll give a basic recipe and tell you what it's good for. This probably should all move to superuser.

  1. learn basic bourne shell. A tutorial for that can't fit into a stackoverflow answer.
  2. run ssh-keygen to make an ssh key pair. Since you want to run scp from cron, you can't use a passphrase, AFAIK. Which means that you have to be quite sure that the machine you are copying from is safe from intruders.
  3. copy the public key from the ssh key pair to your .ssh/authorized_keys on the target machine.
  4. prove that this works by running 'ssh target-machine' and successfully logging on sans-password.

Now, you can make a shell script that uses the scp command to do the actual copies. Start from:

#!/bin/sh
scp PATHNAME_OVER_HERE target-host:/PATHNAME_OVER_THERE

This is secure against basic password spying and against randoms connecting to target-host with telnet. It is not secure if the source system is not secure, and I cannot vouch for the security of ssh protocol, though it certainly is widely used.

So尛奶瓶 2024-09-08 14:33:54

如何使用SSH文件传输协议将文件从本地机器复制到服务器?

使用scp

我想在 cron 作业中完成它。

在 cron 作业中使用 scp 的主要问题是:从哪里获取凭据?
将密码明文保存并不是一个好主意。更好的想法是在您的计算机上运行一个 ssh-agent 进程。要找到合适的ssh-agent,您可以运行此脚本:

#!/bin/sh

for i in $SSH_AUTH_SOCK /tmp/ssh*/agent*
do
  if [ -r "$i" -a -w "$i" ]; then
    case `SSH_AUTH_SOCK="$i" ssh-add -l` in
      *:*:*) echo "$i"; exit 0 ;;
    esac
  fi
done

exit 1

如果脚本成功,您将获得一个可以放入SSH_AUTH_SOCK环境中的值运行 scp 之前的变量。

当您启动客户端时,您应该通过启动 ssh-agent 并运行 ssh-add 来提供您的凭据。

How to copy files from local machine to server using SSH file transfer protocol?

Use scp.

I want to do it in a cron job.

The main issue with using scp in a cron job is this: where do you get your credentials?
Putting your password in the clear is not a good idea. A better idea is to have an ssh-agent process running on your machine. To find an appropriate ssh-agent you can run this script:

#!/bin/sh

for i in $SSH_AUTH_SOCK /tmp/ssh*/agent*
do
  if [ -r "$i" -a -w "$i" ]; then
    case `SSH_AUTH_SOCK="$i" ssh-add -l` in
      *:*:*) echo "$i"; exit 0 ;;
    esac
  fi
done

exit 1

If the script succeeds, you get a value you can put into the SSH_AUTH_SOCK environment variable before running scp.

When you bring up the client, you should present your credentials by launching ssh-agent and running ssh-add.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文