第一部分:expect讲解

expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令。当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令。但当不能使用密钥验证的时候,我们就没有办法了。所以,这时间只知道对方机器的账号和密码可以通过expect脚本实现登录和远程命令。

使用expect之前,需要安装expect:

yum install -y expect

1、自动远程登录,并执行命令

首先来看一个登录后不退出的脚本

vim 1.expect      #需使用expect解释
#! /usr/bin/expect
set host "192.168.1.202"
set passwd "504360522"
spawn ssh -p60522 root@$host           #my ssh port is 60522,not is 22
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
chmod +x 1.expect
./1.expect

2.再来看一个登陆后,执行命令然后退出的脚本:

vim 2.expect
#!/usr/bin/expect
set user "root"
set passwd "504360522"
spawn ssh -p 60522 [email protected]
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]*"
send "touch /tmp/szk.txt\r"
expect "]*"
send "echo szk > /tmp/szk.txt\r"
expect "]*"
send "exit\r"
chmod +x 2.expect
./2.expect


3.我们还可以传递参数

vim 3.expect
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "504360522"
set cm [lindex $argv 2]
spawn ssh -p 60522 $user@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"
测试:
[root@lab-2-C6 shell]# chmod +x 3.expect
[root@lab-2-C6 shell]#./3.expect root 192.168.1.202 ls    #加上 用户 IP  执行命令等参数
spawn ssh -p 60522 [email protected]
[email protected]'s password:
Last login: Sat Mar 12 10:40:11 2016 from 192.168.1.201
[root@Centos6 ~]# ls /root
anaconda-ks.cfg  install.log  install.log.syslog

4.自动同步文件

vim 4.expect
#!/usr/bin/expect
set passwd "504360522"
spawn rsync -avz -e "ssh -p 60522" [email protected]:/tmp/szk.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
测试:
[root@lab-2-C6 shell]# chmod +x 4.expect
[root@lab-2-C6 shell]# ./4.expect
spawn rsync -avz -e ssh -p 60522 [email protected]:/tmp/szk.txt /tmp/
[email protected]'s password:
receiving incremental file list
szk.txt
sent 36 bytes  received 74 bytes  73.33 bytes/sec
total size is 4  speedup is 0.04

5.指定hosts和同步的文件

vim 5.expect 
#!/usr/bin/expect
set passwd "504360522"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -ave  "ssh -p 60522"  root@$host:$file $file
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
测试:
[root@lab-2-C6 shell]# chmod +x 5.expect 
[root@lab-2-C6 shell]# ./5.expect 192.168.1.202 /tmp/szk.txt
spawn rsync -ave ssh -p 60522 [email protected]:/tmp/szk.txt /tmp/szk.txt
[email protected]'s password:
receiving incremental file list
sent 11 bytes  received 37 bytes  96.00 bytes/sec
total size is 4  speedup is 0.08

  

第二部分:构建文件分发系统

1. 需求背景

对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。

2. 实现思路

首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。


3. 核心命令

rsync -av --files-from=list.txt  /  root@host:/


4. 文件分发系统的实现

vim  rsync.expect
#!/usr/bin/expect
set passwd "504360522"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -ave "ssh -p 60522" --files-from=$file / root@$host:/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
================
vim   rsync.sh    #写一个循环脚本
#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./rsync.expect $ip list.txt
done
================
vim ip.list       #ip列表
192.168.1.202
vim list.txt     #文件列表
/tmp/szk.txt
                          
================
测试:
[root@lab-2-C6 shell]# chmod +x rsync.sh rsync.expect
[root@lab-2-C6 shell]# ./rsync.sh
192.168.1.202
spawn rsync -ave ssh -p 60522 --files-from=list.txt / [email protected]:/
[email protected]'s password:
building file list ... done
tmp/
sent 54 bytes  received 15 bytes  46.00 bytes/sec
total size is 4  speedup is 0.06


5.命令批量执行脚本

vim exe.expect
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "504360522"
set cm [lindex $argv 1]
spawn ssh -p 60522 root@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"
================
#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./exe.expect $ip "w;free -m;ls /tmp"
done
测试:
[root@lab-2-C6 shell]# ./exe.sh
192.168.1.202
spawn ssh -p 60522 [email protected]
[email protected]'s password:
Last login: Sat Mar 12 15:49:01 2016 from 192.168.1.201
[root@Centos6 ~]# w;free -m;ls /tmp
 15:50:28 up  5:56,  3 users,  load average: 0.01, 0.01, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                09:54    5:55m  0.04s  0.04s -bash
root     pts/0    192.168.1.112    09:54   33:20   0.19s  0.19s -bash
root     pts/1    192.168.1.201    15:50    0.00s  0.15s  0.08s w
             total       used       free     shared    buffers     cached
Mem:           980        359        621          0         10        252
-/+ buffers/cache:         96        884
Swap:         1983          0       1983
szk.txt  yum.log  yum_save_tx-2015-12-28-16-01qeDbZM.yumtx