歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> shell實現scp批量下發文件

shell實現scp批量下發文件

日期:2017/2/28 14:38:38   编辑:SHELL編程

本文系統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腳本——從多台機器拷貝文件 http://www.linuxidc.com/Linux/2013-06/86034.htm

Linux scp 和 SSH 命令 http://www.linuxidc.com/Linux/2013-03/80645.htm

Linux系統常用的cpio命令及scp命令 http://www.linuxidc.com/Linux/2013-01/77719.htm

Linux cp scp命令使用 http://www.linuxidc.com/Linux/2012-12/76840.htm

Linux服務器 scp 不需要密碼配置 http://www.linuxidc.com/Linux/2012-12/75886.htm

Copyright © Linux教程網 All Rights Reserved