使用shell脚本自动执行scp文件传输

问题描述 投票:79回答:13

我的unix系统上的目录中有n个文件。有没有办法编写一个shellcript,将所有这些文件通过scp传输到指定的远程系统。我将在脚本中指定密码,这样我就不必为每个文件输入密码。

shell scp
13个回答
83
投票

而不是在shell脚本中硬编码密码,使用SSH密钥,它更容易和安全。

$ scp -i ~/.ssh/id_rsa [email protected]:/path/to/bin/*.derp .

假设你的私钥是在~/.ssh/id_rsa

要生成公钥/私钥对:

$ ssh-keygen -t rsa

以上将生成2个文件,~/.ssh/id_rsa(私钥)和~/.ssh/id_rsa.pub(公钥)

设置SSH密钥以供使用(一次性任务):复制~/.ssh/id_rsa.pub的内容并粘贴到~devops/.ssh/authorized_keys服务器中新的myserver.org行。如果~devops/.ssh/authorized_keys不存在,请随意创建它。

有一个清晰的操作指南可用here


1
投票

这将有效:

#!/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

1
投票

这是带有.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#########";

0
投票

试试lftp

lftp -u $user,$pass sftp://$host << --EOF--

cd $directory

put $srcfile

quit

--EOF--

-1
投票

命令scp可以像传统的UNIX cp一样使用。如果你这样做:

scp -r myDirectory/ mylogin@host:TargetDirectory

将工作


40
投票
#!/usr/bin/expect -f

# connect via scp
spawn scp "[email protected]:/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


15
投票

你也可以使用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/

15
投票

你为什么不试试这个?

password="your password"
username="username"
Ip="<IP>"
sshpass -p "$password" scp /<PATH>/final.txt $username@$Ip:/root/<PATH>

9
投票
#!/usr/bin/expect -f
spawn scp -r BASE.zip [email protected]:/tmp
expect "password:"
send "wifinetworks\r"
expect "*\r"
expect "\r"

5
投票

通配符或多个文件怎么样?

scp file1 file2 more-files* user@remote:/some/dir/

4
投票

rsync是一个程序,其行为与rcp的行为大致相同,但是有更多选项,并且在更新目标文件时使用rsync远程更新协议可以大大加快文件传输速度。

rsync远程更新协议允许rsync使用此软件包随附的技术报告中描述的高效校验和搜索算法,仅通过网络连接传输两组文件之间的差异。


将文件夹从一个位置复制到另一个位置

   #!/usr/bin/expect -f   
   spawn rsync -a -e ssh [email protected]:/cool/cool1/* /tmp/cool/   
   expect "password:"   
   send "cool\r"   
   expect "*\r"   
   expect "\r"  

3
投票

如果您可以在每次运行脚本时输入一次密码,则可以使用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"

2
投票

您只能使用ssh公钥/私钥。或者使用putty来设置密码。 scp不支持在命令行中输入密码。

你可以在这里找到公钥/私钥的说明:http://www.softpanorama.org/Net/Application_layer/SSH/scp.shtml

© www.soinside.com 2019 - 2024. All rights reserved.