mirror of
https://github.com/raymond999999/shell.git
synced 2026-03-12 17:51:35 +08:00
add shell files
This commit is contained in:
57
ssh_key/ssh_key.sh
Normal file
57
ssh_key/ssh_key.sh
Normal file
@@ -0,0 +1,57 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
#**********************************************************************************************
|
||||
#Author: Raymond
|
||||
#QQ: 88563128
|
||||
#Date: 2021-12-20
|
||||
#FileName: ssh_key.sh
|
||||
#URL: raymond.blog.csdn.net
|
||||
#Description: ssh_key for CentOS 7/8 & Ubuntu 18.04/24.04 & Rocky 8
|
||||
#Copyright (C): 2021 All rights reserved
|
||||
#*********************************************************************************************
|
||||
#基于key验证多主机ssh互相访问
|
||||
COLOR="echo -e \\033[01;31m"
|
||||
END='\033[0m'
|
||||
PASS=123456
|
||||
#设置网段最后的地址,4-255之间,越小扫描越快
|
||||
END=254
|
||||
|
||||
IP=`ip a s eth0 | awk -F'[ /]+' 'NR==3{print $3}'`
|
||||
NET=${IP%.*}.
|
||||
|
||||
os(){
|
||||
OS_ID=`sed -rn '/^NAME=/s@.*="([[:alpha:]]+).*"$@\1@p' /etc/os-release`
|
||||
}
|
||||
|
||||
ssh_key_push(){
|
||||
rm -f /root/.ssh/id_rsa
|
||||
[ -e ./SCANIP.log ] && rm -f SCANIP.log
|
||||
for((i=3;i<="$END";i++));do
|
||||
ping -c 1 -w 1 ${NET}${i} &> /dev/null && echo "${NET}${i}" >> SCANIP.log &
|
||||
done
|
||||
wait
|
||||
ssh-keygen -f /root/.ssh/id_rsa -P '' &> /dev/null
|
||||
if [ ${OS_ID} == "CentOS" -o ${OS_ID} == "Rocky" ] &> /dev/null;then
|
||||
rpm -q sshpass &> /dev/null || { ${COLOR}"安装sshpass软件包"${END};yum -y install sshpass &> /dev/null; }
|
||||
else
|
||||
dpkg -S sshpass &> /dev/null || { ${COLOR}"安装sshpass软件包"${END};apt -y install sshpass &> /dev/null; }
|
||||
fi
|
||||
sshpass -p ${PASS} ssh-copy-id -o StrictHostKeyChecking=no ${IP}
|
||||
|
||||
AliveIP=(`cat SCANIP.log`)
|
||||
for n in ${AliveIP[*]};do
|
||||
sshpass -p $PASS scp -o StrictHostKeyChecking=no -r /root/.ssh root@${n}:
|
||||
done
|
||||
|
||||
#把.ssh/known_hosts拷贝到所有主机,使它们第一次互相访问时不需要输入回车
|
||||
for n in ${AliveIP[*]};do
|
||||
scp /root/.ssh/known_hosts ${n}:.ssh/
|
||||
done
|
||||
}
|
||||
|
||||
main(){
|
||||
os
|
||||
ssh_key_push
|
||||
}
|
||||
|
||||
main
|
||||
48
ssh_key/ssh_key_push.sh
Normal file
48
ssh_key/ssh_key_push.sh
Normal file
@@ -0,0 +1,48 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
#**********************************************************************************************
|
||||
#Author: Raymond
|
||||
#QQ: 88563128
|
||||
#Date: 2021-11-19
|
||||
#FileName: ssh_key_push.sh
|
||||
#URL: raymond.blog.csdn.net
|
||||
#Description: ssh_key_push for CentOS 7/8 & Ubuntu 18.04/24.04 & Rocky 8
|
||||
#Copyright (C): 2021 All rights reserved
|
||||
#*********************************************************************************************
|
||||
COLOR="echo -e \\033[01;31m"
|
||||
END='\033[0m'
|
||||
|
||||
export SSHPASS=123456
|
||||
HOSTS="
|
||||
172.31.0.6
|
||||
172.31.0.7
|
||||
172.31.2.18
|
||||
172.31.2.20"
|
||||
|
||||
os(){
|
||||
OS_ID=`sed -rn '/^NAME=/s@.*="([[:alpha:]]+).*"$@\1@p' /etc/os-release`
|
||||
}
|
||||
|
||||
ssh_key_push(){
|
||||
rm -rf ~/.ssh/id_rsa*
|
||||
ssh-keygen -f /root/.ssh/id_rsa -P '' &> /dev/null
|
||||
if [ ${OS_ID} == "CentOS" -o ${OS_ID} == "Rocky" ] &> /dev/null;then
|
||||
rpm -q sshpass &> /dev/null || { ${COLOR}"安装sshpass软件包"${END};yum -y install sshpass &> /dev/null; }
|
||||
else
|
||||
dpkg -S sshpass &> /dev/null || { ${COLOR}"安装sshpass软件包"${END};apt -y install sshpass &> /dev/null; }
|
||||
fi
|
||||
for i in $HOSTS;do
|
||||
{
|
||||
sshpass -e ssh-copy-id -o StrictHostKeyChecking=no -i /root/.ssh/id_rsa.pub $i &> /dev/null
|
||||
[ $? -eq 0 ] && echo $i is finished || echo $i is false
|
||||
}&
|
||||
done
|
||||
wait
|
||||
}
|
||||
|
||||
main(){
|
||||
os
|
||||
ssh_key_push
|
||||
}
|
||||
|
||||
main
|
||||
Reference in New Issue
Block a user