summaryrefslogtreecommitdiff
path: root/src/opt/vyatta/sbin/if-mib-alias
blob: bc86f999fa07e72bbf36808d93df8c4f05bb5bd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#! /usr/bin/perl

# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc.
# All Rights Reserved.
#
# Author: Stephen Hemminger
# Date: October 2010
# Description: script is run as net-snmp extension to read interface alias
#
# **** End License ****

use strict;
use warnings;
use feature "switch";
no warnings 'experimental::smartmatch';

# Collect interface all alias values
sub get_alias {
    my @interfaces;

    open (my $ip, '-|', 'ip li')
       or die "Can't run ip command\n";
    my $index;
    while(<$ip>) {
       if (/^(\d+): ([^:]*): /) {
	   $index = $1;
	   $interfaces[$index] = $2;
       } elsif (/^ +alias (.*)$/) {
	   $interfaces[$index] = $1;
       }
    }
    close $ip;
    return @interfaces;
}

sub get_oid {
    my $oid = shift;
    die "Not a valid Object ID: $oid"
	unless ($oid =~ /.(\d+)$/);

    my $ifindex = $1;
    my @interfaces = get_alias();

    my $ifalias = $interfaces[$ifindex];
    print "$oid\nstring\n$ifalias\n" if $ifalias;
}

# OID of ifAlias [RFC2863]
my $BASE = '.1.3.6.1.2.1.31.1.1.1.18';

sub get_next {
    my $oid = shift;

    return get_next("$BASE.0")
	if ($oid eq $BASE);

    die "Not a valid Object ID: $oid"
	unless ($oid =~ /^(\S*)\.(\d+)$/);

    my $base = $1;
    my $ifindex = $2;
    my @interfaces = get_alias();

    while (++$ifindex <= $#interfaces) {
	my $ifalias = $interfaces[$ifindex];
	if ($ifalias) {
	    print "$base.$ifindex\nstring\n$ifalias\n";
	    last;
	}
    }
}

sub ifindextoname {
    my $ifindex = shift;

    open (my $ip, '-|', 'ip li')
	or die "Can't run ip command\n";
    my $index;
    while(<$ip>) {
	next unless (/^(\d+): ([^:]*): /);
	return $2 if ($1 == $ifindex);
    }
    return;
}

sub set_oid {
    my ($oid, $target, $value) = @_;
    die "Not a valid Object ID: $oid"
	unless ($oid =~ /\.(\d+)$/);
    my $ifindex = $1;
    unless ($target eq 'string') {
	print "wrong-type\n";
	return;
    }

    my $ifname = ifindextoname($ifindex);
    if ($ifname) {
	system("ip li set $ifname alias '$value' >/dev/null 2>&1");
	print "not-writeable\n"	if ($? != 0);
    }
}

sub usage {
    warn "Usage: $0 {-g|-n} OID\n";
    warn "       $0 -s OID TARGET VALUE\n";
    exit 1;
}

usage unless $#ARGV >= 1;

given ($ARGV[0]) {
    when ('-g') { get_oid ($ARGV[1]); }
    when ('-n') { get_next ($ARGV[1]); }
    when ('-s') { set_oid ($ARGV[1], $ARGV[2], $ARGV[3]); }
    default {
	warn "$ARGV[0] unknown flag\n";
	usage;
    }
}