写一个脚本,批量生成10个虚拟主机配置:
/etc/httpd/conf.d/vhosts#.conf
主机名:www#
目录:/data/vhosts/www#
访问日志:logs/www#-access_log
接受命令行参数,作为命令和主机名传递;
使用函数:
列出:list [-a|vhost_name]
创建:create vhost_name
删除:delete [-a|vhost_name]
#!/bin/bash
#function: create param_1 param_2 param_3 ...
create(){
for i ;
do
file="/etc/httpd/conf.d/$i" #suffix ".conf" is missing.
if [[ ! -e $file ]];
then
cat << EOF >>$file
<VirtualHost 172.16.8.100:80>
ServerName www"$i".magedu.com
DocumentRoot /data/vhosts/www"$i"
CustomLog logs/www"$i"-access_log combined
</VirtualHost>
EOF
fi
done
}
一台电脑挂100个虚拟机。
#del param_1 param_2 param_3 ...
del(){
for i ;
do
file="/etc/httpd/conf.d/$i"
if [[ -e $file ]];
then
/bin/rm -i $file
fi
done
}
vmware怎么创建新的虚拟机?
#list param_1 param_2 param_3 ...
list(){
for i ;
do
file="/etc/httpd/conf.d/$i"
if [[ -e $file ]];
then
echo $i
fi
done
}
vh_name=vhost
TEMP=`getopt -o c:d:l:: --long create:,delete:,list:: -n 'report' -- "$@"`
如何实现虚拟主机配置。
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
echo "$@"
while true ; do
case "$1" in
-c|--create)
create_param=$(echo $2 | awk -F, '{for(i=1;i<=NF;++i){split($i,arr,"-");if(arr[2] ~ /[^[:space:]]/){for(j=arr[1];j<=arr[2];++j) printf "%s ",j}else printf "%s ",arr[1]}}')
shift 2
#echo $create_param
;;
-d|--delete)
case "$2" in
all)
delete_param=$(/bin/ls /etc/httpd/conf.d/ | grep -Po "(?<=$vh_name)[^[:space:]]+" | tr '\n' ' ')
;;
*)
delete_param=$(echo $2 | awk -F, '{for(i=1;i<=NF;++i){split($i,arr,"-");if(arr[2] ~ /[^[:space:]]/){for(j=arr[1];j<=arr[2];++j) printf "%s ",j}else printf "%s ",arr[1]}}')
;;
esac
shift 2
#echo $delete_param
;;
-l|--list)
# l has an optional argument. As we are in quoted mode,
# an empty parameter will be generated if its optional
# argument is not found.
case "$2" in
"")
list_param=$(/bin/ls /etc/httpd/conf.d/ | grep -Po "(?<=$vh_name)[^[:space:]]+" | tr '\n' ' ')
shift 2
;;
*)
list_param=$(echo $2 | awk -F, '{for(i=1;i<=NF;++i){split($i,arr,"-");if(arr[2] ~ /[^[:space:]]/){for(j=arr[1];j<=arr[2];++j) printf "%s ",j}else printf "%s ",arr[1]}}')
shift 2
echo $list_param
;;
esac
;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
#echo "Remaining arguments:"
for arg do echo 'extra argument --> '"\`$arg'" ; done
if [[ ! -z $arg ]];then echo "error,exit 1";exit 1;fi
if [[ ! -z $create_param ]];then
create_files=$(echo $create_param | awk -v vh=$vh_name '{for(i=1;i<=NF;++i)printf "%s%s ",vh,$i}')
create $create_files
fi
if [[ ! -z $list_param ]];then
list_files=$(echo $list_param | awk -v vh=$vh_name '{for(i=1;i<=NF;++i)printf "%s%s ",vh,$i}')
list $list_files
fi
if [[ ! -z $delete_param ]];then
delete_files=$(echo $delete_param | awk -v vh=$vh_name '{for(i=1;i<=NF;++i)printf "%s%s ",vh,$i}')
del $delete_files
fi