自动化配置ospf脚本

前面有提到过自己写的一个自动化在服务器上配置quagga跑ospf的脚本。简单说一下适用的环境
1. 服务器同时有千兆和万兆的接入,万兆接入交换机上起OSPF,互联地址每个网卡使用/30的一段地址。
2. ospf的验证需要和交换机相一致。
3. 使用dummy0宣告单独的万兆服务地址
使用的方式比较简单


sh config_ospf.sh eth4 eth5 192.168.1.1

当交换机上配置好ospf,就能自动抓包分析配置,在本地dummy0宣告192.168.1.1的地址了。


#!/bin/sh
#****************************************************************#
# ScriptName: config_ospf.sh
# Author: pm@gnuers.org
# Create Date: 2013-10-30 11:29
# Modify Author: pm@gnuers.org
# Modify Date: 2014-05-16 14:18
# Function:
#***************************************************************#
. /etc/profile
#DEVS=(eth4 eth5)
declare -A IPS
declare -A AREAS
declare -A MASKS
declare -A NETWORKS
declare -A HELLOTIMES
declare -A DEADTIMES
declare -A GATEWAYS
OSPF_PASS=OSPF—PASS-WORD
#if no bond0, set GIGADEV=()
GIGADEV=( bond0 )
INNET_GATEWAY="172.15.1.1"
NET=30
TMP=/tmp/autoconfig.$$

