本文系統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 不要求輸入密碼配置