VLAN config generation with Ruby for Cisco Catalyst 3750

VLAN configuration is very different between HP and Cisco switches. HP is VLAN centred and Cisco is based on physical port. While managing Cisco switches, using a script to generate the config may ease the task. This post proposes a simple script to generate the config.

On HP switches, it’s based on VLAN, defining which port(s) it belongs to. However in Cisco switch, it’s based on the physical port – and using a script (e.g., Ruby) to generate the config for Cisco switch make the work much easier.

The Ruby script

#!/usr/bin/env ruby

port_to_vlan = Hash.new
(1..4).each{|t1|
  port_to_vlan[t1] = 1
}
(5..8).each{|t1|
  port_to_vlan[t1] = 2
}
(47..48).each{|t1|
  port_to_vlan[t1] = (1..4)
}

puts "enable"
puts "terminal length 0"
puts "config terminal"
puts "no logging console"

port_to_vlan.each_pair{|key, value|
  puts "interface GigabitEthernet1/0/#{key}"

  puts "no switchport trunk encapsulation dot1q"
  puts "no switchport mode trunk"
  puts "no switchport trunk allowed vlan"
  puts "no switchport mode access"
  puts "no switchport access vlan"

  if value.class == Range
    puts "switchport trunk encapsulation dot1q"
    puts "switchport mode trunk"
    puts "switchport trunk allowed vlan #{value.begin}-#{value.end}"
  else
    puts "switchport mode access"
    puts "switchport access vlan #{value}"
  end
  puts "exit"
}

puts "show running-config"

Will generate the following configuration

enable
terminal length 0
config terminal
no logging console
interface GigabitEthernet1/0/1
no switchport trunk encapsulation dot1q
no switchport mode trunk
no switchport trunk allowed vlan
no switchport mode access
no switchport access vlan
switchport mode access
switchport access vlan 1
exit
interface GigabitEthernet1/0/2
no switchport trunk encapsulation dot1q
no switchport mode trunk
no switchport trunk allowed vlan
no switchport mode access
no switchport access vlan
switchport mode access
switchport access vlan 1
exit
interface GigabitEthernet1/0/3
no switchport trunk encapsulation dot1q
no switchport mode trunk
no switchport trunk allowed vlan
no switchport mode access
no switchport access vlan
switchport mode access
switchport access vlan 1
exit
interface GigabitEthernet1/0/4
no switchport trunk encapsulation dot1q
no switchport mode trunk
no switchport trunk allowed vlan
no switchport mode access
no switchport access vlan
switchport mode access
switchport access vlan 1
exit
interface GigabitEthernet1/0/5
no switchport trunk encapsulation dot1q
no switchport mode trunk
no switchport trunk allowed vlan
no switchport mode access
no switchport access vlan
switchport mode access
switchport access vlan 2
exit
interface GigabitEthernet1/0/6
no switchport trunk encapsulation dot1q
no switchport mode trunk
no switchport trunk allowed vlan
no switchport mode access
no switchport access vlan
switchport mode access
switchport access vlan 2
exit
interface GigabitEthernet1/0/7
no switchport trunk encapsulation dot1q
no switchport mode trunk
no switchport trunk allowed vlan
no switchport mode access
no switchport access vlan
switchport mode access
switchport access vlan 2
exit
interface GigabitEthernet1/0/8
no switchport trunk encapsulation dot1q
no switchport mode trunk
no switchport trunk allowed vlan
no switchport mode access
no switchport access vlan
switchport mode access
switchport access vlan 2
exit
interface GigabitEthernet1/0/47
no switchport trunk encapsulation dot1q
no switchport mode trunk
no switchport trunk allowed vlan
no switchport mode access
no switchport access vlan
switchport trunk encapsulation dot1q
switchport mode trunk
switchport trunk allowed vlan 1-4
exit
interface GigabitEthernet1/0/48
no switchport trunk encapsulation dot1q
no switchport mode trunk
no switchport trunk allowed vlan
no switchport mode access
no switchport access vlan
switchport trunk encapsulation dot1q
switchport mode trunk
switchport trunk allowed vlan 1-4
exit
show running-config

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *