2010年9月14日 星期二

server network setting script (only for ubuntu)

<1>初版
#!/bin/bash
[ "$USER" != "root" ] && echo "need root" && exit 1
clear

function MainMenu {
echo "############################################################################"
echo "<1>IP網路設定"
echo "<2>DNS設定"
echo "<3>Default Gateway 設定"
echo "<4>個人燈入檔設定"
echo "<5>重設MAC"
echo "<6/q>離開"
echo "############################################################################"

echo ""
read -p "your choice? " choice
clear

case $choice in
        "1")
                vim /etc/network/interfaces
                ;;
        "2")
                vim /etc/resolv.conf
                ;;
        "3")
                vim /etc/rc.local
                ;;
        "4")
                read -p "要修改的使用者: " user
                if [ -d /home/$user ]; then
                        vim /home/$user/.bashrc
                elif [ $user == "root" ]; then
                        vim /root/.bashrc
                else
                        echo "找不到使用者"
                fi
                ;;
        "5")
                file="/etc/udev/rules.d/70-persistent-net.rules"
                if [ ! -f file ]; then
                        echo "找不到檔案"
                else
                        rm $file
                fi
                ;;
        "6"|"q"|"Q")
                read -p "are you sure (Y/N)? " ans
                [ "$ans" == "Y" ] || [ "$ans" == "y" ] && exit 0
esac
}

while [ 1 ]
do
        clear
        MainMenu
        echo ""
        echo -ne "Return to Main Menu ..."
        read
done
                       

<2>修正版(基本功能完成,個人測試ok)
#!/bin/bash
#Author : Marcus Tsai  
#20100914 初版
#20100916 第二次修正版
#20100918 第三次修正版,基本功能完成
##################################################################################

[ "$USER" != "root" ] && echo "Error:拒絕不符權限的操作" && exit 1
[ "$LANG" != "zh_TW.UTF-8" ] && echo "please use zh_TW.UTF-8" && exit 1
clear
echo ""
echo ""
echo "請注意!!"
echo "執行本程式會修改系統設定檔,在輸入相關參數時,"
echo "請特別小心,以免造成系統的異常或損毀...."
echo ""
echo ""
echo ""
sleep 5
clear

netfile=/etc/network/interfaces
dnsfile=/etc/resolv.conf
ethfile=/etc/udev/rules.d/70-persistent-net.rules

