shellshell脚本

shell脚本之批量安装redis

2019-07-19  本文已影响1人  ImitationShow丶吃

大家好!今天展示一下shell的新玩法

首先环境是centos7.5 虚拟软件为kvm:

#用脚本生成虚拟机

#!/bin/bash

. /etc/init.d/functions

CONF_DIR=/etc/libvirt/qemu

IMG_DIR=/var/lib/libvirt/images

BASEVM=node_tedu

createvm(){

qemu-img create -f qcow2 -b ${IMG_DIR}/.${BASEVM}.qcow2 $IMG_DIR/${1}.img 30G &> /dev/null

sed  "s/${BASEVM}/${1}/" ${IMG_DIR}/.${BASEVM}.xml > ${CONF_DIR}/${1}.xml

sudo virsh define ${CONF_DIR}/${1}.xml &> /dev/null

echo_success

echo "VM ${1} Create"

}

read -p "Enter VM number: "  VMNUM

if [ -z $VMNUM  ];then

echo    Please enter parameters

exit

fi

if [ -z "${VMNUM}" ]; then

    echo "You must input a number."

    exit 65

elif [ $(echo ${VMNUM}*1 | bc) = 0 ]; then

    echo "You must input a number."

    exit 66

elif [ ${VMNUM} -lt 1 -o ${VMNUM} -gt 99 ]; then

    echo "Input out of range"

    exit 67

fi

if  [ ${VMNUM} -le 9 ];then

  VMNUM=0${VMNUM}

fi

if [ $USER == student ];then

  NEWVM=tedu_node

elif [ $USER == weekend ];then

  NEWVM=tmooc_node

else

  NEWVM=tedu_node

fi

vm_name=${NEWVM}${VMNUM}

if [ -e $IMG_DIR/${vm_name}.img ]; then

  echo_warning

  echo "vm ${vm_name} is exists"

  exit 38

else

  createvm ${vm_name}

fi

###################################

用脚本就可以快速生成虚拟机 192.168.4.{60..67}

然后在本机上执行一键安装脚本

#!/bin/bash

for i in {60..67}

do

scp $pwd/redis root@192.168.4.$i:~

ssh 192.168.4.$i " yum -y install gcc

                tar -xf /root/redis/redis-4.0.8.tar.gz

                cd /root/redis-4.0.8

                make

                make install

                /root/redis-4.0.8/utils/install_server.sh  <  /dev/null "

done

# /root/redis-4.0.8/utils/install_server.sh  <  /dev/null  可以直接跳过回车


上一篇下一篇

猜你喜欢

热点阅读