美文网首页
shell脚本批量传密钥

shell脚本批量传密钥

作者: 南南宫问天 | 来源:发表于2020-04-27 15:42 被阅读0次
[root@db scripts]# cat expectssh2.0.sh 
#!/bin/bash
pass=redhat
>ip.txt
rpm -q expect &>/dev/null || yum install expect -y ##判断是否有expect解释器,没有就安装
[ -f ~/.ssh/id_rsa ] || ssh-keygen -P "" -f "/root/.ssh/id_rsa"  ##判断是否有密钥,没有就生成
for i in {2..254}
do
{
    ip=172.16.210.$i
    ping -c1 -W1 $ip &> /dev/null
    if [ $? -eq 0 ];then
    echo "$ip" >> ip.txt
    /usr/bin/expect <<-EOF  
    set timeout 5
    spawn ssh-copy-id -f $ip 
    expect {
        "yes/no" { send "yes\r"; exp_continue }     
        "password" { send "$pass\r" }
    }
    expect eof
    EOF
    fi
}&
done
wait

echo "finish........"
[root@db scripts]# ./expectssh2.0.sh 
Generating public/private rsa key pair.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:5eSKLNY8aQRuDs7ujmHKy1o3SAK0tViyO64wcn4Wxts root@db
The key's randomart image is:
+---[RSA 2048]----+
| o o             |
|. B .            |
|.+ ..     o      |
|. .. .   =       |
|.oo.o . S o      |
|.=.=+= o .       |
|=o*.*+O .        |
|OO o++E.         |
|***o             |
+----[SHA256]-----+
[root@db scripts]# ./expectssh2.0.sh 
spawn ssh-copy-id -f 172.16.210.11
spawn ssh-copy-id -f 172.16.210.13
spawn ssh-copy-id -f 172.16.210.30
spawn ssh-copy-id -f 172.16.210.36
spawn ssh-copy-id -f 172.16.210.40
spawn ssh-copy-id -f 172.16.210.12
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
root@172.16.210.11's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '172.16.210.30'"
and check to make sure that only the key(s) you wanted were added.

expect: spawn id exp6 not open
    while executing
"expect eof"

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '172.16.210.36'"
and check to make sure that only the key(s) you wanted were added.

expect: spawn id exp6 not open
    while executing
"expect eof"

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '172.16.210.11'"
and check to make sure that only the key(s) you wanted were added.


Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '172.16.210.12'"
and check to make sure that only the key(s) you wanted were added.


Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '172.16.210.13'"
and check to make sure that only the key(s) you wanted were added.

expect: spawn id exp6 not open
    while executing
"expect eof"
expect: spawn id exp6 not open
    while executing
"expect eof"

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '172.16.210.40'"
and check to make sure that only the key(s) you wanted were added.

expect: spawn id exp6 not open
    while executing
"expect eof"
finish........
[root@db scripts]# cat ip.txt  ##传送成功的ip会显示在这
172.16.210.11
172.16.210.13
172.16.210.30
172.16.210.36
172.16.210.40
172.16.210.12
[root@db scripts]# ssh 172.16.210.11 ##尝试连接
Last login: Mon Apr 27 15:37:59 2020 from 172.16.210.36
[root@master-11 ~]# 登出    ##成功连接
Connection to 172.16.210.11 closed.
[root@db scripts]# ssh 172.16.210.12
Last login: Mon Apr 27 15:38:03 2020 from 172.16.210.36
[root@node1-12 ~]# 登出
Connection to 172.16.210.12 closed.
[root@db scripts]# ssh 172.16.210.13
Last login: Thu Apr  9 20:36:55 2020 from 172.16.210.250
[root@node2-13 ~]# 登出
Connection to 172.16.210.13 closed.

相关文章

网友评论

      本文标题:shell脚本批量传密钥

      本文链接:https://www.haomeiwen.com/subject/vhaywhtx.html