Tuesday, July 26, 2016

Bailed out by Comware's python interpreter

Something funny happened to an IRF stack the other day. The gear was moved to a remote location and, as part of the move, the cables/transceivers were moved around.

The IRF (10Gb/s) and uplink (1Gb/s) ports switched places.

The new IRF ports (5 and 6) got added to the configuration just fine, leaving me with:
 #  
 irf-port 1/1  
  port group interface Ten-GigabitEthernet1/1/1  
  port group interface Ten-GigabitEthernet1/1/2  
  port group interface Ten-GigabitEthernet1/1/5  
  port group interface Ten-GigabitEthernet1/1/6  
 #  
 irf-port 2/2  
  port group interface Ten-GigabitEthernet2/1/1  
  port group interface Ten-GigabitEthernet2/1/2  
  port group interface Ten-GigabitEthernet2/1/5  
  port group interface Ten-GigabitEthernet2/1/6  
 #  

But IRF would't release the old ports making it impossible to repurpose them as uplinks:
 [switch]irf-port 1/1  
 [switch-irf-port1/1]undo port group interface Ten-GigabitEthernet1/1/1  
 Check failed for reason:  
  Can't support IRF on a port with 1000M speed!  
 [switch]

Can't remove a port from the IRF group because it's ineligible to participate in IRF. Okaaaaay...

Without functioning uplinks, it was impossible to transfer the saved configuration away for an off-box edit.

Python to the rescue!
I split the IRF, used python to edit the stored configuration in-place on each IRF member, then rebooted the switches individually. Here's the edit-by-python:
 <switch>python   
 Python 2.7.3 (default, Apr 10 2014, 16:32:11)   
 [GCC 4.4.1] on linux2  
 Type "help", "copyright", "credits" or "license" for more information.  
 >>>   
 >>> import fileinput, re, sys  
 >>> for line in fileinput.input('flash:/startup.cfg', inplace = True):  
 ...  if not re.search(r' port group interface Ten-GigabitEthernet[12]/1/[12]'  
 ,line):  
 ...    sys.stdout.write(line)  
 ...   
 >>> ^D
 <switch> 

Many thanks to my pal Chris Young for both pointing out that I had a python interpreter at my disposal, and suggesting that I use it to butcher the saved configuration.