我的 unix 系统的一个目录中有 n 个文件。有没有办法编写一个 shellscript,将所有这些文件通过 scp 传输到指定的远程系统。我将在脚本中指定密码,这样我就不必为每个文件输入密码。
14 回答
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/
assuming your private key is at ~/.ssh/id_rsa
and the files you want to send can be filtered with *.derp
To generate a public / private key pair :
$ ssh-keygen -t rsa
The above will generate 2 files, ~/.ssh/id_rsa
(private key) and ~/.ssh/id_rsa.pub
(public key)
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.
#!/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
你为什么不试试这个?
password="your password"
username="username"
Ip="<IP>"
sshpass -p "$password" scp /<PATH>/final.txt $username@$Ip:/root/<PATH>
you could also use rsync. It seems to work better for multiple files than scp IMHO.
rsync -avzh /path/to/dir/ user@remote:/path/to/remote/dir/
Update
You can use rsync via ssh by adding the '-e' switch:
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"
What about wildcards or multiple files?
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
有 2 种快速方法可以实现这一目标:
使用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
deb like os eg Ubuntu
)这将使您能够在没有密码提示的情况下自动上传文件
这是带有 .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#########";
试试lftp
lftp -u $user,$pass sftp://$host << --EOF--
cd $directory
put $srcfile
quit
--EOF--
该命令scp
可以像传统的 UNIX 一样使用cp
。所以如果你这样做:
scp -r myDirectory/ mylogin@host:TargetDirectory
将工作