iojsioi 发表于 2018-8-28 10:21:54

shell编程【分发系统】

  shell编程【分发系统】
  原文地址:http://www.apelearn.com/bbs/thread-8113-1-1.html
  第一部分:expect讲解
  expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令。当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令。但当不能使用密钥验证的时候,我们就没有办法了。所以,这时候只要知道对方机器的账号和密码就可以通过expect脚本实现登录和远程命令。
  使用expect之前,需要先安装expect:
  yum install -y expect
  确认expect包安装:
  # rpm -qa| grep expect
  expect-5.44.1.15-5.el6_4.i686
  1. 自动远程登录,并执行命令
  首先来看一个登录后不退出的脚本:
  # mkdir shell
  # cd shell
  # vim 1.expect
#! /usr/bin/expect  
set host "192.168.101.108"
  
set passwd "daixuan"
  
spawn sshroot@$host
  
expect {
  
"yes/no" { send "yes\r"; exp_continue}
  
"assword:" { send "$passwd\r" }
  
}
  
interact
  # chmod +x 1.expect
  # ./1.expect   #这里从101.230成功远程登录101.108
  spawn ssh root@192.168.101.108
  The authenticity of host '192.168.101.108 (192.168.101.108)' can't be established.
  RSA key fingerprint is 6a:b8:52:23:8e:38:00:91:b4:30:bc:94:62:f9:fc:8e.
  Are you sure you want to continue connecting (yes/no)? yes
  Warning: Permanently added '192.168.101.108' (RSA) to the list of known hosts.
  root@192.168.101.108's password:
  Last login: Fri Feb 12 20:14:27 2016
  # ifconfig
  eth0      Link encap:EthernetHWaddr 00:0C:29:0B:C6:57
  inet addr:192.168.101.108Bcast:192.168.101.255Mask:255.255.255.0# logout
  Connection to 192.168.101.108 closed.
  # ifconfig
  eth0      Link encap:EthernetHWaddr 00:0C:29:72:15:4F
  inet addr:192.168.101.230Bcast:192.168.101.255Mask:255.255.255.0
  2、再来看一个登陆后,执行命令然后退出的脚本:
  # vim 2.expect
#!/usr/bin/expect  
set user "root"
  
set passwd "daixuan"
  

  
spawn ssh $user@192.168.11.108
  

  
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"
  # chmod +x 2.expect
  # ./2.expect
  spawn ssh root@192.168.101.108
  root@192.168.101.108's password:
  Last login: Fri Feb 12 20:52:12 2016 from www.test.com
  # touch /tmp/12.txt
  # echo 1212 > /tmp/12.txt
  # #
  登录101.108 查看
  # cat /tmp/12.txt
  1212
  3. 我们还可以传递参数
  # vim 3.expect
#!/usr/bin/expect  
set user
  
set host
  
set passwd "daixuan"
  
set cm
  
spawn ssh $user@$host
  
expect {
  
"yes/no" { send "yes\r"}
  
"password:" { send "$passwd\r" }
  
}
  
expect "]*"
  
send "$cm\r"
  
expect "]*"
  
send "exit\r"
  # chmod x 3.expect
  执行: ./3.expect root 192.168.101.108 w
  # ./3.expect root 192.168.101.108 w
  spawn ssh root@192.168.101.108
  root@192.168.101.108's password:
  Last login: Fri Feb 12 22:30:59 2016 from 192.168.101.105
  # w
  22:31:21 up2:57,4 users,load average: 0.00, 0.00, 0.00

  USER   TTY      FROM            LOGIN@>  root   tty1   -                20:14   58:58   0.09s0.09s -bash
  root   pts/0    192.168.101.10521:33   10:46   0.16s0.04s bash
  4. 自动同步文件
  # vim 4.expect
#!/usr/bin/expect  
set passwd "daixuan"
  
spawn rsync -av root@192.168.101.108:/tmp/12.txt /tmp/
  
expect {
  
"yes/no" { send "yes\r"}
  
"password:" { send "$passwd\r" }
  
}
  
expect eof #这里必须有expect eof,否则只是登陆但不同步数据
  # chmod +x 4.expect
  # ./4.expect
  # cat /tmp/12.txt   #数据1212已经同步过来了
  1212
  5. 指定host和要同步的文件
  # vim 5.expect
#!/usr/bin/expect  
set passwd "daixuan"
  
set host
  
set file
  
spawn rsync -av $file root@$host:$file
  
expect {
  
"yes/no" { send "yes\r"}
  
"password:" { send "$passwd\r" }
  
}
  
expect eof
  # chmod +x 5.expect
  新建本地101.230的文件:/tmp/12.txt,内容为空
  执行: ./5.expect 192.168.101.108 /tmp/12.txt
  同步之后,101.108服务器上的/tmp/12.txt内容也为空
  第二部分:构建文件分发系统
  1. 需求背景
  对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。
  2. 实现思路
  首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。
  3. 核心命令
  rsync -av --files-from=list.txt/root@host:/
  4. 文件分发系统的实现
  catrsync.expect
#!/usr/bin/expect  
set passwd "daixuan"
  
set host
  
set file
  
spawn rsync -av --files-from=$file / root@$host:/
  
expect {
  
"yes/no" { send "yes\r"}
  
"password:" { send "$passwd\r" }
  
}
  
expect eof
  此外,还需要定义:
  ip.list (定义需要同步的IP)
  file.list (需要使用绝对路径)
  cat rsync.sh
#!/bin/bash  
for ip in `cat ip.list`
  
do
  
    echo $ip
  
    ./rsync.expect $ip list.txt
  
done
  5. 命令批量执行脚本
  cat exe.expect
#!/usr/bin/expect  
set host
  
set passwd "daixuan"
  
set cm
  

  
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


页: [1]
查看完整版本: shell编程【分发系统】