如何在shell脚本中从远程服务器复制文件?

发布于 2024-10-25 01:12:46 字数 670 浏览 3 评论 0原文

我需要编写脚本以在服务器后端自动将远程服务器文件复制到此处。如下所示:

#!/usr/bin/expect -f   
  spawn /usr/local/bin/scpdata.sh
  set timeout 3000
  expect "[email protected]'s password:"
  set timeout 3000
  send "xxxx"
  set timeout 3000
  send "exit\r"
  expect eof

scpdata.sh 文件

#!/bin/bash
  scp [email protected]:/tmp/11-03-15_03:00:01.tar.gz /tmp

但这不起作用,问题出在哪里以及如何做?请帮忙

I need write script for copy remote server files to here automatically on server backend. something like below:

#!/usr/bin/expect -f   
  spawn /usr/local/bin/scpdata.sh
  set timeout 3000
  expect "[email protected]'s password:"
  set timeout 3000
  send "xxxx"
  set timeout 3000
  send "exit\r"
  expect eof

scpdata.sh file

#!/bin/bash
  scp [email protected]:/tmp/11-03-15_03:00:01.tar.gz /tmp

But this not work, where is problem and how to do it? Please help

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

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

发布评论

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

评论(2

‖放下 2024-11-01 01:12:46

我建议您改用公钥身份验证(在客户端上使用 ssh-keygen 生成公钥/私钥对,默认添加公钥(.ssh/id.pub ) 服务器上 .ssh/authorized_keys 的密钥 — 请参阅手册页)。您可以:

  • 使用 ssh-agent 提前提供密钥的密码,或者
  • 使用没有密码的密钥。

在后一种情况下,我建议您将密钥限制为特定命令。我不确定如何为 scp 设置命令,但是

ssh [email protected] 'cat /tmp/11-03-15_03:00:01.tar.gz' > /tmp/11-03-15_03:00:01.tar.gz

等效的。您只需在 .ssh/authorized_keys 中的密钥前面写入 command="命令" 即可。比 ssh 服务器,当使用此密钥授权时,无论 ssh 命令行上给出什么,都将始终运行指定的命令。这限制了攻击者在访问您的无密码密钥时可能造成的损害。

如果您需要改变文件名,则需要在服务器端编写一个脚本,该脚本将提取 $SSH_ORIGINAL_COMMAND 的名称(这就是服务器端脚本获取任何内容的地方)在 ssh 命令行上给出),检查它是否是允许的文件之一并对其进行cat。

I'd suggest you use public key authentication instead (generate public/private keypair with ssh-keygen on the client, add the public (.ssh/id.pub by default) key to .ssh/authorized_keys on the server—see the man page). Than you can either:

  • Give the passphrase for the key in advance using ssh-agent, or
  • use a key without passphrase.

In the later case I suggest you limit the key to a particular command. I am not sure how to set a command for scp, but

ssh [email protected] 'cat /tmp/11-03-15_03:00:01.tar.gz' > /tmp/11-03-15_03:00:01.tar.gz

is equivalent. You just write command="the command" in front of your key in .ssh/authorized_keys. Than the ssh server, when authorized with this key, will always run specified command no matter what was given on ssh command line. This limits the damage an attacker could do if they got access to your passphrase-less key.

If you need the name of file to get to vary, you will need to write a script on the server side, that will pull out the name of $SSH_ORIGINAL_COMMAND (that's where the server-side script gets whatever was given on ssh command-line), check that it's one of the permitted files and cat it.

柒夜笙歌凉 2024-11-01 01:12:46
  1. 发送密码时,您永远不会“按 Enter”: send "xxxx\r"
  2. set timeout 3000 实际上并没有暂停 - 它将超时值设置为 3000 秒( 50 分钟)。如果您需要暂停,请sleep 3
    • 如果您的预期模式正确,您几乎不需要显式睡眠。使用 exp_internal 1 调试您的模式。
  3. 设置 ssh 密钥,您根本不需要 Expect 脚本。
  1. You never "hit enter" when you send the password: send "xxxx\r"
  2. set timeout 3000 does not actually pause -- it sets the timeout value to 3000 seconds (50 minutes). If you need to pause, sleep 3.
    • If your expect patterns are correct, you almost never need to explicitly sleep. Use exp_internal 1 to debug your patterns.
  3. Set up ssh keys and you don't need the expect script at all.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文