20.27 分发系统介绍
一、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" set passwd "123456" spawn ssh [email protected] expect { "yes/no" { send "yes\r"; exp_continue} "password:" { send "$passwd\r" } } expect "]*" send "touch /tmp/12.txt\r" expect "]*" send "echo 1212 > /tmp/12.txt\r" expect "]*" send "exit\r" |
[root@centos expect]# chmod a+x 2.expect
[root@centos expect]# ./2.expect
3)传递参数登入,执行命令后退出的脚本
[root@centos expect]# vim 3.expect
#!/usr/bin/expect set user [lindex $argv 0] set host [lindex $argv 1] set passwd "123456" set cm [lindex $argv 2] spawn ssh $user@$host expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect "]*" send "$cm\r" expect "]*" send "exit\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
#!/usr/bin/expect set passwd "123456" spawn rsync -avzP [email protected]:/tmp/12.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } 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
#!/usr/bin/expect set passwd "123456" set host [lindex $argv 0] set file [lindex $argv 1] spawn rsync -avzP $file root@$host:$file expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof |
[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 ~]# cd /usr/local/sbin/
[root@centos sbin]# mkdir shell
[root@centos sbin]# cd shell/
[root@centos shell]# vim rsync.expect
#!/usr/bin/expect set passwd "123456" set host [lindex $argv 0] set file [lindex $argv 1] spawn rsync -av --files-from=$file / root@$host:/ expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof |
[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
#!/usr/bin/expect set host [lindex $argv 0] set passwd "123456" set cm [lindex $argv 1] spawn ssh root@$host expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect "]*" send "$cm\r" expect "]*" send "exit\r" |
[root@centos shell]# vim exe.sh
#!/bin/bash for ip in `cat ip.list` do echo $ip ./exe.expect $ip "w;free -m;ls /tmp" done |
[root@centos shell]# chmod a+x exe.expect
[root@centos shell]# chmod a+x exe.sh
[root@centos shell]# ./exe.sh //执行这个脚本实现批量执行命令
参考:http://blog.51cto.com/msiyuetian/1712233
20.28 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
20.29 expect脚本远程执行命令
20.30 expect脚本传递参数