zoukankan      html  css  js  c++  java
  • SSH批量管理 expect自动交互

    SSH批量管理
     expect自动交互

     原创博文http://www.cnblogs.com/elvi/p/7662908.html

    # SSH批量管理
    # expect自动交互
    
    
    #############################
    #!/bin/sh
    # 初次使用,以root批量执行
    
    CMD=(
    date
    uname -r
    hostname
    )
    
    sh ${[*]}
    
    . /etc/init.d/functions
    [[ -f /usr/bin/expect ]] || { echo "install expect";yum install expect -y; } #若没expect则安装
    ##########
    
    
    #############################
    # #SSH批量执行shell命令
    #!/bin/sh
    . /etc/init.d/functions
    [[ $# == 0 ]] && { echo 'ege: /bin/sh '$0' "命令1;命令2"';exit; }
    for n in 81 82 83
    do
      echo  
      ssh -p22 192.168.20.$n $*
      [[ $? == 0 ]] && action "========IP:192.168.20.$n========" /bin/true || action "========IP:192.168.20.$n========" /bin/false
    done
    ##########
    
    
    
    #############################
    ###批量推送文件
    #!/bin/sh
    . /etc/init.d/functions
    [[ $# == 0 ]] && { echo 'ege: /bin/sh '$0' "本地文件" "(远程)目录"';exit; }
    for n in 81 82 83
    do
      scp -P22 $1 sshync@192.168.20.$n ~/ >/dev/null 2>&1 &&
      ssh -p22 -t sshync@192.168.20.$n sudo rsync ~/$1 $2 >/dev/null 2>&1
      [[ $? == 0 ]] && action "========IP:192.168.20.$n" /bin/true || action "========IP:192.168.20.$n" /bin/false
    done
    ##########
    
    #############################
    #!/bin/sh
    # 批量分发SSHKEY
    
    . /etc/init.d/functions
    [[ -f /usr/bin/expect ]] || { echo "install expect";yum install expect -y; } #若没expect则安装
    
    PUB=/`whoami`/.ssh/id_dsa.pub
    [[ -f $PUB ]] || { ssh-keygen -t dsa -P '' -f /`whoami`/.ssh/id_dsa>/dev/null 2>&1; } #若没公钥则生成
    
    USER=sshync
    PASS=sshync
    # IP : SIP + SIP_end
    SIP=192.168.20.
    SIP_end=(41 47 43)
    
    #expect自动交互
    function EXP() {
    /usr/bin/expect << EOF
    set timeout 1
    spawn ssh-copy-id -i $PUB "-p22 $USER@$SIP$n"
    expect {
            "yes/no" { send "yes
    ";exp_continue }
            "password:" { send "$PASS
    ";exp_continue } 
            eof { exit }
            }
    EOF
    }
    #
    [[ $1 == y ]] || { echo -e 'ege: /bin/sh '$0' y 
        当前'`whoami`'用户向远程用户'$USER'分发SSHKEY 
        远程主机列表'$SIP''${SIP_end[*]}' 
    ';exit; }
    
    for n in ${SIP_end[*]}
    do
      EXP |grep '.ssh/authorized_keys'>/dev/null 2>&1
      [[ $? == 0 ]] && action "========IP:192.168.20.$n" /bin/true || action "========IP:192.168.20.$n" /bin/false
    done
    
    
    #############################
    #!/bin/sh
    #Made by elven,2017
    # MariaDB初始化,设置密码
    
    DBPass=db2017
    [[ -f /usr/bin/expect ]] || { echo "install expect";yum install expect -y; } #若没expect则安装
    /usr/bin/expect << EOF
    set timeout 30
    spawn mysql_secure_installation
    expect {
        "enter for none" { send "
    "; exp_continue}
        "Y/n" { send "Y
    " ; exp_continue}
        "password:" { send "$DBPass
    "; exp_continue}
        "new password:" { send "$DBPass
    "; exp_continue}
        "Y/n" { send "Y
    " ; exp_continue}
        eof { exit }
    }
    EOF
    #测试
    mysql -u root -p$DBPass -e "show databases;"
    #############################
  • 相关阅读:
    解决显示器闪屏的问题
    装完系统后由于分辨率问题不能进入系统怎么办?怎么能不进入系统设置分表率?
    如何设置win7任务栏的计算机快速启动
    【转载】Oracle层次查询和分析函数
    【原创】birt 报表工具 不能运行 不能预览问题
    【转】最牛B的编码套路
    Windows下python环境配置
    python:numpy 下载
    TEX(LaTEX)输出PDF设置US LETTER或者LETTERPAPER办法
    Adobe Illustrator Cs6【AI cs6】中文破解版安装图文教程、破解注册方法
  • 原文地址:https://www.cnblogs.com/elvi/p/7662908.html
Copyright © 2011-2022 走看看