zoukankan      html  css  js  c++  java
  • 自动ssh免密配置脚本

    因为调用ssh需要免密,使用脚本加配置文件自动批量免密。脚本如下

    hostip=$1
    name=$2
    PASSWORD=$3
    需要脚本添加上面三个参数或者通过循环一个host文件实现批量即可。
    #!/bin/bash
    #yum安装expect
    #yum -y install expect
    #PWD_1是登陆密码,可以自己设定
    #PASSWORD=123456
    #hosts="master slave1 slave2"
    #
    key_generate() {
        expect -c "set timeout -1;
            spawn ssh-keygen -t rsa;
            expect {
                {Enter file in which to save the key*} {send -- 
    ;exp_continue}
                {Enter passphrase*} {send -- 
    ;exp_continue}
                {Enter same passphrase again:} {send -- 
    ;exp_continue}
                {Overwrite (y/n)*} {send -- n
    ;exp_continue}
                eof             {exit 0;}
        };"
    }
    auto_ssh_copy_id () {
        expect -c "set timeout -1;
            spawn ssh-copy-id -i $HOME/.ssh/id_rsa.pub $1@$2;
                expect {
                    {Are you sure you want to continue connecting *} {send -- yes
    ;exp_continue;}
                    {*password:} {send -- $3
    ;exp_continue;}
                    eof {exit 0;}
                };"
    }
    
    check_cmd () {
        if [[ $? == 0 ]];then
            echo "$1 SUCCESS"
        else
            echo "$1 ERROR"
            exit
        fi
    }
    
    chmod +x ssh_init.exp 
    if [[ ! -e $HOME/.ssh/id_rsa.pub ]]; then
      key_generate && check_cmd "ssh key creat"
    fi
    hostip=$1
    name=$2
    PASSWORD=$3
    auto_ssh_copy_id $name $hostip  $PASSWORD
    ./ssh_init.exp $name $hostip  $PASSWORD
    check_cmd "ssh scripts run cmd"

    做一个秘钥检测,检测是否成功。

    ssh_init.exp 内容如下
    #!/usr/bin/expect -f
     
    set timeout 2
     
    set name [lindex $argv 0]
    set node [lindex $argv 1]
    set pawd [lindex $argv 2]
     
    spawn ssh ${name}@${node}
    expect {
        "*yes/no*" {send "yes
    ";exp_continue}
        "*password:" {send "$pawd
    "}
    }
     
    #expect "*${name}@${node}*"
    #send "ssh-keygen -t rsa -P ''
    "
    #expect "*ssh/id_rsa):"
    #send "
    "
    #expect {
    #    "Overwrite (y/n)?" {send "y
    ";exp_continue}
    #    "*${name}@${node}*" {send "exit
    "}
    #}
    expect eof
    exit
  • 相关阅读:
    URAL——DFS找规律——Nudnik Photographer
    URAL1353——DP——Milliard Vasya's Function
    URAL1203——DPor贪心——Scientific Conference
    递推DP HDOJ 5389 Zero Escape
    区间DP UVA 1351 String Compression
    树形DP UVA 1292 Strategic game
    Manacher HDOJ 5371 Hotaru's problem
    同余模定理 HDOJ 5373 The shortest problem
    递推DP HDOJ 5375 Gray code
    最大子序列和 HDOJ 1003 Max Sum
  • 原文地址:https://www.cnblogs.com/jonyq/p/13063581.html
Copyright © 2011-2022 走看看