Skip to content

Instantly share code, notes, and snippets.

@hiroyuki-sato
Last active March 5, 2021 02:19
Show Gist options
  • Save hiroyuki-sato/35a97418e851e4212713 to your computer and use it in GitHub Desktop.
Save hiroyuki-sato/35a97418e851e4212713 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";
@krajuskar
Copy link

krajuskar commented Oct 5, 2016

I am getting the error :
calling cfgPathGetChildNodes() without config session at /opt/vyatta/share/perl5/Vyatta/Config.pm line 122
Please help me on this.

Resolved :
By using respective return statement print $config->returnEffectiveValue("host-name");

@krajuskar
Copy link

Hi ,
I want to execute vyos command (set system host-name krajuskar) in perl script using perl api, can you please guide me how to use perl api to execute vyos command. I read PERL API but not get any idea to do that.

Please help me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment