Monday, November 9, 2009

BGPGEN: a simple TCL script to generate BGP prefixes

hi all,
during my courses at Europa Networking (BG, Italy) I've always heard Rocco Tessicini talking about a script to generate BGP prefixes on Cisco routers.
Suddently I haven't found one ready to download, so today I've decided to write my own (the simpler, the better)

Here the resulting script:


##################################################################################
## Tclsh BGPGEN SCRIPT v0.3 Beta: Add random BGP prefixes to a process
## Use with care on CISCO routers
## By Marco Rizzi ( http://rizzitech.blogspot.com ) marco.rizzi.com[A_T]gmail.com
## Date Nov 09, 2009
## licensed under a Creative Commons Attribution 3.0 United States License
## ( http://creativecommons.org/licenses/by/3.0/us/ ) ;-)
##################################################################################
### USAGE: BGPGEN (number_of_prefixes_to_gen) (bgp_as_number)

## BE CAREFUL! too much prefixes will consume a lot of router resource!
## DON'T USE ON PRODUCTION SYSTEMS, IT'S ONLY FOR LAB TESTING
## No warranty, provided "AS IS"

## Main procedure
proc BGPGEN {n_prefixes bgp_as} {
## 1) adds a redistribute static command under your bgp process
ios_config "router bgp $bgp_as" "redistribute static"

## 2) creates random static routes to null0 interface from /16 to /24
for {set i 0} {$i <= $n_prefixes} {incr i} {
Gen_rnd_Static
}
}


####################################################################

proc Gen_rnd_Static {} {
## Generate random static routes
## to null0 with variable subnet mask betw 16 and 24 bits

## Network: A.B.C.0 Subnet Mask: 255.255.CM.0
set bits [expr {int(rand()*8)}]
set CM 0
if {$bits == 0} { set CM 0 ; set C 0 }
if {$bits == 1} { set CM 128 ; set C [expr {int(rand()*1)*128}]}
if {$bits == 2} { set CM 192 ; set C [expr {int(rand()*3)*64}]}
if {$bits == 3} { set CM 224 ; set C [expr {int(rand()*7)*32}]}
if {$bits == 4} { set CM 240 ; set C [expr {int(rand()*15)*16}]}
if {$bits == 5} { set CM 248 ; set C [expr {int(rand()*31)*8}]}
if {$bits == 6} { set CM 252 ; set C [expr {int(rand()*63)*4}]}
if {$bits == 7} { set CM 254 ; set C [expr {int(rand()*127)*2}]}
if {$bits == 8} { set CM 255 ; set C [expr {int(rand()*255)}]}

## Create the random network: A.B.C.0
set A [expr {int(rand()*223)}]
## some not bullet-proof control to avoid
## "strange" or private addresses (can be improved ;-) )
if {$A <= 10} { set A [expr {$A + int(rand()*200)}]}
if {$A == 127} { set A [expr {int(rand()*223)}]}
if {$A == 172} { set A [expr {int(rand()*223)}]}
if {$A == 192} { set A [expr {int(rand()*223)}]}

set B [expr {int(rand()*254)}]

## configure the final static
ios_config "ip route $A.$B.$C.0 255.255.$CM.0 null0"

}

################################# END OF SCRIPT ###################################
##
##
### USAGE: BGPGEN (number_of_prefixes_to_gen) (bgp_as_number)
#### enjoy ;-)


Obviously I'm not a good programmer, so it can be improved.

to execute it, simply type tclsh and paste the code, look if there are some errors due to the fast paste, in this case, copy and paste smaller pieces...

then type eg.:
R3(tcl)#BGPGEN 10000 64500

and wait until BGPGEN execution terminates.

on the bgp neighbor you can see the prefixes arriving....
eg:

R2#sh ip bgp summary
BGP router identifier 23.23.23.2, local AS number 65000
BGP table version is 146398, main routing table version 146398
34489 network entries using 4552548 bytes of memory
34489 path entries using 1793428 bytes of memory
2/1 BGP path/bestpath attribute entries using 296 bytes of memory
1 BGP AS-PATH entries using 24 bytes of memory
0 BGP route-map cache entries using 0 bytes of memory
0 BGP filter-list cache entries using 0 bytes of memory
BGP using 6346296 total bytes of memory
BGP activity 90156/55667 prefixes, 90707/56218 paths, scan interval 60 secs

Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd
12.12.12.1 4 65000 3238 794 0 0 0 00:02:46 Active
23.23.23.3 4 64500 855 71 146237 0 0 00:01:49 34489
R2#


as well as you can see errors and experience crashes. ;-)

Any comment and/or feature/improvement is always wellcome!

have phun with your routing tables ;-)
Marco

4 comments:

Nicolas said...

Have you heard about bgpsimple (http://code.google.com/p/bgpsimple/)?. It might give you some ideas if you either want to use an external resource to inject the routes or just improve the coding.

Marco Rizzi said...

Wow, thanks a lot, I'll give it a look. In fact with the actual code I'm experiencing problems over the 50k injected routes ;-)

Marco

Shivlu Jain said...

really thanks for posting such a great code.

Marco Rizzi said...

Thanks Shivlu,
I'll try to improve it, looking for a way to optimize the memory allocation.

Keep in touch

Marco