运维开发网
广告位招商联系QQ:123077622
 
广告位招商联系QQ:123077622

利用expect实现三个Linux主机之间的key验证

运维开发网 https://www.qedev.com 2021-04-28 14:25 出处:51CTO 作者:qsyj
CentOS系统之间使用key认证登录

实验环境:

    主机1:172.18.61.7(CentOS 7.9),主机2:172.18.61.8(CentOS 8.3),主机3:172.18.61.18(CentOS 8.3)

话不多说,直接上脚本

#!/bin/bash

#Author:QsyjSmy

COLOR="echo -e \E[1;32m"

END="\E[0m"

PASSWORD=123456

IPLIST="

172.18.61.7

172.18.61.8

172.18.61.18

"

[ ! -f ~/.ssh/id_rsa ] && ssh-keygen -P "" -f ~/.ssh/id_rsa &> /dev/null    #生成秘钥对

[ ! -f ~/.ssh/authorized_keys ] && cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys    #利用公钥生成authorized_keys文件

rpm -q expect &> /dev/null || yum -y -q install expect &> /dev/null    #安装expect

for ip in $IPLIST

do

        {

        expect <<EOF

        set timeout 60    #设置超时时间

        spawn scp /root/.ssh/authorized_keys /root/.ssh/id_rsa /root/.ssh/id_rsa.pub $ip:/root/.ssh/    #分发相同的秘钥对以及authorized_keys文件,以实现三台主机之间相互登录

        expect  {    #自动登录

                "yes/no" { send "yes\r";exp_continue }

                "password:" { send "$PASSWORD\r" }

        }

        expect eof

EOF

        $COLOR"$ip is ready"$END

        }&

done

wait

$COLOR"Push ssh key is finished!"$END

执行完脚本之后,在三台主机上进行验证:

主机1

[[email protected] ~]# ssh -o StrictHostKeyChecking=no 172.18.61.8    #禁止首次连接询问过程

Warning: Permanently added '172.18.61.8' (ECDSA) to the list of known hosts.

Last login: Tue Apr 27 23:01:23 2021 from 172.18.61.18

[[email protected] ~]# exit 

logout

Connection to 172.18.61.8 closed.

[[email protected] ~]# ssh 172.18.61.8    #再次登录不会出现连接询问

Last login: Tue Apr 27 23:07:32 2021 from 172.18.61.7

[[email protected] ~]# exit 

logout

Connection to 172.18.61.8 closed.

[[email protected] ~]# ssh -o StrictHostKeyChecking=no 172.18.61.18

Warning: Permanently added '172.18.61.18' (ECDSA) to the list of known hosts.

Last login: Tue Apr 27 23:06:45 2021 from 172.18.61.8

[[email protected] ~]# exit 

logout

Connection to 172.18.61.18 closed.

[[email protected] ~]# ssh 172.18.61.18

Last login: Tue Apr 27 23:07:49 2021 from 172.18.61.7

[[email protected] ~]# exit

logout

Connection to 172.18.61.18 closed.

[[email protected] ~]# ll .ssh/

total 16

-rw-r--r--. 1 root root  577 Apr 27 23:05 authorized_keys

-rw-------. 1 root root 2610 Apr 27 23:05 id_rsa

-rw-r--r--. 1 root root  577 Apr 27 23:05 id_rsa.pub

-rw-r--r--. 1 root root  347 Apr 27 23:07 known_hosts

主机2

[[email protected] ~]# ssh -o StrictHostKeyChecking=no 172.18.61.7

Warning: Permanently added '172.18.61.7' (ECDSA) to the list of known hosts.

Last login: Tue Apr 27 23:54:58 2021 from 172.18.61.8

[[email protected] ~]# exit

logout

Connection to 172.18.61.7 closed.

[[email protected] ~]# ssh 172.18.61.7

Last login: Tue Apr 27 23:57:16 2021 from 172.18.61.18

[[email protected] ~]# exit

logout

Connection to 172.18.61.7 closed.

[[email protected] ~]# ssh -o StrictHostKeyChecking=no 172.18.61.18

Warning: Permanently added '172.18.61.18' (ECDSA) to the list of known hosts.

Last login: Tue Apr 27 23:55:04 2021 from 172.18.61.8

[[email protected] ~]# exit

logout

Connection to 172.18.61.18 closed.

[[email protected] ~]# ssh 172.18.61.18

Last login: Tue Apr 27 23:57:40 2021 from 172.18.61.18

[[email protected] ~]# exit

logout

Connection to 172.18.61.18 closed.

[[email protected] ~]# ll .ssh/

total 16

-rw-r--r--. 1 root root  577 Apr 27 23:05 authorized_keys

-rw-------. 1 root root 2610 Apr 27 23:05 id_rsa

-rw-r--r--. 1 root root  577 Apr 27 23:05 id_rsa.pub

-rw-r--r--. 1 root root  347 Apr 27 23:57 known_hosts

主机3

[[email protected] ~]# ssh -o StrictHostKeyChecking=no 172.18.61.7

Last login: Tue Apr 27 23:57:29 2021 from 172.18.61.18

[[email protected] ~]# exit

logout

Connection to 172.18.61.7 closed.

[[email protected] ~]# ssh 172.18.61.7

Last login: Wed Apr 28 00:01:25 2021 from 172.18.61.18

[[email protected] ~]# exit

logout

Connection to 172.18.61.7 closed.

[[email protected] ~]# ssh -o StrictHostKeyChecking=no 172.18.61.8

Warning: Permanently added '172.18.61.8' (ECDSA) to the list of known hosts.

Last login: Tue Apr 27 23:09:19 2021 from 172.18.61.18

[[email protected] ~]# exit

logout

Connection to 172.18.61.8 closed.

[[email protected] ~]# ssh 172.18.61.8

Last login: Wed Apr 28 00:01:42 2021 from 172.18.61.18

[[email protected] ~]# exit

logout

Connection to 172.18.61.8 closed.

[[email protected] ~]# ll .ssh/

total 16

-rw-r--r--. 1 root root  577 Apr 27 23:05 authorized_keys

-rw-------. 1 root root 2610 Apr 27 23:05 id_rsa

-rw-r--r--. 1 root root  577 Apr 27 23:05 id_rsa.pub

-rw-r--r--. 1 root root  520 Apr 28 00:01 known_hosts

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号