zoukankan      html  css  js  c++  java
  • Linux 服务器用户间ssh免密码登录

    1、本脚本为服务器用户间密码互信登录脚本
    2、依赖
    需要安装expect工具
    3、使用
    sh CreateUserssh.sh hadoop hadoop /home/hadoop/.ssh
    三个带入参数
    1)用户名
    2)密码
    3)用户ssh密钥路径
    4、脚本内容
    #!/bin/bash
    ####脚本变量
    v_time=`date +"%Y%m%d:%H:%M:%S"`
    v_date=`date +"%Y%m%d%H%M%S"`
    user=$1
    passw=$2
    gpsshdir=$3
    pwd_dir=`find / -name GPINSTALL  -type d 2>>/dev/null`
    hostdir=$pwd_dir/hostconfig
    h_hostfile=$hostdir/hostfile
    gpcnfdir=$pwd_dir/gpconfig
    scnf=$gpcnfdir/segment.cnf
    logdir=$pwd_dir/gplog
    log="$logdir/usersshkey-auto-$v_date.log"
    ####echo输出定义:INFO、ERROR、SUCESS
    EchoInfo(){
        echo -e "$v_time,$133[30;34m[INFO]33[0m--------------$2"
    }
    EchoError(){
        echo -e "$v_time,$133[30;31m[ERROR]33[0m-------------$2"
    }
    EchoSucess(){
        echo -e "$v_time,$133[30;32m[SUCESS]33[0m------------$2"
    }
    ####全局调用函数
    function JuState(){      ##状态判断函数
        if [ $1 -eq 0 ];then
            $2
        else
            $3
            $4
        fi
    }
    function Multinodeexec(){  ##ssh跳转执行函数
        for i in `cat $1`
        do
            ssh $i "
                $2
            "
        done
    }
    function NullValue(){    ##空值判断函数
        if [ ! "$1" ];then
            $2
            $3
        else
            $4
        fi
    }

    function CreateSshkeygen(){
        if [ -e $gpsshdir ];then
            rm -rf $gpsshdir
            ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ""
        else
            ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
            JuState $? "EchoSucess "ALL" "$user:Master:id_rsa创建成功"" "EchoError "ALL" "$user:Master:id_rsa创建失败"" "exit"
        fi
    }
    function ChecksshPasswd(){
        for i in `cat $1`;do
        ip=$i
    expect <<EE
    set timeout 5
    spawn ssh-copy-id -i $ip
    expect "Are you sure you want to continue connecting"
    send "yes "
    expect "*password"
    send "$passw "
    expect "#" {send "exit "}
    EE
    #    grep -w "Permission denied, please try again" $log >/dev/null
    #    if [ $? -eq 0 ];then
    #        EchoError "$ip" "root密码与配置文件不匹配,检查失败"
    #        exit
    #    else
    #        EchoSucess "$ip" "root密码与配置文件匹配,检查通过"
    #    fi
    done
    }
    function Sshcopyid_rsa(){
        for i in `cat $scnf`;do
            scp $gpsshdir/* $i:~/.ssh
            JuState $? "EchoSucess "$i" "$user:shcopyidrsa成功"" "EchoError "$i" "请检查服务器$user密码是否与密码配置文件一致"" "exit"
        done
    }
    function CheckSSH(){
        for i in `cat $1`
        do
            ssh $i "ls -l"
            JuState $? "EchoSucess "$i" "$user:ssh免密检查通过"" "EchoError "$i" "$user:ssh免密检查失败"" "exit"
        done
    }
    function HostUserSSH(){
        CreateSshkeygen
        ChecksshPasswd      $h_hostfile
        Sshcopyid_rsa
        CheckSSH        $h_hostfile
    }
    HostUserSSH
    5、运行日志
    [hadoop@data0 dependsoft]$ sh CreateUserssh.sh hadoop hadoop /home/hadoop/.ssh
    Generating public/private rsa key pair.
    Created directory '/home/hadoop/.ssh'.
    Your identification has been saved in /home/hadoop/.ssh/id_rsa.
    Your public key has been saved in /home/hadoop/.ssh/id_rsa.pub.
    The key fingerprint is:
    b7:4c:6e:ff:59:b1:fe:7c:80:8e:2d:c0:15:af:22:fb hadoop@data0
    The key's randomart image is:
    +--[ RSA 2048]----+
    |                 |
    |          .      |
    |           o     |
    |          . .    |
    |       .S.o. . . |
    |      . +=... . o|
    |       o o=+   o.|
    |      .  .o.o .+.|
    |       .E  ...o.=|
    +-----------------+
    spawn ssh-copy-id -i 192.168.56.60
    The authenticity of host '192.168.56.60 (192.168.56.60)' can't be established.
    RSA key fingerprint is 03:8f:ff:28:24:90:87:30:54:1c:d2:04:7f:b3:24:62.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '192.168.56.60' (RSA) to the list of known hosts.
    hadoop@192.168.56.60's password:
    Now try logging into the machine, with "ssh '192.168.56.60'", and check in:
      .ssh/authorized_keys
    to make sure we haven't added extra keys that you weren't expecting.
    spawn ssh-copy-id -i 192.168.56.61
    The authenticity of host '192.168.56.61 (192.168.56.61)' can't be established.
    RSA key fingerprint is 03:8f:ff:28:24:90:87:30:54:1c:d2:04:7f:b3:24:62.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '192.168.56.61' (RSA) to the list of known hosts.
    hadoop@192.168.56.61's password:
    Now try logging into the machine, with "ssh '192.168.56.61'", and check in:
      .ssh/authorized_keys
    to make sure we haven't added extra keys that you weren't expecting.
    spawn ssh-copy-id -i 192.168.56.62
    The authenticity of host '192.168.56.62 (192.168.56.62)' can't be established.
    RSA key fingerprint is 03:8f:ff:28:24:90:87:30:54:1c:d2:04:7f:b3:24:62.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '192.168.56.62' (RSA) to the list of known hosts.
    hadoop@192.168.56.62's password:
    Now try logging into the machine, with "ssh '192.168.56.62'", and check in:
      .ssh/authorized_keys
    to make sure we haven't added extra keys that you weren't expecting.
    authorized_keys                                                                                                                                                                                         100%  394     0.4KB/s   00:00
    id_rsa                                                                                                                                                                                                  100% 1675     1.6KB/s   00:00
    id_rsa.pub                                                                                                                                                                                              100%  394     0.4KB/s   00:00
    known_hosts                                                                                                                                                                                             100% 1185     1.2KB/s   00:00
    20191031:16:30:28,192.168.56.61[SUCESS]------------hadoop:shcopyidrsa成功
    authorized_keys                                                                                                                                                                                         100%  394     0.4KB/s   00:00
    id_rsa                                                                                                                                                                                                  100% 1675     1.6KB/s   00:00
    id_rsa.pub                                                                                                                                                                                              100%  394     0.4KB/s   00:00
    known_hosts                                                                                                                                                                                             100% 1185     1.2KB/s   00:00
    20191031:16:30:28,192.168.56.62[SUCESS]------------hadoop:shcopyidrsa成功
    total 4
    drwxr-xr-x 12 hadoop hadoop 4096 Oct 25 10:02 hadoop-2.8.5
    20191031:16:30:28,192.168.56.60[SUCESS]------------hadoop:ssh免密检查通过
    total 4
    drwxr-xr-x 12 hadoop hadoop 4096 Oct 25 10:02 hadoop-2.8.5
    20191031:16:30:28,192.168.56.61[SUCESS]------------hadoop:ssh免密检查通过
    total 4
    drwxr-xr-x 12 hadoop hadoop 4096 Oct 25 10:02 hadoop-2.8.5
    20191031:16:30:28,192.168.56.62[SUCESS]------------hadoop:ssh免密检查通过
    [hadoop@data0 dependsoft]$
    [hadoop@data0 dependsoft]$
    [hadoop@data0 dependsoft]$
    [hadoop@data0 dependsoft]$
    [hadoop@data0 dependsoft]$
    [hadoop@data0 dependsoft]$ cd
    [hadoop@data0 ~]$
    [hadoop@data0 ~]$
    [hadoop@data0 ~]$ ssh data0
    The authenticity of host 'data0 (192.168.56.60)' can't be established.
    RSA key fingerprint is 03:8f:ff:28:24:90:87:30:54:1c:d2:04:7f:b3:24:62.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'data0' (RSA) to the list of known hosts.
    Last login: Thu Oct 31 16:27:37 2019 from data1
    [hadoop@data0 ~]$ exit
    logout
    Connection to data0 closed.
    [hadoop@data0 ~]$ ssh data1
    The authenticity of host 'data1 (192.168.56.61)' can't be established.
    RSA key fingerprint is 03:8f:ff:28:24:90:87:30:54:1c:d2:04:7f:b3:24:62.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'data1' (RSA) to the list of known hosts.
    Last login: Thu Oct 31 16:25:28 2019 from data0
    [hadoop@data1 ~]$ exit
    logout
    Connection to data1 closed.
    [hadoop@data0 ~]$ ssh data2
    The authenticity of host 'data2 (192.168.56.62)' can't be established.
    RSA key fingerprint is 03:8f:ff:28:24:90:87:30:54:1c:d2:04:7f:b3:24:62.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'data2' (RSA) to the list of known hosts.
    Last login: Thu Oct 31 16:27:28 2019 from data1
    [hadoop@data2 ~]$ exit
    logout
    Connection to data2 closed.
  • 相关阅读:
    python-正则表达式
    python-定制类
    阿里云全新发布云市场能力中心,招募10个领域、500家供应商
    《DNS攻击防范科普系列3》 -如何保障 DNS 操作安全
    《DNS攻击防范科普系列2》 -DNS服务器怎么防DDoS攻击
    《DNS攻击防范科普系列1》—你的DNS服务器真的安全么?
    云栖干货回顾 |“顶级玩家”集结!分布式数据库专场精华解读
    AnalyticDB for PostgreSQL 6.0 新特性介绍
    云栖干货回顾 | 云原生数据库POLARDB专场“硬核”解析
    从零开始入门 K8s | 可观测性:你的应用健康吗?
  • 原文地址:https://www.cnblogs.com/xibuhaohao/p/11772047.html
Copyright © 2011-2022 走看看