• Linux记录-Shell自动化部署批量建立用户和批量SSH配置(转载)


    if [  !  $# -eq 2  ] ; then
    echo "请输入用户名和密码以空格分开!"
    exit
    
    else
    name="$1"
    passwd="$2"
    fi
    
    
    cat hosts | while read hosts
    do
    
    echo "正在$hosts上用户$name"
    expect <<EOF
    spawn  ssh $hosts "useradd $name; echo $name:$passwd | chpasswd"
    expect {
    "*yes/no" {send "yes
    " ;exp_continue}
    "*password:" {send "dongliang
    " ;exp_continue  }
    }
    
    EOF
    
    echo  "成功建立"
    
    done
    
    if [  ! $# -eq 2  ] ;then
    echo "请输入用户密码以空格分开"
    exit
    else
    #用户名
    uname="$1"
    #密码
    passwd="$2"
    fi
    
    #执行检测并安装expect模块
    ep=`rpm -qa | grep expect`
    
    if [ -z $ep  ] ; then
     echo "检测到你的系统,没有安装expect模块,准备执行安装"
     sleep 2
     yum install -y expect
    
    else
     echo "已经安装expect模块,3秒后,开始执行ssh文件同步"
     sleep 3
    fi
    
    #公钥无ssh认证模块
    cat hosts | while read host
    do
    
    echo "当前正在向$host上传输ssh文件"
    expect <<EOF
    spawn  scp -r .ssh/  $host:/$uname
    expect {
    "*yes/no" {send "yes
    " ;exp_continue}
    "*password:" {send "$passwd
    " ;exp_continue  }
    }
    
    EOF
    
    echo "当前正在$host上进行公钥认证....."
    sleep 2
    expect <<EOF
    spawn  ssh-copy-id  -i .ssh/id_rsa.pub $host
    expect {
    "*yes/no" {send "yes
    " ;exp_continue}
    "*password:" {send "$passwd
    " ;exp_continue  }
    }
    
    EOF
    
    echo "认证成功...."
    
    done
    
    #切换root权限进行hosts文件分发
    
    echo "同步本机的hosts文件到各个机器上"
    
    sleep 1
    
    #同步本机的hosts文件到其他各个机器上
    
    cat hosts | while read host
    do
    
    scp -r /etc/hosts $host:/etc/
    
    
    done
    
    echo "同步hosts文件完毕"
    
    if [ ! $# -eq 2  ] ; then
    
    echo "请输入用户名密码以空格分开"
    exit
    else
    
    #用户的名字
    uname="$1"
    #用户的统一密码
    pd="$2"
    fi
    
    
    #公钥无ssh认证模块
    cat hosts | while read host
    do
    
    echo "当前正在向$host上传输ssh文件"
    expect <<EOF
    spawn  scp -r .ssh/  $host:/home/$uname
    expect {
    "*yes/no" {send "yes
    " ;exp_continue}
    "*password:" {send "$pd
    " ;exp_continue  }
    }
    
    
    EOF
    
    echo "当前正在$host上进行公钥认证....."
    sleep 2
    expect <<EOF
    spawn  ssh-copy-id  -i .ssh/id_rsa.pub $host
    expect {
    "*yes/no" {send "yes
    " ;exp_continue}
    "*password:" {send "$pd
    " ;exp_continue  }
    }
    
    
    EOF
    
    echo "认证成功...."
    
    done
  • 相关阅读:
    换个角度认识大数据(上)——何为元数据
    如何优雅地停止Spark Streaming Job
    split("\,")引起的java.lang.ArrayIndexOutOfBoundsException异常解决方案
    spark streaming整合kafka
    Kafka相关命令
    Hive性能调优(二)----数据倾斜
    Hive SQL编译过程(转)
    Hive性能调优(一)----文件存储格式及压缩方式选择
    Hive表的几种存储格式
    spring框架之javaconfig
  • 原文地址:https://www.cnblogs.com/xinfang520/p/11611525.html
Copyright © 2020-2023  润新知