============== problem: the remote management hosts ip and push to get the public key to the 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.'
# Where you can use read -p read variables to ensure safety
#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
be
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..."
====== implementation of the results ==========================================
[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:
+ --- [RS 2048] ---- +
| ...BX*o |
| o =. = +. . |
| . ++ oo. o |
| . ++ = oo |
| O So + + o +. |
| ..or + + oo. |
| .And + |
| . = ..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
[email protected]'s password: [email protected]'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...
===== directly connect to a remote machine to perform the ifconfig command
[root@Manager scripts]# ssh [email protected] "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