Skip to content

Instantly share code, notes, and snippets.

@cmouse
Created May 1, 2015 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cmouse/ca98715131d00a4cd436 to your computer and use it in GitHub Desktop.
Save cmouse/ca98715131d00a4cd436 to your computer and use it in GitHub Desktop.
Exabgp peering
#!/usr/bin/ruby
require 'socket'
# our IP here
@target = 'x.212'
# bind these to lo using ip addr add x.x.x.x/32 dev lo
@routes = %w[ x.245/32 x.8/32 x.6/32 x.22/32 x.7/32 ]
@sockfile = '/var/run/pdns_recursor.controlsocket'
@state = :announce
STDOUT.sync = true
def sockname
(('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a).sample(8).join('') + "." + Process.pid.to_s
end
def test
ret = false
lpath = File.join File.dirname(@sockfile), sockname
begin
s = Socket.new(:UNIX, :DGRAM, 0)
s.bind Socket.sockaddr_un lpath
File.chmod 0666, lpath
s.connect Socket.sockaddr_un @sockfile
s.send 'ping', 0
if IO.select([s], nil, nil, 1).nil? == false
s.recv(10)
ret = true
end
s.close
rescue
end
File.unlink lpath
return ret
end
def announce
return unless @state == :announce
@routes.each do |r|
print "announce route #{r} next-hop #{@target} med 50 local-preference 50 community [ 16086:16086 ]\n"
end
print "flush routes\n"
@state = :withdraw
end
def withdraw
return unless @state == :withdraw
@routes.each do |r|
print "withdraw route #{r} next-hop #{@target} med 100 local-preference 50 community [ 16086:16086 ]\n"
end
print "flush routes\n"
@state = :announce
end
def main
# print "version exabgp 3.3.0\n"
while true
if test
announce
else
withdraw
end
sleep 1
end
end
main
#!/usr/bin/ruby
require 'socket'
# our IP here
@target = '2001:14b8:1000:1::3:100'
# bind these to lo using ip addr add x:x::x/128 dev lo
@routes = %w[ x:1000::1/128 ]
@sockfile = '/var/run/pdns_recursor.controlsocket'
@state = :announce
STDOUT.sync = true
def sockname
(('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a).sample(8).join('') + "." + Process.pid.to_s
end
def test
ret = false
lpath = File.join File.dirname(@sockfile), sockname
begin
s = Socket.new(:UNIX, :DGRAM, 0)
s.bind Socket.sockaddr_un lpath
File.chmod 0666, lpath
s.connect Socket.sockaddr_un @sockfile
s.send 'ping', 0
if IO.select([s], nil, nil, 1).nil? == false
s.recv(10)
ret = true
end
s.close
rescue
end
File.unlink lpath
return ret
end
def announce
return unless @state == :announce
@routes.each do |r|
print "announce route #{r} next-hop #{@target} med 100 local-preference 50 community [ 16086:16086 ]\n"
end
print "flush routes\n"
@state = :withdraw
end
def withdraw
return unless @state == :withdraw
@routes.each do |r|
print "withdraw route #{r} next-hop #{@target} med 100 local-preference 50 community [ 16086:16086 ]\n"
end
print "flush routes\n"
@state = :announce
end
def main
# print "version exabgp 3.3.0\n"
while true
if test
announce
else
withdraw
end
sleep 1
end
end
main
neighbor x.x.x.209 {
# our IP address
router-id x.x.x.212;
local-address x.x.x.212;
local-as x;
peer-as x;
hold-time 21;
group-updates;
capability {
asn4;
}
process watchdog-1 {
run /etc/exabgp/processes/check-v4.rb;
}
}
neighbor x::3:1 {
# SAME as above
router-id x.212;
# our IP
local-address x::3:100;
# same as above
local-as x;
peer-as x;
hold-time 21;
group-updates;
capability {
asn4;
}
process watchdog-2 {
run /etc/exabgp/processes/check-v6.rb;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment