shell脚本_【分发系统】

来源:互联网 发布:如何设置mac桌面图标 编辑:程序博客网 时间:2024/05/17 07:34



第一部分:expect讲解

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

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


首先来看一个登录后、 不退出、 的脚本:vim expect
  1. #! /usr/bin/expect
  2. set host "192.168.1.102"
  3. set passwd "123456"
  4. spawn ssh  root@$host
  5. expect {
  6. "yes/no" { send "yes\r"; exp_continue}
  7. "assword:" { send "$passwd\r" }
  8. }
  9. interact


执行的时候,另一台机器提前是打开

# ./expect

#运行 ifcnofig查看,都是对方机器的数据。


再来看一个  登陆后、 执行命令然后、 退出、 的脚本:
  1. #!/usr/bin/expect
  2. set user "root"
  3. set passwd "123456"
  4. spawn ssh $user@192.168.1.18
  5. expect {
  6. "yes/no" { send "yes\r"; exp_continue}
  7. "assword:" { send "$passwd\r" }
  8. }
  9. expect "]*"
  10. send "touch /tmp/12.txt\r"
  11. expect "]*"
  12. send "echo 1212 >> /tmp/12.txt\r"
  13. expect "]*"
  14. send "mkdir -p ~/1212hao/wo
  15. send "exit\r"

# ./expect

本机上也可以看到执行过程。

验证:可以到另一台机器上查看,与否。


2. 我们还可以传递参数
  1. #!/usr/bin/expect
  2. set user [lindex $argv 0]
  3. set host [lindex $argv 1]
  4. set passwd "123456"    #可以将密码设成密码变量,执行的时候输入密码即可 [lindex $argv 3]
  5. set cm [lindex $argv 2]
  6. spawn ssh $user@$host
  7. expect {
  8. "yes/no" { send "yes\r"}
  9. "assword:" { send "$passwd\r" }
  10. }
  11. expect "]*"
  12. send "$cm\r"
  13. expect "]*"
  14. 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. 自动同步文件
  1. #!/usr/bin/expect
  2. set passwd "123456"
  3. spawn rsync -avzP root@192.168.11.18:/tmp/12.txt /tmp/
  4. expect {
  5. "yes/no" { send "yes\r"}
  6. "ssword:" { send "$passwd\r" }
  7. }
  8. expect eof

把 对方机器上 文件拉过来, 拉到我这 儿。

前提双方机器要有rsync 服务# yum install -y rsync

#chmod +x file

#./file

检查:#cat /tmp/12.txt 也可以再次追加12,。txt文件,再次拉过来看看,有何变化。



4. 指定  host  和要  同步的文件

  1. #!/usr/bin/expect
  2. set passwd "123456"
  3. set host [lindex $argv 0]
  4. set file [lindex $argv 1]
  5. spawn rsync -av $file root@$host:$file
  6. expect {
  7. "yes/no" { send "yes\r"}
  8. "ssword:" { send "$passwd\r" }
  9. }
  10. 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
  1. #!/usr/bin/expect
  2. set passwd "123456"
  3. set host [lindex $argv 0]
  4. set file [lindex $argv 1]
  5. spawn rsync -av --files-from=$file / root@$host:/
  6. expect {
  7. "yes/no" { send "yes\r"}
  8. "ssword:" { send "$passwd\r" }
  9. }
  10. expect eof




  1. cat ip.list
  2. 192.168.1.18
  3. 192.168.1.19
  4. ......
  5. cat file.list
  6. /tmp/* 1.txt
  7. /usr/local/sbin/* 2.txt




cat rsync.sh
  1. #!/bin/bash
  2. for ip in `cat ip.list`
  3. do
  4.     echo $ip
  5.     ./rsync.expect $ip list.txt
  6. done

执行脚本。sh就可以了。

#chmod a+x file1 file2 file3 file4

#./rsync.sh






5. 命令批量执行脚本,设置为当前目录下
cat exe.expect
  1. #!/usr/bin/expect
  2. set host [lindex $argv 0]
  3. set passwd "123456"
  4. set cm [lindex $argv 1]
  5. spawn ssh root@$host
  6. expect {
  7. "yes/no" { send "yes\r"}
  8. "password:" { send "$passwd\r" }
  9. }
  10. expect "]*"
  11. send "$cm\r"
  12. expect "]*"
  13. send "exit\r"


cat exe.sh
  1. #!/bin/bash
  2. for ip in `cat ip.list`
  3. do
  4.     echo $ip
  5.     ./exe.expect $ip "w;free -m;ls /tmp"
  6. done
0 0
原创粉丝点击