function MainMenu {
echo "#####################[ 網路設定程式主選單 ]####################"
echo ""
echo "<1> 網路相關設定"                                                           
echo "<2> 設定預設閘道"
echo "<3> DNS 設定"
echo "<4> 個人參數檔案設定"
echo "<5> 更換網卡,刪除網路檔案"
echo "<6> 增加路由設定"
echo "<7> 啟動路由器模式"
echo "<8> 設定主機NAT設定"
echo "<9> 設定完成,將系統重新啟動"
echo " 離開"
echo ""
echo "####################################################"

echo ""
read -p "請問要執行哪一項設定(1~9)? " choice
clear

case $choice in
        "1")
                [ -f /tmp/int.txt ] && rm /tmp/int.txt
        cat $netfile > /tmp/int.txt
                read -p "請輸入要修改的網路卡(ex.eth0): " eth
       
        if [ ! -z "$eth" ]; then
            read -p "Please input IP info: " ip
                    read -p "Please input Netmask info: " mask
            f=/tmp/int.txt       
            i=$(grep -A 3 "$eth" "$f" |grep addr)
            j=$(grep -A 3 "$eth" "$f" |grep mask)
            k=$(grep -A 3 "$eth" "$f" |grep gate )
            if [ -z "$i" ]; then
                sed -i -e  /$eth/d $f
                echo -e "\nauto $eth \niface $eth inet static \n  address $ip\n  netmask $mask" >> $f
            else
                sed -i -e /$eth/,/netmask/d $f
                [ ! -z "$k" ] && sed -i -e /gateway/d $f
                echo -e "\nauto $eth \niface $eth inet static \n  address $ip\n  netmask $mask" >> $f
            fi
            [ -f $f ] && cat $f > $netfile
        else
            echo "沒有這個裝置"
            return
        fi
                ;;
    "2")
        read -p "請輸入要增加預設路由的網路卡(ex.eth0): " eth
        if [ ! -z "$eth" ]; then
            cat $netfile > /tmp/gw.txt
            f=/tmp/gw.txt
            dhcp=$(grep "$eth" "$f" |grep dhcp)
            i=$(grep -A 3 "$eth" "$f" |grep addr)
                        j=$(grep -A 3 "$eth" "$f" |grep mask)
            k=$(grep -A 3 "$eth" "$f" |grep gate )
            if [ ! -z "$dhcp" ]||[ -z "$i" ]; then
                echo "該裝置使用DHCP或沒有設定ip"
                return
            else
                read -p "請輸入預設閘道IP: " gwip
                sed -i -e "/$eth/,/netmask/d" $f
                sed -i -e "/gateway/d" $f
                echo -e "\nauto $eth \niface $eth inet static\n  address ${i##* }\n  netmask ${j##* }" >> $f
                echo "  gateway $gwip" >> $f
                cat $f > $netfile
            fi
        else
            echo "沒有這個裝置"
        fi
        ;;
    "3")
        read -p "請輸入DNS IP: " dns
        i=$(cat $dnsfile)
        if [ ! -z "$dns" ]; then
            if [ ! -z "$i" ]; then
                j=$(grep nameserver $dnsfile )
                sed -i -e "s/${j##* }/$dns/g" $dnsfile
            else
                echo "nameserver $dns" > $dnsfile
            fi
        else
            echo "取消設定DNS"
        fi
        ;;
    "4")
        read -p "要修改的使用者: " user
                if [ -d /home/$user ]; then
                        vim /home/$user/.bashrc
                elif [ $user == "root" ]; then
                        vim /root/.bashrc
                else
                        echo "找不到使用者"
                fi
                ;;
    "5")
        echo -ne "確定要刪除$ethfile嗎?"
        read ans
        if [ "$ans" == "y" -o "$ans" == "Y" ]; then
            if [ -f $ethfile ]; then
                            rm $ethfile
            else
                echo "找不到檔案!!"
            fi
                fi

        ;;
    "6")
        read -p "要加入路由表的網段(ex:192.168.1.0): " dest
        read -p "該網段的netmask: " mask
        read -p "透過哪一個gateway ip: " gwip
        read -p "透過哪一張網路卡: " nic
        sed -i -e /exit/d /etc/rc.local
        echo -e "route add -net $dest netmask $mask gw $gwip dev $nic" >> /etc/rc.local
        echo "exit 0" >> /etc/rc.local
        ;;
    "7")
        read -p "確定要啟動路由器服務(y/n)?" ans
        if [ "$ans" == "y" ] || [ "$ans" == "Y"  ]; then
            sed -i -e s/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/g /etc/sysctl.conf
            sysctl -p &> /dev/null
            echo "設定完成"
        else
            echo "不明選項"
        fi
        ;;
    "8")
        read -p "透過本機哪張網卡上網(ex.eth0): " eth
        if [ ! -z "$eth" ]; then
            check=$(grep $eth $netfile)
            if [ -z "$check" ]; then
                echo "裝置不存在"
                return
            else
                sed -i -e /exit/d /etc/rc.local
                echo "iptables -t nat -A POSTROUTING -o $eth -j MASQUERADE" >> /etc/rc.local
                echo "exit 0" >> /etc/rc.local
                sed -i -e s/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/g /etc/sysctl.conf
                            sysctl -p &> /dev/null
                            echo "設定完成"
            fi
        else
            echo "不明的裝置"
        fi
        ;;
    "9")
        read -p "你確定要重開機?(y/n)" ans
        if [ $ans == "y" ] || [ $ans == "Y" ]; then
                        reboot
                elif [ $ans == "n" ] || [ $ans == "N" ]; then
                        echo ""
                else
                        echo "不明選項"
                fi
        ;;       
        "q"|"Q")

        echo ""
                read -p  " 確定要離開程式?(y/n) " ans
                if [ $ans == "y" ] || [ $ans == "Y" ]; then
            clear
            echo ""
            exit 0
        elif [ $ans == "n" ] || [ $ans == "N" ]; then
            echo ""

        else
            echo "不明選項"
        fi
                ;;
esac
}

while [ 1 ]
do
        clear
        MainMenu

        echo ""
        echo -ne "Return to Main Menu..."
        read
done

沒有留言:

張貼留言