쉘 스크립트를 사용하여 scp 파일 전송 자동화
내 유닉스 시스템의 디렉토리에 n 개의 파일이 있습니다. scp를 통해 모든 파일을 지정된 원격 시스템으로 전송하는 쉘 스크립트를 작성하는 방법이 있습니까? 각 파일에 대해 입력 할 필요가 없도록 스크립트 내에 암호를 지정하겠습니다.
쉘 스크립트에서 비밀번호를 하드 코딩하는 대신 SSH 키를 사용하면 더 쉽고 안전합니다.
$ scp -i ~/.ssh/id_rsa devops@myserver.org:/path/to/bin/*.derp .
개인 키가 ~/.ssh/id_rsa
공개 / 개인 키 쌍을 생성하려면 :
$ ssh-keygen -t rsa
위는 ~/.ssh/id_rsa
(개인 키)와 ~/.ssh/id_rsa.pub
(공개 키) 2 개의 파일을 생성합니다.
설정하려면 사용을위한 SSH 키 (한 번 작업) : 복사의 내용 ~/.ssh/id_rsa.pub
과의 새로운 라인에 붙여 넣기 ~devops/.ssh/authorized_keys
의 myserver.org
서버입니다. ~devops/.ssh/authorized_keys
존재하지 않는 경우 자유롭게 만드십시오.
#!/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
rsync를 사용할 수도 있습니다. scp IMHO보다 여러 파일에서 더 잘 작동하는 것 같습니다.
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 /
이걸 해보지 그래?
password="your password"
username="username"
Ip="<IP>"
sshpass -p "$password" scp /<PATH>/final.txt $username@$Ip:/root/<PATH>
#!/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 원격 업데이트 프로토콜을 사용하는 프로그램입니다.
The rsync remote-update protocol allows rsync to transfer just the differences between two sets of files across the network connection, using an efficient checksum-search algorithm described in the technical report that accompanies this package.
Copying folder from one location to another
#!/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"
If you are ok with entering your password once for every run of the script, you can do so easily using an SSH master connection.
#!/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"
You can do it with ssh public/private keys only. Or use putty in which you can set the password. scp doesn't support giving password in command line.
You can find the instructions for public/private keys here: http://www.softpanorama.org/Net/Application_layer/SSH/scp.shtml
here's bash code for SCP with a .pem key file. Just save it to a script.sh file then run with 'sh script.sh'
Enjoy
#!/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#########";
This will work:
#!/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
Try lftp
lftp -u $user,$pass sftp://$host << --EOF--
cd $directory
put $srcfile
quit
--EOF--
The command scp
can be used like a traditional UNIX cp
. SO if you do :
scp -r myDirectory/ mylogin@host:TargetDirectory
will work
참고URL : https://stackoverflow.com/questions/1346509/automate-scp-file-transfer-using-a-shell-script
'program tip' 카테고리의 다른 글
C의`free`가 해제 할 바이트 수를 사용하지 않는 이유는 무엇입니까? (0) | 2020.09.24 |
---|---|
int 값과 관련된 열거 형 가져 오기 (0) | 2020.09.24 |
neo4j 1.8에서 모든 노드 및 관계 삭제 (0) | 2020.09.23 |
창 가장자리와 관련하여 팝 오버의 X 위치를 기준으로 부트 스트랩 팝 오버의 위치를 변경 하시겠습니까? (0) | 2020.09.23 |
angular-cli로 하나의 테스트 사양 만 실행하는 방법 (0) | 2020.09.23 |