Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xiangchu0/bc4d9a9358a7c0f1e014024b92a9aa34 to your computer and use it in GitHub Desktop.
Save xiangchu0/bc4d9a9358a7c0f1e014024b92a9aa34 to your computer and use it in GitHub Desktop.
VyOS Perl API example.

Goal

I would like to get value of advertise-interval in vrrp-group 100. And I would like to use Perl API.

show config

interfaces {
    ethernet eth0 {
        address dhcp
        duplex auto
        hw-id 00:0c:29:8f:10:55
        smp_affinity auto
        speed auto
        vrrp {
            vrrp-group 100 {
                advertise-interval 1
                description test
                preempt true
                virtual-address 10.10.10.0/24
            }
        }

Sample script1.

Work fine as I expected. But I don't need foreach part.

#!/usr/bin/perl -w

use lib '/opt/vyatta/share/perl5';
use Vyatta::Config;
$config = new Vyatta::Config;

$config->setLevel("interfaces ethernet eth0 vrrp vrrp-group 100");
my @c = $config->listNodes();

#=begin
foreach my $i ( @c ){
  print $i,"\n";
}
#=end

print $config->returnValue("advertise-interval"),"\n";

Execution result.

vyos@foo# perl d.pl
advertise-interval
description
preempt
virtual-address
1
[edit]

Sample script 2

So I enabled =begin and =end part.

use lib '/opt/vyatta/share/perl5';
use Vyatta::Config;
$config = new Vyatta::Config;

$config->setLevel("interfaces ethernet eth0 vrrp vrrp-group 100");
my @c = $config->listNodes();

=begin
foreach my $i ( @c ){
  print $i,"\n";
}
=end

print $config->returnValue("advertise-interval"),"\n";

I expected it output advertise-interval value. But none of output.

vyos@foo# perl d.pl 
[edit]

show version

vyos@foo:~$ show version
Version:      VyOS 1.1.0
Description:  VyOS 1.1.0 (helium)
Copyright:    2014 VyOS maintainers and contributors
Built by:     maintainers@vyos.net
Built on:     Thu Oct  9 22:27:26 UTC 2014
Build ID:     1410092227-af6433f
#!/usr/bin/perl -w
use lib '/opt/vyatta/share/perl5';
use Vyatta::Config;
$config = new Vyatta::Config;
$config->setLevel("interfaces ethernet eth0 vrrp vrrp-group 100");
my @c = $config->listNodes();
foreach my $i ( @c ){
print $i,"\n";
}
print $config->returnValue("advertise-interval"),"\n";
#!/usr/bin/perl -w
use lib '/opt/vyatta/share/perl5';
use Vyatta::Config;
$config = new Vyatta::Config;
$config->setLevel("interfaces ethernet eth0 vrrp vrrp-group 100");
my @c = $config->listNodes();
#=begin
#foreach my $i ( @c ){
# print $i,"\n";
#}
#=end
print $config->returnValue("advertise-interval"),"\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment