长枪不倒 发表于 2018-8-19 14:44:31

shell编程之【分发系统】

  一、expect讲解
  expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令。当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令。但当不能使用密钥验证的时候,我们就没有办法了。所以,这时候只要知道对方机器的账号和密码就可以通过expect脚本实现登录和远程命令。
  1、安装expect
  # yum install -y expect
  2、自动远程登入脚本
  自动远程登录,并执行命令,下面介绍几个脚本:
  第一个:登陆后不退出的脚本;
  第二个:登陆后,执行命令然后退出的脚本;
  第三个:传递参数登入,然后执行命令退出的脚本;
  第四个:自动同步文件脚本;
  第五个:指定host和要同步的文件。
  1)登入后不退出的脚本
  # cd /usr/local/sbin/
  # mkdir expect
  # cd expect/
  # vim 1.expect
  #! /usr/bin/expect
  set host "192.168.0.10"
  set passwd "123456"
  spawn sshroot@$host
  expect {
  "yes/no" { send "yes\r"; exp_continue}
  "assword:" { send "$passwd\r" }
  }
  interact
  # chmod a+x 1.expect
  # ./1.expect               //运行脚本登入远程机器,logout就可以退出
  2)执行命令后退出的脚本
  # vim 2.expect
  #!/usr/bin/expect
  set user "root"
  set passwd "123456"
  spawn ssh $user@192.168.0.10
  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 a+x 2.expect
  # ./2.expect
  3)传递参数登入,执行命令后退出的脚本
  # vim 3.expect
  #!/usr/bin/expect
  set user
  set host
  set passwd "123456"
  set cm
  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定义后面需要执行的命令。
  # chmod a+x 3.expect
  # ./3.expect root 192.168.0.10 "cat /etc/passwd"
  4)自动同步文件脚本
  # vim 4.expect
  #!/usr/bin/expect
  set passwd "123456"
  spawn rsync -avzP root@192.168.0.10:/tmp/12.txt /tmp/
  expect {
  "yes/no" { send "yes\r"}
  "password:" { send "$passwd\r" }
  }
  expect eof
  注意:eof相当于结束的意思;另外要想实现远程传输文件,本机和远程机器都必须安装rsync。
  # yum install -y rsync
  # chmod a+x 4.expect
  # ./4.expect
  5)指定host和要同步的文件
  # vim 5.expect
  #!/usr/bin/expect
  set passwd "123456"
  set host
  set file
  spawn rsync -avzP $file root@$host:$file
  expect {
  "yes/no" { send "yes\r"}
  "password:" { send "$passwd\r" }
  }
  expect eof
  # chmod a+x 5.expect
  # ./5.expect 192.168.0.10 /tmp/12.txt
  说明:要想实现同步,必须实现统一化,就是所有的远程机器密码相同,并且文件路径保持一致。这里的 $file 就是本机和远程机相同的文件路径,这样才能实现同步。若想实现多台的远程机器批量同步,我们进行下面操作:
  # touch /tmp/ip.list                  //IP列表
  # for ip in `cat /tmp/ip.list`; do echo $ip; ./5.expect $ip /tmp/12.txt; done
  二、构建文件分发系统
      对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。
  首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。
  1、批量分发文件
  # cd /usr/local/sbin/
  # mkdir shell
  # cd shell/
  # vim rsync.expect
  #!/usr/bin/expect
  set passwd "123456"
  set host
  set file
  spawn rsync -av --files-from=$file / root@$host:/
  expect {
  "yes/no" { send "yes\r"}
  "password:" { send "$passwd\r" }
  }
  expect eof
  # vim rsync.sh
  #!/bin/bash
  for ip in `cat ip.list`
  do
  echo $ip
  ./rsync.expect $ip file.list
  done
  # vim ip.list
  192.168.0.115
  192.168.0.114
  # vim file.list                  //要确保每台机器都有下列文件(必须是绝对路径)
  /123/test1.txt
  /456/test2.txt
  # chmod a+x rsync.expect
  # chmod a+x rsync.sh
  # ./rsync.sh                  //执行这个脚本实现文件同步
  2、批量执行命令
  # vim exe.expect
  #!/usr/bin/expect
  set host
  set passwd "123456"
  set cm
  spawn ssh root@$host
  expect {
  "yes/no" { send "yes\r"}
  "password:" { send "$passwd\r" }
  }
  expect "]*"
  send "$cm\r"
  expect "]*"
  send "exit\r"
  # vim exe.sh
  #!/bin/bash
  for ip in `cat ip.list`
  do
  echo $ip
  ./exe.expect $ip "w;free -m;ls /tmp"
  done
  # chmod a+x exe.expect
  # chmod a+x exe.sh
  # ./exe.sh                  //执行这个脚本实现批量执行命令

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