Perl API
<p>Vyatta::Config (/opt/vyatta/share/perl5/Vyatta/Config.pm) is a Perl API library for working with VyOS configuration (and Vyatta and EdgeOS too).</p>
<h1 id="using_the_library">Using the Library</h1>
<pre><code>use lib '/opt/vyatta/share/perl5';
use Vyatta::Config;
$config = new Vyatta::Config;</code></pre>
<h1 id="definitions">Definitions</h1>
<p><strong>Active config</strong> is config, currently used by system.</p>
<p><strong>Working config</strong> is config we are making during configuration session.</p>
<h2 id="effective_config">Effective config</h2>
<p>The definition of "effective" is different under these two scenarios.</p>
<ol>
<li>When used outside a config session, "effective" == "active". In other words, in such cases the effective config is the same as the running config.</li>
<li>When used during a config session, a config path (leading to either a "node" or a "value") is "effective" if ANY of the following is true.</li>
</ol>
<ul>
<li>active && working<br />
Path is in both active and working configs, i.e., unchanged.</li>
<li>!active && working && committed<br />
Path is not in active, has been set in working, AND has already been committed, i.e., "commit" has successfully processed the addition/update of the path.</li>
<li>active && !working && !committed<br />
Path is in active, has been deleted from working, AND has not been committed yet, i.e., "commit" (per priority) has not processed the deletion of the path yet, or it has been processed but failed.</li>
</ul>
<p><strong>Note:</strong> during commit, deactivate has the same effect as delete. So in such cases, as far as these functions are concerned, deactivated nodes don't exist.</p>
<h1 id="method_reference">Method reference</h1>
<p>Currently API has the following methods:</p>
<h2 id="usage_example">Usage example</h2>
<p>Background: Your helpdesk guys ask you to make a spreadsheet with remote-access PPTP VPN user information and keep it up to date. Of course you do not want to copy and paste this information by hand.</p>
<p>All spreadsheet software supports import from CSV, so the only thing we need to do is to extract values from config and print then as comma separated lines, passwords enclosed into double quotes for the case they contain a comma and add some header. Like this:</p>
<p><code>user,password,address,is_disabled</code><br />
<code>johnsmith,"this,isapass",192.0.2.41,no</code></p>
<p>Here we go:</p>
<pre><code>#!/usr/bin/perl
use strict;
using the Vyatta perl API
use lib "/opt/vyatta/share/perl5/";
use Vyatta::Config;
Settings
my $proto = "pptp";
my $delimiter = ",";
CSV RFC4180 requires to use CRLF (\r\n) as line break
my $header = "user,password,address,disabled\r\n";
Create a config instance
my $config = new Vyatta::Config;
Set default config level, this allows to use relative paths
$config->setLevel("vpn pptp remote-access authentication local-users username");
Obtain array of user names
my @users = $config->listNodes();
Print file header
print $header;
Now walk through the user names, obtain values and print them
foreach my $user (@users) {
- Since we set config level to "vpn pptp remote-access authentication local-users username"
- we now can specify only the remaining part as methods argument,
- it will be appended to the path we used in setLevel() my $password = $config->returnValue("$user password"); my $address = $config->returnValue("$user static-ip"); #"disable" is a leaf node with no value, so we should check
- if it exists instead of obtaining its value my $disabled = $config->exists("$user disable") ? "yes" : "no";
- It's not prohibited to use "," in passwords,
- so we should enclose them into quotes to prevent
- the parser from treating them as delimiters $password = "\"$password\"";
my $line = join($delimiter, $user, $password, $address, $disabled); print "$line\r\n";
}</code></pre>
<p>Check it works:</p>
<pre><code>vyatta@R1# show vpn pptp remote-access authentication local-users
username joerandomuser {
disable password qwerrty static-ip 10.91.17.105
}
username johnsmith {
password this,isapass
}
vyatta@R1# perl ./pptpusers.pl
user,password,address,disabled
joerandomuser,"qwerrty",10.91.17.105,yes
johnsmith,"this,isapass",,no</code></pre>
<p><a href="Category:_Development" title="wikilink">Category: Development</a></p>
- Last Author
- Unknown Object (User)
- Last Edited
- Apr 14 2020, 10:03 PM