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
  • 相关阅读:
    myeclipse源码相关操作
    来自一个程序员内心深处的心声
    编程乐趣--汉字转拼音
    MyEclipse下安装FreeMark插件
    java web 加载Spring --web.xml 篇
    注解方式实现声明式事务管理
    spring与struts简单整合案例
    创建对象与对象依赖关系
    几种对数据的处理以及对数据的封装
    Action开发、通配符、路径问题和struts中常量用法
  • 原文地址:https://www.cnblogs.com/jonyq/p/13063581.html
Copyright © 2011-2022 走看看