-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbgp.inc.sh
95 lines (82 loc) · 2.27 KB
/
bgp.inc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash
#**
#
# Management of BGP peering in the AS (add/delete network routes via quagga/vtysh)
# @date 2012
# @author pcheynier
#
#**
# --------- CONFIGURATION
# VTYSH Path
VTYSH="/usr/bin/vtysh"
# Quagga Configuration Path
QUAGGA_CONF="/etc/quagga/bgpd.conf"
# Default local AS number
BGP_LOCAL_AS="64699"
# --------- END OF CONF
# Send a message to logger and logged in users
bgp_message () {
/usr/bin/logger -s -t 'bgp_mgt' "$1"
}
# Set an AS number diferent than the default one
# @param AS number
#
set_AS () {
BGP_LOCAL_AS="$1"
}
# Check if a route/an address looks like IPv6 one
# @param IP route/address
#
is_v6 () {
# Match colons, simpler than using IPv6 regex as we do not need validation...
if [[ -n `echo $1 | egrep ":"` ]]; then
return 0
else
return 1
fi
}
# Check if a route is announced
# @param Network route
#
check_route () {
# Get the right network type to use in "sh bgp .." command
if is_v6 $1; then
net_type="ipv6"
else
net_type="ipv4"
fi
# Return the state of the route
if [[ -z `$VTYSH -c "sh bgp $net_type unicast $1" | grep "Network not in table"` ]]; then
return 0
else
return 1
fi
}
# Add/Restore a route
# @param Network route
#
add_route () {
bgp_message "Adding route to $1"
# Be sure the route exists in the initial configuration to avoid some big fails if your peer is too permissive (..)
NETWORK_ROUTEMAP_CMD=`cat $QUAGGA_CONF | grep "$1" | head -1`
if [[ -n $NETWORK_ROUTEMAP_CMD ]]; then
if is_v6 $1; then
$VTYSH -c "conf t" -c "router bgp $BGP_LOCAL_AS" -c "address-family ipv6" -c "$NETWORK_ROUTEMAP_CMD"
else
$VTYSH -c "conf t" -c "router bgp $BGP_LOCAL_AS" -c "$NETWORK_ROUTEMAP_CMD"
fi
sleep 1
fi
}
# Delete a route
# @param Network route
#
remove_route () {
bgp_message "Deleting route to $1"
if is_v6 $1; then
$VTYSH -c "conf t" -c "router bgp $BGP_LOCAL_AS" -c "address-family ipv6" -c "no network $1"
else
$VTYSH -c "conf t" -c "router bgp $BGP_LOCAL_AS" -c "no network $1"
fi
sleep 1
}