1. 程式人生 > >同網段存活IP公鑰分發指令碼

同網段存活IP公鑰分發指令碼

#!/bin/bash

function bendiyum(){
umount /dev/sr0 &> /dev/null
[ -e /media/cdrom ] || mkdir -p /media/cdrom
mount /dev/sr0 /media/cdrom &> /dev/null
if [ $? -ne 0 ];then
        echo "請插入光碟"       
        exit
fi
cd /etc/yum.repos.d || mkdir -p /etc/yum.repos.d
mv /etc/yum.repos.d/* /tmp
cat > /etc/yum.repos.d/localyum.repo << FOF
[local]
name=local
baseurl=file:///media/cdrom/
gpgcheck=0
enabled=1
FOF
yum -y clean all &> /dev/null
yum makecache &> /dev/null
[ $? -eq 0 ] && echo "成功搭建yum倉庫" || echo "yum倉庫搭建失敗"
}

function yunyum(){
which wget
if [ $? -ne 0 ];then
        bendiyum
        yum -y install wget &> /dev/null
        if [ $? -ne 0 ];then
                echo "安裝wget失敗"
                exit
        fi
else
        ping -c 2 www.baidu.com &> /dev/null
        if [ $? -ne 0 ];then
                echo "網路不通"
                exit
        else
                wget -O /etc/yum.repos.d/epel.repo 
http://mirrors.aliyun.com/repo/epel-6.repo &> /dev/null if [ $? -eq 0 ];then yum -y clean all &> /dev/null yum makecache &> /dev/null echo "雲yum倉庫搭建成功" else echo "雲yum倉庫搭建失敗" fi fi fi } which scp &> /dev/null if [ $? -ne 0 ];then yum -y install openssh-clients &> /dev/null if [ $? -ne 0 ];then echo "安裝scp失敗" exit fi fi which sshpass &> /dev/null yum -y install sshpass &> /dev/null if [ $? -ne 0 ];then echo "安裝sshpass失敗" exit fi fi [ -e /root/.ssh/id_rsa_pub ] if [ $? -ne 0 ];then mv /root/.ssh/* /tmp &> /dev/null ssh-keygen -f/root/.ssh/id_rsa -P "" &> /dev/null fi [ -e /root/.ssh/id_rsa ] if [ $? -ne 0 ];then mv /root/.ssh/* /tmp &> /dev/null ssh-keygen -f/root/.ssh/id_rsa -P "" &> /dev/null fi for i in 192.168.200.{1..254} do ping -c 1 -i 0.1 $i > /dev/null if [ $? == 0 ];then sshpass -p "jinchuan" ssh-copy-id -i /root/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no $i" &> /dev/null if [ $? == 0 ];then echo "$i 公鑰分發成功" fi fi done