一、expect講解
expect可以讓我們實作自動登入遠端機器,并且可以實作自動遠端執行指令。當然若是使用不帶密碼的密鑰驗證同樣可以實作自動登入和自動遠端執行指令。但當不能使用密鑰驗證的時候,我們就沒有辦法了。是以,這時候隻要知道對方機器的賬号和密碼就可以通過expect腳本實作登入和遠端指令。
1、安裝expect
[root@centos ~]# yum install -y expect
2、自動遠端登入腳本
自動遠端登入,并執行指令,下面介紹幾個腳本:
第一個:登陸後不退出的腳本;
第二個:登陸後,執行指令然後退出的腳本;
第三個:傳遞參數登入,然後執行指令退出的腳本;
第四個:自動同步檔案腳本;
第五個:指定host和要同步的檔案。
1)登入後不退出的腳本
[root@centos ~]# cd /usr/local/sbin/
[root@centos sbin]# mkdir expect
[root@centos sbin]# cd expect/
[root@centos expect]# vim 1.expect
#! /usr/bin/expect
set host "192.168.0.10"
set passwd "123456"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
interact
[root@centos expect]# chmod a+x 1.expect
[root@centos expect]# ./1.expect //運作腳本登入遠端機器,logout就可以退出
2)執行指令後退出的腳本
[root@centos expect]# vim 2.expect
#!/usr/bin/expect
set user "root"
spawn ssh [email protected]
"password:" { send "$passwd\r" }
expect "]*"
send "touch /tmp/12.txt\r"
send "echo 1212 > /tmp/12.txt\r"
send "exit\r"
[root@centos expect]# chmod a+x 2.expect
[root@centos expect]# ./2.expect
3)傳遞參數登入,執行指令後退出的腳本
[root@centos expect]# vim 3.expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set cm [lindex $argv 2]
spawn ssh $user@$host
"yes/no" { send "yes\r"}
send "$cm\r"
注意:這裡定義參數格式是 "$argv 0" ,和我們shell定義參數格式 "$0" 不太一樣。cm定義後面需要執行的指令。
[root@centos expect]# chmod a+x 3.expect
[root@centos expect]# ./3.expect root 192.168.0.10 "cat /etc/passwd"
4)自動同步檔案腳本
[root@centos expect]# vim 4.expect
spawn rsync -avzP [email protected]:/tmp/12.txt /tmp/
expect eof
注意:eof相當于結束的意思;另外要想實作遠端傳輸檔案,本機和遠端機器都必須安裝rsync。
[root@centos expect]# yum install -y rsync
[root@centos expect]# chmod a+x 4.expect
[root@centos expect]# ./4.expect
5)指定host和要同步的檔案
[root@centos expect]# vim 5.expect
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -avzP $file root@$host:$file
[root@centos expect]# chmod a+x 5.expect
[root@centos expect]# ./5.expect 192.168.0.10 /tmp/12.txt
說明:要想實作同步,必須實作統一化,就是所有的遠端機器密碼相同,并且檔案路徑保持一緻。這裡的 $file 就是本機和遠端機相同的檔案路徑,這樣才能實作同步。若想實作多台的遠端機器批量同步,我們進行下面操作:
[root@centos expect]# touch /tmp/ip.list //IP清單
[root@centos expect]# for ip in `cat /tmp/ip.list`; do echo $ip; ./5.expect $ip /tmp/12.txt; done
二、建構檔案分發系統
對于大公司而言,肯定時不時會有網站或者配置檔案更新,而且使用的機器肯定也是好多台,少則幾台,多則幾十甚至上百台。是以,自動同步檔案是至關重要的。
首先要有一台模闆機器,把要分發的檔案準備好,然後隻要使用expect腳本批量把需要同步的檔案分發到目标機器即可。
1、批量分發檔案
[root@centos sbin]# mkdir shell
[root@centos sbin]# cd shell/
[root@centos shell]# vim rsync.expect
spawn rsync -av --files-from=$file / root@$host:/
[root@centos shell]# vim rsync.sh
#!/bin/bash
for ip in `cat ip.list`
do
echo $ip
./rsync.expect $ip file.list
done
[root@centos shell]# vim ip.list
192.168.0.115
192.168.0.114
[root@centos shell]# vim file.list //要確定每台機器都有下列檔案(必須是絕對路徑)
/123/test1.txt
/456/test2.txt
[root@centos shell]# chmod a+x rsync.expect
[root@centos shell]# chmod a+x rsync.sh
[root@centos shell]# ./rsync.sh //執行這個腳本實作檔案同步
2、批量執行指令
[root@centos shell]# vim exe.expect
set cm [lindex $argv 1]
spawn ssh root@$host
[root@centos shell]# vim exe.sh
./exe.expect $ip "w;free -m;ls /tmp"
[root@centos shell]# chmod a+x exe.expect
[root@centos shell]# chmod a+x exe.sh
[root@centos shell]# ./exe.sh //執行這個腳本實作批量執行指令
本文轉自 M四月天 51CTO部落格,原文連結:http://blog.51cto.com/msiyuetian/1712233,如需轉載請自行聯系原作者