shell实现scp批量下发文件

本文系统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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/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、用法实例:

1
[root@master ~]# ./allscp.sh install.log root  192.168 . 100.145  /tmp/  123456

215942910.jpg

你也可以使用其他帐号;

上面实现了对单台机器复制;

4、批量服务器复制

1
2
3
4
5
6
7
8
9
10
11
12
#!/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

用法实例:

221023301.jpg

希望大家有更好的介意~


本文转自 西索oO 51CTO博客,原文链接:http://blog.51cto.com/lansgg/1325006


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值