现在一些比较大的企业,大都使用了负载均衡,而有时由于一些程序要更改,或者有些bug要修改,快速更新代码等,若是仅是几台server的话,很简单,把已经改好的程序拷过去,或者rsync远程推送,再或者网上NFS共享一下就能够了。但若是有几十台几百台,那样的方法会太繁琐,此时就能够用expect来批量实现分发任务。shell
expect的安装:yum install -y expectvim
下面来进行expect脚本的远程登陆:bash
本机IP为120,远程IP为124负载均衡
进入/usr/local/sbin目录下(expect脚本通常放在此目录中),新建一个名称为test.expect的脚本文件,内容以下:ssh
#! /usr/bin/expect set host "192.168.6.124" set passwd "123456" spawn ssh root@$host expect { "yes/no" { send "yes\r"; exp_continue} "assword:" { send "$passwd\r" } } interact
而后更改权限:chmod a+x test.expect,此时就能够远程链接124了。 网站
再在/usr/local/sbin/目录下新建一个test2.expect的脚本,内容以下:spa
#!/usr/bin/expect set user "root" set passwd "123456" spawn ssh $user@192.168.6.124 expect { "yes/no" { send "yes\r"; exp_continue} "password:" { send "$passwd\r" } } expect "]*" send "touch /tmp/test.txt\r" expect "]*" send "echo hello world! > /tmp/test.txt\r" expect "]*" send "exit\r"
给test2.expect脚本赋予x权限:chmod a+x test2.expect,而后执行: .net
在124上查看/tmp/目录下是否有test.txt: 3d
新建名为test3.expect的脚本文件,内容以下:code
#!/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"
增长x权限后执行:
新建名为test4.expect的脚本文件,内容以下:
#!/usr/bin/expect set passwd "123456" spawn rsync -av root@192.168.6.124:/tmp/test.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof
增长x权限后执行:
新建名为test5.expect的脚本文件,内容以下:
#!/usr/bin/expect set passwd "123456" set host [lindex $argv 0] set file [lindex $argv 1] spawn rsync -av $file root@$host:$file expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof
增长x权限后执行:
需求背景:
对于大公司而言,确定时不时会有网站或者配置文件更新,并且使用的机器确定 也是好多台,少则几台,多则几十甚至上百台。因此,自动同步文件是相当重要的。
实现思路:
首先要有一台模板机器,把要分发的文件准备好,而后只要使用expect脚本批量把 须要同步的文件分发到目标机器便可。
核心命令:
rsync -av --files-from=list.txt / root@host:/ 注意:这里的都是根目录
使用rsync 的 --files参数,能够实现调用文件里面的列表,进行多个文件远程传输,从而实现文件分发文件分发系统的实现。
新建名为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
而后在/tmp/目录下新建文件ip.list
[root@zhangjin-120:/usr/local/sbin]#vim /tmp/ip.list 192.168.6.123 192.168.6.124
建立须要同步文件的列表文件file.list
[root@zhangjin-120:/usr/local/sbin]#vim /tmp/file.list /tmp/test.txt /tmp/test2.txt /root/123.sh
再建立一个名为rsync的shell脚本,内容以下:
#!/bin/bash for ip in `cat /tmp/ip.list` do echo $ip ./rsync.expect $ip /tmp/file.list done
增长x权限后执行:
当同步完代码后有可能须要批量地重启服务,所以还须要批量远程执行命令,相似于自动化。 这里是用expect编写执行命令的脚本并用shell脚原本批量调用它。
建立名为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"
赋予x权限。
再建立exe.sh的shell脚本,内容以下:
#!/bin/bash for ip in `cat ip.list` do echo $ip ./exe.expect $ip "w;free -m;ls /tmp" done