• 第四部分shell编程5项目二分发系统


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

    1. 自动远程登录,并执行命令,登陆另外一台机器,与机器交互的脚本,登陆自动输入密码
    首先来看一个登录后不退出的脚本:
    1. #! /usr/bin/expect
    2. set host "192.168.11.102"
    3. set passwd "123456"
    4. spawn ssh root@$host #spawn 是一个语法
    5. expect {
    6. "yes/no" { send "yes "; exp_continue} #第一次登陆系统提示exp_continue往下走
    7. "assword:" { send "$passwd " }
    8. }
    9. interact

     


    再来看一个登陆后,执行命令然后退出的脚本:

    1. #!/usr/bin/expect
    2. set user "root"
    3. set passwd "123456"
    4.
    5. spawn ssh $user@192.168.11.18
    6.
    7. expect {
    8. "yes/no" { send "yes "; exp_continue}
    9. "password:" { send "$passwd " }
    10. }
    11. expect "]*" #这里定位在输入状态*表示任意的字符root是显示#,其他用户显示$
    12. send "touch /tmp/12.txt "
    13. expect "]*"
    14. send "echo 1212 > /tmp/12.txt "
    15. expect "]*"
    16. send "exit "

     

    2. 我们还可以传递参数
    1. #!/usr/bin/expect
    2. set user [lindex $argv 0] #第一个是用户
    3. set host [lindex $argv 1] #第二个是IP
    4. set passwd "123456"
    5. set cm [lindex $argv 2] #第三个cm是一个命令,用一个参数来体现
    6.
    7. spawn ssh $user@$host
    8.
    9. expect {
    10. "yes/no" { send "yes "}
    11. "password:" { send "$passwd " }
    12. }
    13. expect "]*"
    14. send "$cm "
    15. expect "]*"
    16. send "exit "


    执行 chmod +x 3.exp
    ./3.exp root 192.168.0.16 "ls /tmp/"

    ===============
    rsync -av 1.expect
    a代表带了很多很多的选项,包含权限软连接


    把本地文件1.expect传到远程的机器上,如果远程机器的目录不存在,可以自动创建一层的目录,两层不行
    rsync -av /tmp/shell/1.expect root@192.168.11.18:/tmp

    rsync -av /tmp/shell/1.expect 192.168.11.18:/tmp 省略用户,登陆的是当前的用户

    把远程机器的文件传到本地的机器上
    rsync -av 192.168.11.18:/tmp/12.txt /tmp/12.txt


    这个是自动把本地机器的文件同步到远程机器
    3. 自动同步文件
    1. #!/usr/bin/expect
    2. set passwd "123456"
    3. spawn rsync -av /tmp/12.txt root@192.168.11.18:/tmp
    4. expect {
    5. "yes/no" { send "yes "}
    6. "password:" { send "$passwd " }
    7. }
    8. expect eof
    9.

     


    这个是自动把远程机器的文件同步到本地机器
    3. 自动同步文件
    1. #!/usr/bin/expect
    2. set passwd "123456"
    3. spawn rsync -av root@192.168.11.18:/tmp/12.txt /tmp/
    4. expect {
    5. "yes/no" { send "yes "}
    6. "password:" { send "$passwd " }
    7. }
    8. expect eof
    9.


    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 "}
    8. "password:" { send "$passwd " }
    9. }
    10. expect eof


    执行: ./4.expect 192.168.11.18 /tmp/12.txt
    或者expect 4.expect 192.168.11.18 /tmp/12.txt

    第二部分:构建文件分发系统
    1. 需求背景
    对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。

    2. 实现思路
    首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。
    3. 核心命令
    rsync -av --files-from=list.txt / root@host:/ #必须从根目录开始查找,list.txt可以写绝对路径,能找到这个文件就可以,就是路径拼接


    =====
    执行
    rsync -av --files-from=list.txt / 192.168.11.16:/

    先在一个文件list.txt里面创建好文件列表
    /bin/shell/1.sh
    /tmp/12.txt

    ====


    4. 文件分发系统的实现
    5.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 "}
    8. "password:" { send "$passwd " }
    9. }
    10. expect eof

    11. #下面可以省略,用shell代替了
    12. cat ip.list
    13. 192.168.11.18
    14. 192.168.11.19
    15. ......


    先实现一个机器分发
    for ip in 192.168.11.16; do expect 5.expect $ip ./list.txt ; done

    如果是分发多个
    for ip in `cat ip.txt`; do expect 5.expect $ip ./list.txt ; done
    =======================

    执行的脚本

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


    =====================

    5. 命令批量执行脚本
    exe.expect
    1. #!/usr/bin/expect
    2. set host [lindex $argv 0]
    3. set passwd "123456"
    4. set cm [lindex $argv 1]
    5.
    6. spawn ssh root@$host
    7.
    8. expect {
    9. "yes/no" { send "yes "}
    10. "password:" { send "$passwd " }
    11. }
    12. expect "]*"
    13. send "$cm "
    14. expect "]*"
    15. send "exit "

     

    ip.txt 里面有192.168.11.16 多个可以有循环

    for ip in `cat ip.txt`; do ./exe.expect $ip "w;ls -dl /tmp; touch /root/aming123.txt"; done


    ==============
    执行的脚本

    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

     

     

    ssh 192.168.11.16

  • 相关阅读:
    leetcode 87. Scramble String
    leetcode 1278 分割回文串
    back propagation in NNs and RNNs
    刷题笔记——单调栈
    unsupervised learning -- K MEANS
    install j3d on macOS
    java extract jar file on MacOS
    back-propagation algorithm
    day 4
    day 3
  • 原文地址:https://www.cnblogs.com/chenshoubiao/p/4854983.html
Copyright © 2020-2023  润新知