第一部分:expect讲解
expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令。当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令。但当不能使用密钥验证的时候,我们就没有办法了。所以,这时候只要知道对方机器的账号和密码就可以通过expect脚本实现登录和远程命令。
使用expect之前,需要先安装expect:
yum install -y expect
1. 自动远程登录,并执行命令
首先来看一个登录后、 不退出、 的脚本:vim expect
- #! /usr/bin/expect
- set host "192.168.1.102"
- set passwd "123456"
- spawn ssh root@$host
- expect {
- "yes/no" { send "yes\r"; exp_continue}
- "assword:" { send "$passwd\r" }
- }
- interact
执行的时候,另一台机器提前是打开
# ./expect
#运行 ifcnofig查看,都是对方机器的数据。
再来看一个 登陆后、 执行命令然后、 退出、 的脚本:
- #!/usr/bin/expect
- set user "root"
- set passwd "123456"
-
- spawn ssh $user@192.168.1.18
-
- expect {
- "yes/no" { send "yes\r"; exp_continue}
- "assword:" { send "$passwd\r" }
- }
- expect "]*"
- send "touch /tmp/12.txt\r"
- expect "]*"
- send "echo 1212 >> /tmp/12.txt\r"
- expect "]*"
- send "mkdir -p ~/1212hao/wo
- send "exit\r"
# ./expect
本机上也可以看到执行过程。
验证:可以到另一台机器上查看,与否。
2. 我们还可以传递参数
- #!/usr/bin/expect
- set user [lindex $argv 0]
- set host [lindex $argv 1]
- set passwd "123456" #可以将密码设成密码变量,执行的时候输入密码即可 [lindex $argv 3]
- set cm [lindex $argv 2]
-
- spawn ssh $user@$host
-
- expect {
- "yes/no" { send "yes\r"}
- "assword:" { send "$passwd\r" }
- }
- expect "]*"
- send "$cm\r"
- expect "]*"
- send "exit\r"
# chmod a+x .filename
# ./filename root 192.168.1.18 "ls /tmp/12.txt"
还有另一种方法,
#./filename root 192.168.1.18 "ls /tmp/12.txt"123456密码
同样执行成功。
3. 自动同步文件
- #!/usr/bin/expect
- set passwd "123456"
- spawn rsync -avzP root@192.168.11.18:/tmp/12.txt /tmp/
- expect {
- "yes/no" { send "yes\r"}
- "ssword:" { send "$passwd\r" }
- }
- expect eof
-
把 对方机器上 文件拉过来, 拉到我这 儿。
前提双方机器要有rsync 服务# yum install -y rsync
#chmod +x file
#./file
检查:#cat /tmp/12.txt 也可以再次追加12,。txt文件,再次拉过来看看,有何变化。
4. 指定 host 和要 同步的文件
- #!/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"}
- "ssword:" { send "$passwd\r" }
- }
- expect eof
执行的时候加# ./5.expect 192.168.1.18 /tmp/12.txt
把 己方文 件同步到 对方机器上
第二部分:构建文件分发系统
1. 需求背景
对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。
2. 实现思路
首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。
3. 核心命令
rsync -av --files-from=list.txt / root@host:/
4. 文件分发系统的实现
cat 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"}
- "ssword:" { send "$passwd\r" }
- }
- expect eof
- cat ip.list
- 192.168.1.18
- 192.168.1.19
- ......
- cat file.list
- /tmp/* 1.txt
- /usr/local/sbin/* 2.txt
cat rsync.sh
- #!/bin/bash
- for ip in `cat ip.list`
- do
- echo $ip
- ./rsync.expect $ip list.txt
- done
-
执行脚本。sh就可以了。
#chmod a+x file1 file2 file3 file4
#./rsync.sh
5. 命令批量执行脚本,设置为当前目录下
cat 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"
cat exe.sh
- #!/bin/bash
- for ip in `cat ip.list`
- do
- echo $ip
- ./exe.expect $ip "w;free -m;ls /tmp"
- done