• linux免密登录配置


    第一步:安装openssh-clients
      yum install -y openssh-clients.x86_64
    第二步:生成密钥
      ssh-keygen
    第三步:拷贝公钥到其他机器
      ssh-copy-id hostname

    使用脚本批量配置每台机器互相免密登录

      环境:centos6.10

      安装脚本编程交互工具expect

        yum install -y expect

      脚本编写,小例子

      

      generatekey.expect

    #!/usr/bin/expect
    spawn ssh-keygen
    expect "*id_rsa*"
    send
    expect {
    "*verwrite*" {
    send y
    expect "*passphrase*"
    send
    expect "*again*"
    send
    }
    "*assphrase*" {
    send
    expect "*again*"
    send
    }
    }
    expect "*randomart*"
    send_user "generate successful "

      

      copykey.expect

    #!/usr/bin/expect
    set from [lindex $argv 0]
    set hostname [lindex $argv 1]
    spawn ssh-copy-id $from@$hostname
    expect {
    "*assword*" { # "*password*"和左大括号之见一定要留间隔,否则不能识别
    send wanger
    expect "*check*"
    send_user "add to $hostname successful! "
    }
    "*yes/no*" {
    send yes
    expect "*assword*"
    send wanger
    expect "*check*"
    send_user "add to $hostname successful! "
    }
    "*check*" {
    send_user "already added to $hostname! "
    }
    }

     

      machines:把所有机器的hostname添加到这个文件

    tdh1
    tdh2
    tdh3
    tdh4

     

      passphraseless_onetomutiple.sh

    #!/bin/bash
    dirname=/home/wanger/code/passphraseless
    $dirname/generatekey.expect
    input="$dirname/machines"
    while read -r hostname
    do
    $dirname/copykey.expect $1 $hostname
    done < $input

      

      passphraseless_all

    #!/bin/bash
    current_host=`hostname`
    dirname=/home/wanger/code/passphraseless
    input=$dirname/machines
    # echo $current_host
    $dirname/passphraseless_onetomutiple.sh wanger
    
    for hostname in `awk  '$1' $input`
    do
    if [ $hostname != $current_host ];
    then
      scp -r ../passphraseless wanger@$hostname:/home/wanger/code/
      $dirname/scp.expect $hostname
      ssh wanger@$hostname $dirname/passphraseless_onetomutiple.sh wanger
    fi
    done

      

      给执行权限后,在任意一台机器上执行./passphraseless_all即可

    root版本配置代码在github上: https://github.com/cqdxwjd/code/tree/master/linux/shell/passphraseless_root

  • 相关阅读:
    P3916 图的遍历 题解
    NBL小可爱纪念赛「 第一弹 」 游记(部分题解)
    P4147 玉蟾宫 题解
    十、一些小例子
    九、基础正则表达式BRE
    八.linux系统文件属性知识
    七、linux目录结构知识---实战
    六、linux目录结构知识
    3.20-30岁形成好的习惯
    五、Centos linux系统优化-实战
  • 原文地址:https://www.cnblogs.com/cqdxwjd/p/9813048.html
Copyright © 2020-2023  润新知