shell實現scp批量下發檔案

來源:互聯網
上載者:User

本文系統CentOS6.0

Expect是Unix系統中用來進行自動化控制和測試的軟體工具,由Don Libes製作,作為Tcl指令碼語言的一個擴充,應用在互動式軟體中如telnet,ftp,Passwd,fsck,rlogin,tip,ssh等等。該工具利用Unix偽終端封裝其子進程,允許任意程式通過終端接入進行自動化控制;

1、安裝
1 yum install expect expect-devel -y

2、編寫Script
#!/usr/bin/expect
if {$argc < 2} {
        send_user "usage: $argv0 src_file username ip dest_file password\n"
exit
}
set src_file [lindex $argv 0]
set username [lindex $argv 1]
set host_ip [lindex $argv 2]
set dest_file [lindex $argv 3]
set password [lindex $argv 4]
spawn scp  -r $src_file $username@$host_ip:$dest_file
expect {
        "(yes/no)?"
                {
                        send "yes\n"
                        expect "*assword:" {send "$password\n"}
                }
        "*assword:"
                {
                        send "$password\n"
                }
}
expect "100%"
expect eof

3、用法執行個體:
[root@master ~]# ./allscp.sh install.log root 192.168.100.145 /tmp/ 123456


你也可以使用其他帳號;
上面實現了對單台機器複製;
4、批量伺服器複製
#!/bin/bash
               
  src_file=$1
  username=$2
  host_list=$3
  dest_file=$4
  password=$5
  cat $host_list | while read line
  do
      host_ip=`echo $line | awk '{print $1}'`
      ./allscp.sh $src_file $username $host_ip $dest_file $password
  done

用法執行個體:

希望大家有更好的介意~

推薦閱讀:

批量scp指令碼——從多台機器拷貝檔案

Linux scp 和 SSH 命令

Linux系統常用的cpio命令及scp命令

Linux cp scp命令使用

Linux伺服器 scp 不要求輸入密碼配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.