第一部分:expect讲解
expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令。当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令。但当不能使用密钥验证的时候,我们就没有办法了。所以,这时间只知道对方机器的账号和密码可以通过expect脚本实现登录和远程命令。
使用expect之前,需要安装expect:
yum install -y expect
1、自动远程登录,并执行命令
首先来看一个登录后不退出的脚本
vim 1.expect #需使用expect解释#! /usr/bin/expectset host "192.168.1.202"set passwd "504360522"spawn ssh -p60522 root@$host #my ssh port is 60522,not is 22expect {"yes/no" { send "yes\r"; exp_continue}"password:" { send "$passwd\r" }}chmod +x 1.expect./1.expect
2.再来看一个登陆后,执行命令然后退出的脚本:
vim 2.expect#!/usr/bin/expectset user "root"set passwd "504360522"spawn ssh -p 60522 $user@192.168.1.202expect {"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/expectset user [lindex $argv 0]set host [lindex $argv 1]set passwd "504360522"set cm [lindex $argv 2]spawn ssh -p 60522 $user@$hostexpect {"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 root@192.168.1.202root@192.168.1.202's password:Last login: Sat Mar 12 10:40:11 2016 from 192.168.1.201[root@Centos6 ~]# ls /rootanaconda-ks.cfg install.log install.log.syslog
4.自动同步文件
vim 4.expect#!/usr/bin/expectset passwd "504360522"spawn rsync -avz -e "ssh -p 60522" root@192.168.1.202:/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.expectspawn rsync -avz -e ssh -p 60522 root@192.168.1.202:/tmp/szk.txt /tmp/root@192.168.1.202's password:receiving incremental file listszk.txtsent 36 bytes received 74 bytes 73.33 bytes/sectotal size is 4 speedup is 0.04
5.指定hosts和同步的文件
vim 5.expect #!/usr/bin/expectset passwd "504360522"set host [lindex $argv 0]set file [lindex $argv 1]spawn rsync -ave "ssh -p 60522" root@$host:$file $fileexpect {"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.txtspawn rsync -ave ssh -p 60522 root@192.168.1.202:/tmp/szk.txt /tmp/szk.txtroot@192.168.1.202's password:receiving incremental file listsent 11 bytes received 37 bytes 96.00 bytes/sectotal 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/expectset 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/bashfor ip in `cat ip.list`do echo $ip ./rsync.expect $ip list.txtdone================vim ip.list #ip列表192.168.1.202vim list.txt #文件列表/tmp/szk.txt ================测试:[root@lab-2-C6 shell]# chmod +x rsync.sh rsync.expect[root@lab-2-C6 shell]# ./rsync.sh192.168.1.202spawn rsync -ave ssh -p 60522 --files-from=list.txt / root@192.168.1.202:/root@192.168.1.202's password:building file list ... donetmp/sent 54 bytes received 15 bytes 46.00 bytes/sectotal size is 4 speedup is 0.06
5.命令批量执行脚本
vim exe.expect#!/usr/bin/expectset host [lindex $argv 0]set passwd "504360522"set cm [lindex $argv 1]spawn ssh -p 60522 root@$hostexpect {"yes/no" { send "yes\r"}"password:" { send "$passwd\r" }}expect "]*"send "$cm\r"expect "]*"send "exit\r"================#!/bin/bashfor ip in `cat ip.list`do echo $ip ./exe.expect $ip "w;free -m;ls /tmp"done测试:[root@lab-2-C6 shell]# ./exe.sh192.168.1.202spawn ssh -p 60522 root@192.168.1.202root@192.168.1.202'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.00USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot tty1 - 09:54 5:55m 0.04s 0.04s -bashroot pts/0 192.168.1.112 09:54 33:20 0.19s 0.19s -bashroot pts/1 192.168.1.201 15:50 0.00s 0.15s 0.08s w total used free shared buffers cachedMem: 980 359 621 0 10 252-/+ buffers/cache: 96 884Swap: 1983 0 1983szk.txt yum.log yum_save_tx-2015-12-28-16-01qeDbZM.yumtx