zoukankan      html  css  js  c++  java
  • EXPECT交互式批量上传公钥

    EXPECT交互式批量上传公钥

    # vim key.sh
    #/bin/bash
    Public_Key() {
    [ ! -f /usr/bin/expect ] && yum install expect -y &>/dev/null  #判断expect是否安装,没有则安装expect(这个挺好用的)
    /usr/bin/expect <<EOF   #在控制节点机器上生成密钥对,一种是从未生成过,一种是原来生成过(注意本脚本选择不覆盖)
    set timeout 30
    spawn ssh-keygen
    expect "(/root/.ssh/id_rsa):"
    send "
    "
    expect  {
            "Overwrite" { send "n
    " }
            "Enter passphrase (empty for no passphrase):" {
                    send "
    "
                    expect "Enter same passphrase again:"
                    send "
    " }
    }
    expect eof
    EOF
    while read line;do #遍历循环/test/host_passwd.txt文件,批量上传公钥
    host=$(echo $line | awk '{print $1}')
    passwd=$(echo $line | awk '{print $2}')
    /usr/bin/expect <<EOF
    spawn ssh-copy-id $host
    expect {
            "yes/no" { send "yes
    "; exp_continue }
            "password:" { send "$passwd
    "}
    }
    expect eof
    EOF
    done < /test/host_passwd.txt  #主机ip及密码文件的绝对路径
    } Public_Key
    && echo "now you can ssh ip" || echo "Please check your host_passwd.txt"
    # cat /test/host_passwd.txt
    192.168.10.10    123456
    192.168.10.11    123456
    192.168.10.12    123456
    192.168.10.13    123456
    ...

    完成后删除 rm -rf /test/host_passwd.txt 即可

  • 相关阅读:
    文化课随笔
    微积分与无穷级数
    [康复计划]-数论基础
    [Codeforces]CF742(Div.2)A-E
    第一次个人编程作业的过程和想法
    第一次个人编程作业
    Python命令行参数及文件读出写入
    第一次个人编程作业
    第一次个人编程作业
    第一次博客作业
  • 原文地址:https://www.cnblogs.com/user-sunli/p/13889477.html
Copyright © 2011-2022 走看看