get_link_config(){
    killall -9 zebra ospfd
    for dev in ${DEVS[@]}
    do
        #tcpdump
        ifconfig $dev up
        echo "get $dev info"
        tcpdump  -i $dev  proto ospf -nn -v -c1 2>/dev/null >$TMP
        RIP=$(grep OSPFv2 $TMP |awk '{print $1}')
        if [ $RIP ];then
            LIP=$( echo $RIP|sed "s/.*\.//")
            LIP=$((LIP + 1))
            LIP=$(echo $RIP|sed -r "s/\.[0-9]*$/.$LIP/")
            AREAID=$(grep Area $TMP |awk '{print $4}'|tr -d ",")
            HELLO=$(grep "Hello Timer" $TMP|awk '{print $3}'|cut -ds -f1)
            DEAD=$(grep "Hello Timer" $TMP|awk '{print $6}'|cut -ds -f1)
            #MASK=$( grep Mask $TMP |awk '{print $8}' |tr -d ",")
            MASK="255.255.255.252"
            NETWORK=$(ipcalc -n $LIP/30|cut -d"=" -f2)
            IPS[$dev]=$LIP
            AREAS[$dev]=$AREAID
            MASKS[$dev]=$MASK
            NETWORKS[$dev]=$NETWORK
            DEADTIMES[$dev]=$DEAD
            HELLOTIMES[$dev]=$HELLO
            GATEWAYS[$dev]=$RIP
        fi
        areanum=$(echo ${AREAID[@]}|tr " " "\n"|sort -u|wc -l)
        if [ $areanum -ne 1 ];then
            echo "more than one area id,exit"
            exit
        else
            AREA=$(echo ${AREAID[@]}|tr " " "\n"|sort -u)
        fi
        rm -f $TMP
    done

}
auto_config_dev()
{
    for dev in ${!IPS[@]}
    do
        dev_conf="/etc/sysconfig/network-scripts/ifcfg-$dev"
        ifdown $dev
        echo "config $dev  ${NETWORKS[$dev]}/30 ${IPS[$dev]} ${MASKS[$dev]}"
        cat >$dev_conf<<EOF
DEVICE=$dev
BOOTPROTO=none
TYPE="Ethernet"
ONBOOT=yes
IPADDR=${IPS[$dev]}
NETMASK=${MASKS[$dev]}
EOF
    ifup $dev
    done

}
config_zebra(){
    conf=/etc/zebra.conf
    HOST_NAME=$(hostname)
    cat >$conf <<EOF
hostname $HOST_NAME
password  xxxxxxxxxxxxx
enable password  xxxxxxxxxxx
log syslog
log facility local4
log file /var/log/zebra.log

!
!
interface lo
!
EOF
for dev in  ${!IPS[@]}
do
    cat >>$conf <<EOF
interface $dev
    link-detect
    !
EOF
done
}
config_ospfd(){
    conf=/etc/ospfd.conf
    cat >$conf <<EOF
!
log syslog
log facility local5
log file /var/log/ospf.log
service password-encryption
password 8  xxxxxxxxxxxxxx
enable password 8 xxxxxxxxxxxxx

!
!
interface lo
!
EOF
for dev in  ${!IPS[@]}
do
    cat >>$conf <<EOF
interface $dev
    ip ospf authentication message-digest
    ip ospf message-digest-key 1 md5 $OSPF_PASS
    ip ospf network point-to-point
    ip ospf hello-interval ${HELLOTIMES[$dev]}
    ip ospf dead-interval ${DEADTIMES[$dev]}
EOF
done


    ROUTE_ID=$(hostname  -i)
    cat >>$conf <<EOF
router ospf
    ospf router-id $DUMMY_IP
    log-adjacency-changes
    ! Important: ensure reference bandwidth is consistent across all routers
    !auto-cost reference-bandwidth 100000
EOF

    for dev in  ${!IPS[@]}
    do
        echo "    network ${NETWORKS[$dev]}/30 area $AREA " >>$conf
    done
     cat >>$conf <<EOF
      network $DUMMY_IP/24 area $AREA
      area $AREA  nssa translate-candidate no-summary

!
line vty
!
EOF
}
config_route_rule(){
metric=100
rt=/etc/iproute2/rt_tables
for dev in  ${!IPS[@]}
do
        grep $dev $rt &>/dev/null
        if [ $? -ne 0 ];then
                echo "$metric route_$dev" >>$rt
                metric=$((metric+1))
        fi
        echo "table route_$dev default via ${GATEWAYS[$dev]} dev $dev" > /etc/sysconfig/network-scripts/route-$dev
        echo "from ${IPS[$dev]} table  route_$dev" > /etc/sysconfig/network-scripts/rule-$dev
done

}
config_giga_route_rule(){
metric=5
rt=/etc/iproute2/rt_tables
name=innet
for dev in  ${GIGADEV[@]}
do
        echo "config $dev route rule"
        grep $dev $rt &>/dev/null
        if [ $? -ne 0 ];then
                echo "$metric route_$dev" >>$rt
                metric=$((metric+1))
        fi
        innet_ip=$(ip addr show dev bond0|grep inet|awk '{print $2}'|cut -d/ -f1)
        echo "table route_$dev default via $INNET_GATEWAY dev $dev" > /etc/sysconfig/network-scripts/route-$dev
        echo "from $innet_ip table  route_$dev" > /etc/sysconfig/network-scripts/rule-$dev

done

}
config_dummy_dev(){
    dummy_conf="/etc/sysconfig/network-scripts/ifcfg-dummy0"
    echo "config dummy0 ip: $DUMMY_IP/32"
    cat >$dummy_conf <<EOF
IPADDR=$DUMMY_IP
NETMASK=255.255.255.255
DEVICE="dummy0"
BOOTPROTO="static"
ONBOOT="yes"
TYPE="ethernet"
EOF
 ifup dummy0
}
restart_ospf(){
    echo "restart ospf"
    killall -9 ospfd zebra &>/dev/null
    /sbin/zebra -u root -d -f /etc/zebra.conf
    /sbin/ospfd -u root -d -f /etc/ospfd.conf
}
check_quagga(){
    rpm -q quagga &>/dev/null
    if [ $? -ne 0 ];then
        yum install  quagga  -y

    else
        echo "quagga alreay been installed"
    fi
    groupadd  quagga &>/dev/null

}
add_rpfilter(){
cat >>/etc/sysctl.conf<<EOF
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.bond0.rp_filter = 0
net.ipv4.conf.dummy0.rp_filter = 0
EOF

for dev in  ${!IPS[@]}
do
cat >>/etc/sysctl.conf<<EOF
net.ipv4.conf.$dev.rp_filter = 0
EOF
done
sysctl -p
}
add_rclocal(){
    rc=/etc/rc.local
    grep zebra $rc &>/dev/null
    if [ $? -ne 0 ];then
        echo "/sbin/zebra -u root -d -f  /etc/zebra.conf" >> $rc
    fi
    grep ospfd $rc &>/dev/null
    if [ $? -ne 0 ];then
        echo "/sbin/ospfd -u root -d -f  /etc/ospfd.conf" >> $rc
    fi

}
unbootbond(){
    sed -i 's/ONBOOT=yes/ONBOOT=no/' /etc/sysconfig/network-scripts/ifcfg-bond0
}
usage(){
    echo -e "help:\n\t./config_ospf.sh  \e[1;32meth4 eth5 dummy0_ip\e[m"
    exit 1

}
if [ $# -ne 3 ];then

    usage

else
    DEVS=( $1 $2 )
    DUMMY_IP=$3
    echo "try to config ${DEVS[@]} with $DUMMY_IP"
fi
check_quagga
get_link_config
#config_route_rule
config_giga_route_rule
auto_config_dev
config_zebra
config_ospfd
add_rpfilter
restart_ospf
config_dummy_dev
add_rclocal
unbootbond
此条目发表在OSPF分类目录。将固定链接加入收藏夹。

发表评论