apache-vhost:自动完成安装,基本配置

            通过提示输入域名、网站根目录、等信息完成虚拟主机的添加工作

service httpd stop &> /dev/null


conf="/usr/local/apache/conf/httpd.conf"

vhostconf="/usr/local/apache/conf/extra/httpd-vhosts.conf"

bin="/usr/local/apache/bin/apachectl"

rootdir=/www

mkdir -p $rootdir 



grep "vhost" $conf | grep "#" &> /dev/null

if [ $? -eq 0 ]; then

vhost=$(grep "vhost" $conf | sed "s/#//")

sed -i '/vhost/ d' $conf &> /dev/null

echo "$vhost" >> $conf


echo "NameVirtualHost *:80" > $vhostconf


echo "

order allow,deny

allow from all

">> $vhostconf

fi


while true

do

read -p "hostname of FDQN:" fdqn

read -p "directory of website:" sitedir

mkdir -p $rootdir/$sitedir/html

mkdir -p $rootdir/$sitedir/logs


domain=$(echo $fdqn | awk -F. '{print $2"."$3}')

echo "

    ServerAdmin admin@$domain

    DocumentRoot $rootdir/$sitedir/html

    ServerName  $fdqn

    ErrorLog $rootdir/$sitedir/logs/error_log

    CustomLog $rootdir/$sitedir/logs/access_log common

" >> $vhostconf


read -p "continue to do?(yes/no)" yn

if [ $yn = no ];then

$bin start &> /dev/null

exit 0

fi

done

DHCP:自动完成安装,基本配置

    :通过提示输入网段、子网掩码、网关、DNS、IP起止地址等信息完成作用域的添加

#!/bin/bash

conf="/etc/dhcpd.conf"

rpm -q dhcp &> /dev/null

if [ $? -ne 0 ];then

yum -y install dhcp

fi



grep "subnet" $conf &> /dev/null

if [ $? -ne 0 ]; then

echo "

ddns-update-style interim;

ignore client-updates;

default-lease-time 21600;

max-lease-time 43200;

" > $conf 

fi


read -p "please input network:" network

read -p "please input netmask:" netmask

read -p "please input gateway:" gateway

read -p "please input DNS:" dns

read -p "please input start_ip:" start_ip

read -p "please input end_ip:" end_ip


echo "

subnet $network netmask $netmask {

        option routers                  $gateway;

option subnet-mask              $netmask;

        option domain-name-servers      $dns;

        range dynamic-bootp $start_ip $end_ip;

}


" >> $conf 

service dhcpd restart


DNS:完成DNS基本配置

    根据提示输入域名、区域文件名、记录类型、主机头、IP地址等信息完成添加域及记录的工作

conf=/var/named/chroot/etc/named.conf

datadir=/var/named/chroot/var/named



rpm -q bind &> /dev/null

if [ $? -ne 0 ]; then

yum -y install bind bind-chroot caching-nameserver

fi


if [ ! -f $conf ];then

echo "options {

directory \"/var/named\";

};" > $conf

fi


read -p "please input domain_name:" domain

grep $domain $conf &> /dev/null

if [ $? -ne 0 ]; then

echo "zone \"$domain\" in {  

type master;

file \"$domain\";

};" >> $conf

echo "\$ttl 86400

@ in soa ${domain}. root.${domain}. (

2013010101

3h

15m

1w

1d

)"> $datadir/$domain

while true

do

read -p "Type of recond(ns/a/cname/mx/ptr):" type

case $type in

ns)

echo $domain | grep "in-addr.arpa" &> /dev/null 


if [ $? -eq 0 ]; then

read -p "hostname of FDQN:" fdqn

                echo "@ in      ns      $fdqn.">> $datadir/$domain


else

read -p "head of hostname:" head

echo "@ in ns $head">> $datadir/$domain

fi

;;

a)

read -p "head of hostname:" head

read -p "ip address:" ip

echo "$head in a $ip">> $datadir/$domain 


;;

ptr)

read -p "ip address:" ip

read -p "hostname of FQDN:" fqdn

host=$(echo $ip | awk -F. '{print $4}')

echo "$host in ptr $fqdn.">> $datadir/$domain 

;;

mx)

read -p "priority of recond:" priority

read -p "head of hostname:" head

read -p "ip address:" ip

echo "@ in mx $priority $head">>$datadir/$domain

echo "$head in a $ip">> $datadir/$domain 

;;

cname)

read -p "please input aliase:" aliase

read -p "head of hostname:" head 

echo "$aliase in cname $head">> $datadir/$domain 

;;

esac

read -p "continue to do (yes/no)?" yn

if [ $yn = no ]; then

exit 0

fi

done

fi



vsftpd:关闭匿名用户登录,支持本地用户登录

       通过交互输入用户名、部门名实现目录及权限设置

       目录结构如下:

/data/public (公司公共目录,所有员工可读、可写,但不可删除其他人的文件)

/data/部门目录 (部门员工可读,部门管理员可写,其他人不可访问)

/data/部门目录/用户目录


注意:在实例脚本中使用了continue/break/exit,主要目的让大家了解三者之间的区别

conf=/etc/vsftpd/vsftpd.conf

rpm -q vsftpd &> /dev/null

if [ $? -ne 0 ]; then

yum -y install vsftpd

sed -i 's/anonymous_enable=YES/anonymous_enable=NO/' $conf

echo "local_root=/data" >> $conf


mkdir -p /data/public

chmod 1777 /data/public

fi

while true

do

read -p "please input username:" username

read -p "please input bumen:" bumen

read -p "Are you sure?(yes/no)" yn

if [ $yn = no ]; then

continue

fi

if [ ! -d /data/$bumen ]; then

mkdir -p /data/$bumen

groupadd $bumen

useradd -g $bumen -d /data/$bumen/${bumen}-adm ${bumen}-adm

echo "${bumen}-adm:123,qwe." | chpasswd

chage -d 0 ${bumen}-adm

chown ${bumen}-adm:$bumen /data/$bumen

chmod o-rx /data/$bumen

fi

grep "$username" /etc/passwd &> /dev/null

if [ $? -ne 0 ]; then

useradd -g $bumen -d /data/$bumen/$username $username

echo "$username:123456" | chpasswd

                chage -d 0 $username

else

echo "$username is exist."


fi

read -p "continue to yes/no?" yn

if [ $yn = no ]; then

break

fi

done

service vsftpd status &> /dev/null


if [ $? -eq 0 ]; then

exit 0

else

service vsftpd start

fi