Commit e1873388 authored by Mark Schouten's avatar Mark Schouten
Browse files

Support gaps in interface index numbers

parent bba2ec13
Pipeline #168 passed with stage
in 7 seconds
......@@ -20,14 +20,14 @@ except:
if cmd not in ['-s', '-g', '-n']:
usage()
oidre = re.compile('^([0-9.]+\.)+([0-9]+)$')
oidre = re.compile('^(\.1\.3\.6\.1\.2\.1\.31\.1\.1\.1\.18(\.?))([0-9]+)?$')
oidmatch = oidre.match(oid)
if not oidmatch:
usage()
oidbase = oidmatch.group(1)
oididx = oidmatch.group(2)
oidbase = '.1.3.6.1.2.1.31.1.1.1.18'
oididx = oidmatch.group(3) or 0
if cmd == '-s':
try:
......@@ -37,29 +37,45 @@ if cmd == '-s':
usage()
ip = IPRoute()
links = ip.get_links()
links = {}
linklist = []
for l in ip.get_links():
descr = l.get_attr('IFLA_IFALIAS') or l.get_attr('IFLA_IFNAME')
links[l['index']] = descr
for idx in sorted(links.keys()):
linklist.append({'index': idx, 'descr': links[idx] })
def get_next(idx):
if idx == 0:
return linklist[idx]
try:
for lidx, l in enumerate(linklist):
if l['index'] == int(idx):
return linklist[lidx+1]
except IndexError:
pass
raise IndexError
def snmp_print(iface):
if iface.get_attr('IFLA_IFALIAS'):
print(''.join([str(oidbase), str(iface['index'])]))
print('string')
print(iface.get_attr('IFLA_IFALIAS'))
print('.'.join([str(oidbase), str(iface['index'])]))
print('string')
print(iface['descr'])
if cmd == '-g':
for l in links:
for l in linklist:
if int(l['index']) == int(oididx):
snmp_print(l)
elif cmd == '-n':
lidx = 0
for l in links:
if int(l['index']) == int(oididx):
try:
nextidx = lidx+1
nlink = links[nextidx]
snmp_print(nlink)
except KeyError:
pass
lidx+=1
try:
i = get_next(oididx)
snmp_print(i)
except IndexError:
pass
elif cmd == '-s':
ip.link('set', index=int(oididx), IFLA_IFALIAS=value)
ip.close()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment