您的位置:首页 > 运维架构 > Shell

100例shell脚本之八远程管理获得hosts ip以及推送公钥到hosts

2019-06-07 18:48 1406 查看

==============问题:远程管理获得hosts ip以及推送公钥到hosts========================================

[root@Manager scripts]# cat getip_push_pkey.sh 

#!/bin/bash

#get hosts ip push public key to hosts


>ip.txt

ip_prefix='10.0.0.'

#这里可以用read -p读入变量,保证安全性

#password="123456"

read -p  "Enter remote host password: " -s password


ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa


which expect &>/dev/null

if [ $? -ne 0 ];then

        yum install expect -y &>/dev/null

fi


for i in {40..60}

do

        { ip=$ip_prefix$i    

        ping -c1 -w1 $ip &>/dev/null

        if [ $? -eq 0 ];then

                echo $ip>>ip.txt

                /usr/bin/expect <<-EOF

                spawn ssh-copy-id $ip

                expect {

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

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


                }

                expect eof                

                EOF

        fi }&

done

wait

echo "finish..."

======执行效果==========================================

[root@Manager scripts]# sh getip_push_pkey.sh 

Generating public/private rsa key pair.

Created directory '/root/.ssh'.

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:PrGrvdltlSxYzIEY5hFFZskKD/SyXETFup0Xd6S3pOY root@Manager

The key's randomart image is:

+---[RSA 2048]----+

|     ...BX*o     |

|      o=.+= .   .|

|      .++o o . o |

|     . ++   = o.o|

|      o So + +o+.|

|       ..o+ oo+. |

|        +  .oo   |

|       . = ..E   |

|      ..=....    |

+----[SHA256]-----+

spawn ssh-copy-id 10.0.0.40

spawn ssh-copy-id 10.0.0.50

/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"

The authenticity of host '10.0.0.50 (10.0.0.50)' can't be established.

ECDSA key fingerprint is SHA256:ShTey9zjYvAvkDP2H5cXpZOa7xz21fvjF4EE/KEqiZA.

ECDSA key fingerprint is MD5:fb:70:c1:d4:a8:4f:96:b7:7e:a0:5f:75:2e:04:b6:7f.

Are you sure you want to continue connecting (yes/no)? The authenticity of host '10.0.0.40 (10.0.0.40)' can't be established.

ECDSA key fingerprint is SHA256:ShTey9zjYvAvkDP2H5cXpZOa7xz21fvjF4EE/KEqiZA.

ECDSA key fingerprint is MD5:fb:70:c1:d4:a8:4f:96:b7:7e:a0:5f:75:2e:04:b6:7f.

Are you sure you want to continue connecting (yes/no)? yes

yes

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

root@10.0.0.50's password: root@10.0.0.40's password: 



Number of key(s) added: 1


Now try logging into the machine, with:   "ssh '10.0.0.50'"

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 '10.0.0.40'"

and check to make sure that only the key(s) you wanted were added.


finish...

=====直接连接远程机器执行ifconfig命令

[root@Manager scripts]# ssh root@10.0.0.40 "ifconfig"

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 10.0.0.40  netmask 255.255.255.0  broadcast 10.0.0.255

        inet6 fe80::401:d3d8:1c5f:9890  prefixlen 64  scopeid 0x20<link>

        inet6 fe80::9a8:50bc:a079:f518  prefixlen 64  scopeid 0x20<link>

        ether 00:0c:29:e5:fb:7f  txqueuelen 1000  (Ethernet)

        RX packets 1444  bytes 146322 (142.8 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: