zoukankan      html  css  js  c++  java
  • centos shell编程4【分发系统】 服务器标准化 mkpasswd 生成密码的工具 expect讲解 expect传递参数 expect自动同步文件 expect指定host和要同步的文件 expect文件分发系统 expect自动发送密钥脚本 Linux脚本执行方式 第三十八节课

    centos shell编程4【分发系统】 服务器标准化  mkpasswd 生成密码的工具  expect讲解   expect传递参数   expect自动同步文件  expect指定host和要同步的文件   expect文件分发系统  expect自动发送密钥脚本  Linux脚本执行方式  第三十八节课

    expect:TCL语言非常经典的扩展部分,实现程序的自动交互

    服务器标准化:所有程序的路径,所有用户的密码,root密码都是一样的
    定期会全部更新root密码,这个叫标准化

    http://www.cnblogs.com/MYSQLZOUQI/p/4811790.html
    mkpasswd 生成密码的工具,安装 expect包

    yum install -y expect

    上半节课

    服务器标准化
    mkpasswd 生成密码的工具
    expect讲解
    expect传递参数
    expect自动同步文件
    expect指定host和要同步的文件

    下半节课

    expect文件分发系统
    expect自动发送密钥脚本
    Linux脚本执行方式

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

    spawn 它主要的功能是给ssh运行进程加个shell,用来传递交互指令。
    set .. lindex:从程序输入参数中获取变量值并赋给变量
    expect ...send对:expect等待希望出现的匹配串,对于匹配到的串,send发送命令进行执行。
    expect eof  等待表示子进程已结束的标示符eof,然后退出。(注:这个等待eof必须要有,如果没有eof,很可能在子进程没有结束前就退出,造成问题。)
    interact 执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。如果你只是登录过去执行


    使用expect之前,需要先安装expect:
    yum install -y expect

    1. 自动远程登录,并执行命令
    首先来看一个登录后不退出的脚本:
    #! /usr/bin/expect
    set host "192.168.11.102"
    set passwd "123456"
    spawn ssh root@$host
    expect {
    "yes/no" { send "yes "; exp_continue}
    "assword:" { send "$passwd " } #匹配字符串,可以不用写全,因为可以模拟匹配,比如assword,他找到password是模块匹配assword的
    }
    interact


    vim 1.expect
    chmod 700 1.expect #涉及到密码
    ./1.expect 执行脚本 ,自动登录
    logout退出


    如果要批量登录,需要每台机器的root或者登录用户的密码必须一样
    :回车

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

    #!/usr/bin/expect
    set user "root"
    set passwd "123456"

    spawn ssh $user@192.168.11.18

    expect {
    "yes/no" { send "yes "; exp_continue}
    "password:" { send "$passwd " }
    }
    expect "]*"
    send "touch /tmp/12.txt "
    expect "]*"
    send "echo 1212 > /tmp/12.txt "
    expect "]*"
    send "exit "


    expect "]*":匹配 ]$ ]# ]任意字符

    2. 我们还可以传递参数
    #!/usr/bin/expect
    set user [lindex $argv 0]
    set host [lindex $argv 1]
    set passwd "123456"
    set command [lindex $argv 2]

    spawn ssh $user@$host

    expect {
    "yes/no" { send "yes "}
    "password:" { send "$passwd " }
    }
    expect "]*"
    send "$command "
    expect "]*"
    send "exit "

    传参格式:[lindex $argv 0]
    上面脚本定义了三个参数
    set user [lindex $argv 0]
    set host [lindex $argv 1]
    set command [lindex $argv 2]
    shell格式:$1

    执行:
    ./2.expect root 192.168.11.18 w
    ./2.expect root 192.168.11.18 "ls /tmp/11.txt"

    传密码
    #!/usr/bin/expect
    set user [lindex $argv 0]
    set host [lindex $argv 1]
    set passwd [lindex $argv 3]
    set command [lindex $argv 2]

    spawn ssh $user@$host

    expect {
    "yes/no" { send "yes "}
    "password:" { send "$passwd " }
    }
    expect "]*"
    send "$command "
    expect "]*"
    send "exit "


    ./2.expect root 192.168.11.18 "ls /tmp/11.txt" pwd123456

    3. 自动同步文件
    #!/usr/bin/expect
    set passwd "123456"
    spawn rsync -av root@192.168.11.18:/tmp/12.txt /tmp/
    expect {
    "yes/no" { send "yes "}
    "password:" { send "$passwd " }
    }
    expect eof

    因为是非交互,所以要加expect eof


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


    执行: ./4.expect 192.168.11.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 "}
    "password:" { send "$passwd " }
    }
    expect eof

    cat ip.list
    192.168.11.18
    192.168.11.19
    ......


    list.txt 里面要写绝对路径 --files-from=list.txt


    cat rsync.sh
    #!/bin/bash
    for ip in `cat ip.list`
    do
    echo $ip
    ./rsync.expect $ip list.txt
    done

    chmod 700 rsync.expect

    5. 命令批量执行脚本
    cat exe.expect
    #!/usr/bin/expect
    set host [lindex $argv 0]
    set passwd "123456"
    set command [lindex $argv 1]

    spawn ssh root@$host

    expect {
    "yes/no" { send "yes "}
    "password:" { send "$passwd " }
    }
    expect "]*"
    send "$command "
    expect "]*"
    send "exit "


    cat exe.sh
    #!/bin/bash
    for ip in `cat ip.list`
    do
    echo $ip
    ./exe.expect $ip 'w;free -m;ls /tmp"
    done

    如果复杂的话,用rsync发生shell脚本到服务器,然后执行shell脚本sh xx.sh,完成一些服务器的优化工作

    文章

    http://blog.chinaunix.net/uid-20465760-id-3064252.html

    http://blog.sina.com.cn/s/blog_5140741f01015w3j.html


    扩展阅读

    自动发送密钥脚本

    #跳板机
    yum install -y expect
    su - steven
    ------------------------------------------
    vi auto_ssh.expect
    #!/usr/bin/expect
    set timeout 30
    set port [lindex $argv 0]
    set password [lindex $argv 1]
    set hostname [lindex $argv 2]
    spawn ssh-copy-id -i /home/steven/.ssh/id_rsa.pub   " -p $port  steven@$hostname"
    
    
    #first connect, no public key in ~/.ssh/known_hosts
    expect { 
    "*want to continue connecting (yes/no)?"  { send "yes
    "; exp_continue }
    "*assword:" { send "$password
    " }
    }
    
    #already has public key in ~/.ssh/known_hosts
    expect "*assword:" send "$password
    "
    
    expect "* weren't expecting."  send "
    "
    
    ----------------------------------------
    vi ip.list
    192.168.1.109
    ---------------------------------------
    
    vi forkey.sh
    #!/bin/bash
    
    for ip in `cat /home/steven/ip.list`
    do
    echo $ip
    expect  /home/steven/auto_ssh.expect  22  123456  $ip  2> /tmp/error.log 
    if [ $? == 0 ]
    then 
        continue
    else 
        break
    fi
    done
    ------------------------------------------
    chmod 700 auto_ssh.expect
    chmod 600 forkey.sh
    sh forkey.sh
    
    
    #如果对方服务器没有steven用户的报错
    cat error.log 
    usage: send [args] string
        while executing
    "send"
        invoked from within
    "expect "*assword:" send "$password
    ""
        (file "/home/steven/auto_ssh.expect" line 16)

    Linux脚本执行方式

    http://www.jb51.net/article/66824.htm

    1、相对路径方式,需先cd到脚本路径下
    [root@banking tmp]# cd /tmp
    [root@banking tmp]# ./ceshi.sh

    2、绝对路径方式
    [root@banking tmp]# /tmp/ceshi.sh  

    3、bash命令调用  使用所用脚本语言 比如 expect xx.expect  python xx.py   bash xx.sh  sh  xxx.sh
    [root@banking /]# bash /tmp/ceshi.sh

    4、. (空格)  相对或绝对路径方式   
    [root@banking /]# . /tmp/ceshi.sh   


    一般用第三种和第四种
    第一、二、三种需要赋予脚本执行权限
    第一、二、三种都是开子shell,第四种在当前shell,注意环境变量继承

    f

  • 相关阅读:
    20188477 编程作业
    原型设计
    案例分析
    编程作业
    阅读任务
    准备工作
    原型设计作业
    案例分析作业
    编程作业
    阅读任务
  • 原文地址:https://www.cnblogs.com/MYSQLZOUQI/p/5161782.html
Copyright © 2011-2022 走看看