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

虚拟机脚本

2016-07-21 17:35 387 查看
#!/bin/bash
#author my
#mk virtual

add() {
if [[ "$1" =~ [[:digit:]]+ ]]
then
local w=`ls /httpdtest/html/ | grep -o "[[:digit:]]\+" | sort -n | tail -1 |grep -o "[[:digit:]]*"`
local y=$[$w+1]
local z=$[$y-1+$1]
for i in `seq $y $z`
do
touch /etc/httpd/conf.d/virtualhost${i}.conf
mkdir -pv /httpdtest/html/www$i > /dev/null
touch /httpdtest/html/www${1}/index.html
echo -e "<h>test$i</h>" > /httpdtest/html/www${i}/index.html
echo -e "<VirtualHost 172.16.7.3:80>\nServerName www${i}.mymy.com\nDocumentRoot /httpdtest/html/www${i}\n</VirtualHost>" > /etc/httpd/conf.d/virtualhost${i}.conf
echo "172.16.7.3 www${i}.mymy.com" >> /etc/hosts
service httpd reload >/dev/null
done
else
echo "sorry ,add + number"
fi
}

del() {
#local w=`ls /httpdtest/html/ | sort -n | tail -1 |grep -o "[[:digit:]]*"`
#local y=$[$w+1]
#local z=$[$w+1-$1]
if [[ "$1" =~ [[:digit:]]+ ]]
then
#for i in `seq $z $w`
# do
#rm -rf /httpdtest/html/www$i >/dev/null
rm -rf /etc/httpd/conf.d/virtualhost${1}.conf >/dev/null
#done
else
case $1 in
"-a")
read -p "sure ? (y/n)" m
if [ "$m" == "y" ]
then
rm -rf /etc/httpd/conf.d/virtualhost* >/dev/null
else
exit
fi
;;
*)
echo "input number or -a "
;;
esac
fi
}

list() {
if [ "$1" == "-a" ]
then
httpd -S |sed -n '/^V/,/^w/p' | sed '/^w/d'
else
if [[ "$1" =~ [[:digit:]]+ ]]
then
file=/etc/httpd/conf.d/virtualhost`echo $1`.conf
if [ -e "$file" ]
then
cat $file
else
echo " no exist"
read -p "do you want to create (y/n):" p
case $p in
y)
touch /etc/httpd/conf.d/virtualhost${1}.conf
mkdir -pv /httpdtest/html/www$1 > /dev/null
touch /httpdtest/html/www${1}/index.html
echo -e "<h>test$1</h>" > /httpdtest/html/www${1}/index.html
echo -e "<VirtualHost 172.16.7.3:80>\nServerName www${1}.mymy.com\nDocumentRoot /httpdtest/html/www${1}\n</VirtualHost>" > /etc/httpd/conf.d/virtualhost${1}.conf
echo "172.16.7.3 www${1}.mymy.com" >> /etc/hosts
service httpd reload >/dev/null
;;
*)
exit
;;
esac
fi
else
echo "sorry,just number"
fi
fi
}

case $1 in
list*)
list $2
;;
add*)
add $2
;;
del*)
del $2
;;
*)
echo "error input"
;;
esac
#if [[ "$k" =~ \+[[:digit:]]+ ]]
# then

#       w=`ls /httpdtest/html/ | sort -n | tail -1 |grep -o "[[:digit:]]*"`
#       y=$[$w+1]
#       z=$[$y+$k-1]
#               for i in `seq $y $z`
#                do
#                       p=`echo $k | grep -o "[[:digit:]]\+"`
#                       mkvirtual $p
#                done
#elif [[ "$k" =~ \-[[:digit:]]+ ]]
# then
#                       r=`echo $k | grep -o "[[:digit:]]\+"
#                      delvirtual $r
#else
#       echo "an yao qiu shu ru"
#fi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LINUX