linux系統之間文件傳輸

linux之間文件傳輸(轉) linux的scp命令mysql

linux 的 scp 命令 能夠 在 linux 之間複製 文件 和 目錄;linux

scp 命令sql

scp 能夠在 2個 linux 主機間複製文件;shell

命令基本格式:安全

scp [可選參數] file_source file_target

從 本地 複製到 遠程bash

  • 複製文件: * 命令格式: scp local_file remote_username@remote_ip:remote_folder 或者 scp local_file remote_username@remote_ip:remote_file 或者 scp local_file remote_ip:remote_folder 或者 scp local_file remote_ip:remote_fileoracle

    第1,2個指定了用戶名,命令執行後須要再輸入密碼,第1個僅指定了遠程的目錄,文件名字不變,第2個指定了文件名;
              第3,4個沒有指定用戶名,命令執行後須要輸入用戶名和密碼,第3個僅指定了遠程的目錄,文件名字不變,第4個指定了文件名;
      * 例子:
              scp /home/space/music/1.mp3 root@www.cumt.edu.cn:/home/root/others/music
              scp /home/space/music/1.mp3 root@www.cumt.edu.cn:/home/root/others/music/001.mp3
              scp /home/space/music/1.mp3 www.cumt.edu.cn:/home/root/others/music
              scp /home/space/music/1.mp3 www.cumt.edu.cn:/home/root/others/music/001.mp3

*** 複製目錄:**app

* 命令格式:
            scp -r local_folder remote_username@remote_ip:remote_folder
            或者
            scp -r local_folder remote_ip:remote_folder

            第1個指定了用戶名,命令執行後須要再輸入密碼;
            第2個沒有指定用戶名,命令執行後須要輸入用戶名和密碼;
    * 例子:
            scp -r /home/space/music/ root@www.cumt.edu.cn:/home/root/others/
            scp -r /home/space/music/ www.cumt.edu.cn:/home/root/others/

            上面 命令 將 本地 music 目錄 複製 到 遠程 others 目錄下,即複製後有 遠程 有 ../others/music/ 目錄

腳本執行效果: (1).遠程傳輸本地 /ora_exp/dmp/CWDB_RAMS_* 文件至 11.4.24.21 的 /ora_exp/dmp 目錄下。運維

腳本編寫步驟: 假設 oracle 用戶於 11.4.24.21 上的密碼爲 123abc,傳輸本地 /ora_exp/dmp/CWDB_RAMS_* 文件至 11.4.24.21 的 /ora_exp/dmp 目錄下。 則傳輸腳本相似以下。ssh

#!/bin/sh
expect -c "
  spawn bash -c \"scp /ora_exp/dmp/CWDB_RAMS_*  oracle@11.4.24.21:/ora_exp/dmp\"
  expect {
    \"yes/no\" {send \"yes\r\"; exp_continue;}
    \"*assword\" {set timeout 300; send \"123abc\r\";}
  }
  expect eof"

注意: yes/no 判斷後必須加入 exp_continue,這樣才能保證 yes/no 判斷後還能執行 *assword 的判斷。

注意:scp 傳輸文件中常常使用了通配符,而若是直接使用 spawn 傳遞帶通配符的命令 scp /ora_exp/dmp/CWDB_RAMS_* oracle@11.4.24.21:/ora_exp/dmp\ 將沒法執行。所以,需在 spawn 內套用 bash -c 調用帶通配符的命令。

sample

#!/bin/bash
a=$1
b=$2
ip=$3
port=$4
passwd=$5
/bin/rpm -qa|/bin/grep -q expect
if [ $? -ne 0 ];then
        echo "please install expect"
        exit
fi
if [ $# -ne 5 ];then
        echo "must 5 parameter,1:source file,2:object file,3:object ip 4:object port,5:passwd"
        exit
fi
expect -c "
  spawn scp -P $port $a root@$ip:$b
  expect {
    \"*assword\" {set timeout 300; send \"$passwd\r\";}
    \"yes/no\" {send \"yes\r\"; exp_continue;}
  }
  expect eof"

scp是一個基於ssh的Linux環境下傳輸文件的好工具,可是使用shell腳本調用scp時會面臨一個問題,即scp強制要求經過交互方式輸入密碼,而不像mysql等擁有-u -p選項。下面有兩種方法幫助shell腳本跨過輸入密碼這個障礙。

1.創建機器間徹底信任關係 假設須要從機器A傳輸文件至機器B 1)在機器A上運行
ssh-keygen -t rsa 上述命令會在~/.ssh/目錄生成私鑰證書id_rsa和公鑰證書id_rsa.pub; 2)將公鑰證書id_rsa.pub複製到機器B的用戶根目錄的.ssh子目錄中,再將文件內容append到文件authorized_keys中。

其實只要用一條單行命令就能夠完成步驟2,它被commandlinefu.com的用戶投票選爲十大最酷的Linux單行命令之一:
ssh-copy-id [-i [identity_file]] [user@]machine
identity_file是公鑰證書的路徑,默認狀況下是~/.ssh/id_rsa.pub.

若是要創建雙方向的徹底信任關係,還要從機器B到機器A再重複一遍上面的操做。 不過這樣的方法並不完美,一是運維成本過高,二是機器間的安全屏障徹底消失,安全代價太大,因此本人強烈推薦第二種方法。

2.expect腳本 expect腳本是一種創建在tcl基礎上的腳本語言,曝光率不高,卻堪稱shell腳本的好基友。expect腳本爲交互而生,被設計爲專門針對交互式程序的工具,常與對telnet、ftp、fsck、rlogin、tip、scp等配合使用。例如:

#!/usr/bin/expect
spawn scp -P 22 20111111.log root@192.168.0.1:/log/expect 
{  
"*assword" {set timeout 300; send "passwd\r";}  
"yes/no" {send "yes\r"; exp_continue;}
}
expect eof

上面是一個獨立的expect腳本文件,若是像把這段腳本嵌入其它shell腳本中就要用到expect -c

#!/bin/bashexpect -c "  spawn scp -P 22 20111111.log root@192.168.0.1:/log/  
expect {   
 \"*assword\" {set timeout 300; send \"passwd\r\";}    
\"yes/no\" {send \"yes\r\"; exp_continue;}  
}

expect eof"這段腳本的含義是監聽scp命令,若是輸入包含*assword,則輸入密碼「passwd」;若是輸入包含yes/no,輸入yes並繼續進行交互(exp_continue)。 注:兩個用戶第一次scp鏈接時會提示「… Are you sure you want to continue connecting (yes/no)? …」,因此要考慮yes/no的狀況。

相關文章
相關標籤/搜索