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,$1\033[30;34m[INFO]\033[0m--------------$2"
}
EchoError(){
    echo -e "$v_time,$1\033[30;31m[ERROR]\033[0m-------------$2"
}
EchoSucess(){
    echo -e "$v_time,$1\033[30;32m[SUCESS]\033[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\r"
expect "*password"
send "$passw\r"
expect "#" {send "exit\r"}
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.

Linux 服务器用户间ssh免密码登录的更多相关文章

  1. 多台CentOS服务器下实现SSH免密码登录

    ROOT用户下实现SSH免密码登录 第一步:进入目录/root/.ssh $ cd  /root/.ssh/ 执行以下命令,会在当前目录下生成公钥(id_rsa.pub)/私钥(id_rsa)对 第二 ...

  2. Linux多台服务器间SSH免密码登录配置

    SSH实现各个服务器间的文件相互备份,如运行scp命令,可以实现免密码登录,从而可以使用SHELL脚本实现一些自动化的处理. 假如A机要免密码登录B机,具体方法如下: 1.在A机运行:"ss ...

  3. linux下设置了SSH免密码登录但还是需要输入密码的解决办法

    今天在设置linux的免密码登录后,仍然需要输入密码,后来找到了原因,是因为用户没有权限修改.ssh目录下的know_hosts文件导致的. 具体情况是这样的: 在/home/username/.ss ...

  4. linux 服务器名 访问 shh免密码登录

    以根用户登录,或者登录后切换到根用户,然后在提示符下输入hostname命令,可以看出当前系统的主机名为localhost.localdomain.   更改/etc/sysconfig下的netwo ...

  5. Mac下到Linux主机ssh免密码登录

    最近忙得忘乎所以,写篇博客放松放松,RT,直接上命令好了 # Local ssh-keygen -t rsa scp ~/.ssh/id_rsa.pub username@server:~/.ssh/ ...

  6. Linux下SSH免密码登录

    转自:http://haitao.iteye.com/blog/1744272 ssh配置 主机A:10.0.5.199 主机B:10.0.5.198 需要配置主机A无密码登录主机A,主机B 先确保所 ...

  7. Linux 配置ssh 免密码登录

    在平常应用中,我们经常会登录到其他主机,比如说服务器,每次都需要用户名和密码. 我们可以通过ssh免密码登录服务器而不需要输入密码. 现在有一台ubuntu的阿里云服务器,称之为 server.  公 ...

  8. Mac ssh 免密码登录 Mac 或者 Linux

    最近在 Mac上操作另一台 Mac 和 Linux 服务器,每次输密码太麻烦.所以直接设置 ssh 免密码登录,省去输入密码的过程.先在本机执行 ls ~/.ssh 若不存在 id_rsa,id_rs ...

  9. linux实现ssh免密码登录

    linux实现ssh免密码登录 本地系统执行 ssh-keygen -t rsa 命令,生成密钥文件 在相应的目录下查看生成的密钥文件,其中:id_rsa为私钥文件,id_rsa.pub为公钥文件 本 ...

随机推荐

  1. python实战项目 — 爬取 妹子图网,保存图片到本地

    重点: 1. 用def函数 2. 使用 os.path.dirname("路径保存") , 实现每组图片保存在独立的文件夹中 方法1: import requests from l ...

  2. Django content-type组件

    介绍 Django包含一个contenttypes应用程序(app),可以跟踪Django项目中安装的所有模型(Model),提供用于处理模型的高级通用接口. Contenttypes应用的核心是Co ...

  3. 【BZOJ 2351】Matrix(Hash)

    题目链接 二维\(Hash\)类似二维前缀和,每一行看成一个\(h\)进制数,每一个以(1,1)为左上角的矩阵看成一个由每一行的\(Hash\)值组成的\(l\)进制数. 然后自己推推柿子就行. #i ...

  4. 如何导出ane所需的swc

    来源:http://blog.sina.com.cn/s/blog_6471e1bb01012ard.html 打包.ane文件的时候需要用到ActionScript的扩展库(swc文件),那么如何生 ...

  5. RedHat6.9下替换yum源

    因为RedHat的yum需要收费,且要注册后才能使用.因此想把Yum源更新为CentOS的.使用的RedHat版本为6.9,因此对应的CentOS版本也要为6.9 1. 检查并删除原有的yum源 rp ...

  6. jenkins rpm卸载

    rpm卸载 1.rpm -e jenkins rpm -ql jenkins 检查是否卸载成功 2.彻底删除残留文件:find / -iname jenkins | xargs -n 1000 rm ...

  7. 交易开拓者旗舰版(TB旗舰版)软件升级中如何迁移用户数据

    方法一: 1.导出工作区,桌面和系统设置.操作:文件->导入和导出 2.导出公式.操作:tb公式->公式导入导出. 3.拷贝工作室文件.目录在:user\tb用户名\Workroom 4. ...

  8. Python入门篇-基础语法

    Python入门篇-基础语法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.编程基础 1>.程序 一组能让计算机识别和执行的指令. 程序 >.算法+ 数据结构= 程 ...

  9. PTA-多项式A除以B

    多项式A除以B 这仍然是一道关于A/B的题,只不过A和B都换成了多项式.你需要计算两个多项式相除的商Q和余R,其中R的阶数必须小于B的阶数. 输入格式: 输入分两行,每行给出一个非零多项式,先给出A, ...

  10. css定位基础知识

    标题:css定位 地址:https://www.w3school.com.cn/css/css_positioning.asp