【发布时间】:2010-11-23 17:00:04
【问题描述】:
我的 unix 系统上的一个目录中有 n 个文件。有没有办法编写一个 shellscript,将所有这些文件通过 scp 传输到指定的远程系统。我将在脚本中指定密码,这样我就不必为每个文件输入密码了。
【问题讨论】:
-
您能否告诉我在带有 rsync 的 shell 脚本中使用密码是否有效,或者您是否尝试过?谢谢。
我的 unix 系统上的一个目录中有 n 个文件。有没有办法编写一个 shellscript,将所有这些文件通过 scp 传输到指定的远程系统。我将在脚本中指定密码,这样我就不必为每个文件输入密码了。
【问题讨论】:
不要在 shell 脚本中硬编码密码,而是使用 SSH 密钥,它更简单、更安全。
$ scp -i ~/.ssh/id_rsa *.derp devops@myserver.org:/path/to/target/directory/
假设你的私钥在~/.ssh/id_rsa,你想发送的文件可以用*.derp过滤
生成公钥/私钥对:
$ ssh-keygen -t rsa
上面会生成2个文件,~/.ssh/id_rsa(私钥)和~/.ssh/id_rsa.pub(公钥)
设置 SSH 密钥以供使用(一次性任务):
复制~/.ssh/id_rsa.pub 的内容并在myserver.org 服务器中粘贴~devops/.ssh/authorized_keys 的新行。如果~devops/.ssh/authorized_keys 不存在,请随意创建它。
here 提供了清晰的操作指南。
【讨论】:
#!/usr/bin/expect -f
# connect via scp
spawn scp "user@example.com:/home/santhosh/file.dmp" /u01/dumps/file.dmp
#######################
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send "PASSWORD\r"
}
}
interact
http://blogs.oracle.com/SanthoshK/entry/automate_linux_scp_command
【讨论】:
expect 将匹配整个输出,因此第一条规则将始终匹配。如果你交换两个规则的顺序,你会得到想要的行为。
你为什么不试试这个?
password="your password"
username="username"
Ip="<IP>"
sshpass -p "$password" scp /<PATH>/final.txt $username@$Ip:/root/<PATH>
【讨论】:
你也可以使用 rsync。它似乎比 scp 恕我直言更适用于多个文件。
rsync -avzh /path/to/dir/user@remote:/path/to/remote/dir/
更新
您可以通过添加“-e”开关来通过 ssh 使用 rsync:
rsync -avzh -e ssh /path/do/dir/user@remote:/path/to/remote/dir/
【讨论】:
#!/usr/bin/expect -f
spawn scp -r BASE.zip abhishek@192.168.1.115:/tmp
expect "password:"
send "wifinetworks\r"
expect "*\r"
expect "\r"
【讨论】:
通配符或多个文件呢?
scp file1 file2 more-files* user@remote:/some/dir/
【讨论】:
rsync 是一个行为方式与 rcp 大致相同的程序,但有更多的选项和用途 rsync 远程更新协议可在目标文件正在传输时大大加快文件传输速度 更新了。
rsync 远程更新协议允许 rsync 仅传输两组文件之间的差异 通过网络连接,使用技术中描述的有效校验和搜索算法 此包裹随附的报告。
#!/usr/bin/expect -f
spawn rsync -a -e ssh username@192.168.1.123:/cool/cool1/* /tmp/cool/
expect "password:"
send "cool\r"
expect "*\r"
expect "\r"
【讨论】:
如果您可以在每次运行脚本时输入一次密码,您可以使用 SSH 主连接轻松完成。
#!/usr/bin/env bash
USER_AT_HOST="user@host" # use "$1@$2" here if you like
SSHSOCKET=~/".ssh/$USER_AT_HOST"
# This is the only time you have to enter the password:
# Open master connection:
ssh -M -f -N -o ControlPath="$SSHSOCKET" "$USER_AT_HOST"
# These do not prompt for your password:
scp -o ControlPath="$SSHSOCKET" file1.xy "$USER_AT_HOST":remotefile1.xy
scp -o ControlPath="$SSHSOCKET" file2.xy "$USER_AT_HOST":remotefile2.xy
# You can also use the flag for normal ssh:
ssh -o ControlPath="$SSHSOCKET" "$USER_AT_HOST" "echo hello"
ssh -o ControlPath="$SSHSOCKET" "$USER_AT_HOST" "echo world"
# Close master connection:
ssh -S "$SSHSOCKET" -O exit "$USER_AT_HOST"
【讨论】:
您只能使用 ssh 公钥/私钥来执行此操作。或者使用可以设置密码的腻子。 scp 不支持在命令行输入密码。
您可以在此处找到有关公钥/私钥的说明: http://www.softpanorama.org/Net/Application_layer/SSH/scp.shtml
【讨论】:
这将起作用:
#!/usr/bin/expect -f
spawn scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no file1 file2 file3 user@host:/path/
expect "password:"
send "xyz123\r"
expect "*\r"
expect "\r"
interact
【讨论】:
这是带有 .pem 密钥文件的 SCP 的 bash 代码。 只需将其保存到 script.sh 文件,然后使用 'sh script.sh' 运行
享受
#!/bin/bash
#Error function
function die(){
echo "$1"
exit 1
}
Host=ec2-53-298-45-63.us-west-1.compute.amazonaws.com
User=ubuntu
#Directory at sent destination
SendDirectory=scp
#File to send at host
FileName=filetosend.txt
#Key file
Key=MyKeyFile.pem
echo "Aperture in Process...";
#The code that will send your file scp
scp -i $Key $FileName $User@$Host:$SendDirectory || \
die "@@@@@@@Houston we have problem"
echo "########Aperture Complete#########";
【讨论】:
有两种快速的方法来实现这一点:
使用 scp
#!/usr/bin/env bash
password="YOURPASSWORD"
username="YOURUSERNAME"
dir_origin="YOURSOURCEDIRECTORY"
dir_destination="REMOTEDESTINATION"
Ip="SERVERIP"
echo "Uploading files to remote server...."
sshpass -p "$password" scp -rC $dir_origin $username@$Ip:$dir_destination
echo "File upload to remote server completed! ;)"
使用 rsync
#!/usr/bin/env bash
password="YOURPASSWORD"
username="YOURUSERNAME"
dir_origin="YOURSOURCEDIRECTORY"
dir_destination="REMOTEDESTINATION"
Ip="SERVERIP"
echo "Uploading files to remote server...."
sshpass -p "$password" rsync -avzh $dir_origin $username@$Ip:$dir_destination
echo "File upload to remote server completed! ;)"
**注意:**您需要安装sshpass(例如,通过运行apt install sshpass for deb like
操作系统,例如Ubuntu),这将使您能够在没有密码提示的情况下自动上传文件
【讨论】:
试试lftp
lftp -u $user,$pass sftp://$host << --EOF--
cd $directory
put $srcfile
quit
--EOF--
【讨论】:
命令scp 可以像传统的UNIX cp 一样使用。所以如果你这样做:
scp -r myDirectory/ mylogin@host:TargetDirectory
会起作用
【讨论】: