rubyでCiscoルータの設定を自動化してみた

network
Published: 2012-07-01

 「Peeringする度にコマンドを手で打つのもあれだなー。やる事は同じだし」ということで、rubyで自動化しました。実行すると対話形式で必要な情報の入力を求められ、答えると自動的にCiscoルータに我が家のテンプレconfigが投入されます。本番テストはまだですが・・・

 BGPの経路監視とLookingGrassについで3回目のrubyでCiscoルータシリーズですが、同じ事の繰り返しな作業についてはNet::Telnetを使えば何でも出来る気がしてきました。

 また、当たり前の事ではありますが、自動化するって事はルールを決める事なんだなと実感しました。これまではPeering用のパラメータを何となく連番で決めてきましたが、今後は下記のルールで統一しようと思います。

  • Peering用トンネル番号:AS番号
  • トンネル用IPアドレス:2610:D0:3211:FFFF::相手のAS番号5ケタ目:相手のAS番号1~4桁名:1/126

使い方

# ruby peering.rb 
input the neighbor's name : test
input the neighbor's AS number : 64585
input the neighbor's IPv4 address : 192.168.1.1

素人コード

#!/usr/local/bin/ruby

require 'net/telnet'

#必要情報を収集する
print "input the neighbor's name : "
name = gets.to_s
name.chomp!

if /^\n$/ =~ name then
  print "name is mpty!!\n"
  exit!
end  

print "input the neighbor's AS number : "
as = gets.to_s
as.chomp!

if /^\n$/ =~ as then
  print "AS number is empty!!\n"
  exit!
end

print "input the neighbor's IPv4 address : "
ip = gets.to_s
ip.chomp!

if /^\n$/ =~ ip then
  print "IPv4 address is empty!!\n"
  exit!
elsif /[0-255]\.[0-255]\.[0-255]\.[0-255]/ !~ ip then
  print "your input is not IPv4 address\n"
  exit!
end

#AS番号をIPv6アドレス形式に変換

ascount = as.split(//).size

if ascount > 4 then
  asvar = as.split('')
  asaddress = "#{asvar[0]}:#{asvar[1]}#{asvar[2]}#{asvar[3]}#{asvar[4]}"   
else  
  asaddress = as
end
  
#config投入

@router = "router'S IPaddress"
@user = "username"
@password = "password"
@enpassword = "enable password"
@prompt = "hostname"

tn  = Net::Telnet::new("Host" => @router, "Timeout" => 5)

tn.cmd("String" => "#{@user}","Match"=>/^Password/) 
tn.cmd("String" => "#{@password}","Match"=>/^#{@prompt}/)
tn.cmd("String" => "en","Match"=>/^Password/)
tn.cmd("String" => "#{@enpassword}","Match"=>/^#{@prompt}/) 

tn.cmd("String" => "conf t","Match"=>/^#{@prompt}/)
tn.cmd("String" => "interface tunnel #{as}","Match"=>/^#{@prompt}/)
tn.cmd("String" => "description #{name}","Match"=>/^#{@prompt}/)
tn.cmd("String" => "no ip address","Match"=>/^#{@prompt}/)
tn.cmd("String" => "ipv6 address 2610:D0:3211:FFFF::#{asaddress}:1/126","Match"=>/^#{@prompt}/)
tn.cmd("String" => "ipv6 enable","Match"=>/^#{@prompt}/)
tn.cmd("String" => "ipv6 mtu 1280","Match"=>/^#{@prompt}/)
tn.cmd("String" => "tunnel source 36.2.107.75","Match"=>/^#{@prompt}/)
tn.cmd("String" => "tunnel mode ipv6ip","Match"=>/^#{@prompt}/)
tn.cmd("String" => "tunnel destination #{ip}","Match"=>/^#{@prompt}/)
tn.cmd("String" => "exit","Match"=>/^#{@prompt}/)
tn.cmd("String" => "router bgp 64585","Match"=>/^#{@prompt}/)
tn.cmd("String" => "neighbor 2610:D0:3211:FFFF::#{asaddress}:2 remote-as #{as}","Match"=>/^#{@prompt}/)
tn.cmd("String" => "neighbor 2610:D0:3211:FFFF::#{asaddress}:2 description #{name}","Match"=>/^#{@prompt}/)
tn.cmd("String" => "neighbor 2610:D0:3211:FFFF::#{asaddress}:2 timers 10 30","Match"=>/^#{@prompt}/)
tn.cmd("String" => "address-family ipv6","Match"=>/^#{@prompt}/)
tn.cmd("String" => "neighbor 2610:D0:3211:FFFF::#{asaddress}:2 activate","Match"=>/^#{@prompt}/)
tn.cmd("String" => "neighbor 2610:D0:3211:FFFF::#{asaddress}:2 next-hop-self","Match"=>/^#{@prompt}/)
tn.cmd("String" => "neighbor 2610:D0:3211:FFFF::#{asaddress}:2 soft-reconfiguration inbound","Match"=>/^#{@prompt}/)
tn.cmd("String" => "neighbor 2610:D0:3211:FFFF::#{asaddress}:2 prefix-list this-network out","Match"=>/^#{@prompt}/)
tn.cmd("String" => "end","Match"=>/^#{@prompt}/)
tn.puts("exit") 
tn.close