如何在shell脚本中从远程服务器复制文件?
我需要编写脚本以在服务器后端自动将远程服务器文件复制到此处。如下所示:
#!/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您改用公钥身份验证(在客户端上使用
ssh-keygen
生成公钥/私钥对,默认添加公钥(.ssh/id.pub
) 服务器上.ssh/authorized_keys
的密钥 — 请参阅手册页)。您可以:在后一种情况下,我建议您将密钥限制为特定命令。我不确定如何为 scp 设置命令,但是
等效的。您只需在
.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:ssh-agent
, orIn 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
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.send "xxxx\r"
set timeout 3000
实际上并没有暂停 - 它将超时值设置为 3000 秒( 50 分钟)。如果您需要暂停,请sleep 3
。exp_internal 1
调试您的模式。send "xxxx\r"
set timeout 3000
does not actually pause -- it sets the timeout value to 3000 seconds (50 minutes). If you need to pause,sleep 3
.exp_internal 1
to debug your patterns.