Contributor
ringwyrm
Posts: 15
Registered: 08-11-2008

Perl subroutine to make it easier to pull configs in JUNOScript..

[ Edited ]

You can use "get_configuration" to pull configurations from a device in Perl.  The syntax is something like this:

 

####  SNIPPET #1 #########

my $query = "get_configuration";
my %queryargs = (configuration => $conf,
                 database      => 'committed');

 

# connect TO the JUNOScript server
my $jnx = new JUNOS::smileyvery-happy:evice(%deviceinfo);
unless ( ref $jnx ) {
    die "ERROR: $deviceinfo{hostname}: failed to connect.\n";
}

 

# send the command and receive a XML::smileyvery-happy:OM object

my $res = $jnx->$query( %queryargs );
####  SNIPPET #1  ##########

 

Notice the part in blue.  The configuration item is pointing to a variable called $conf.  If you read the documentation, you will find this note about this variable:

 

##### FROM PAGE 217 OF JUNOSCRIPT API GUIDE (JUNOS 9.4) ##############

http://www.juniper.net/techpubs/software/junos/junos94/junoscript-guide/junoscript-guide.pdf 

 

 

A set of configuration statements or corresponding tag elements is defined as type $DOM. In the following example, the get_configuration method takes a set of configuration statements (along with two attributes):

get_configuration => {
configuration => $DOM,
format => $ATTRIBUTE,
database => $ATTRIBUTE,
},

########################################################################

 

 

What is this "type $DOM" they speak of?  Actually it is an element object from XML::smileyvery-happy:OM::Element.  The purpose of the "configuration" parameter is to specify a subsection of the configuration you want to retrieve (rather than the whole config).  For instance, on the command line you might type "show configuration system services" to see what service are configured.

 

############

ringwyrm@topM120-re0> show configuration system services
ftp;
ssh;
telnet;
xnm-clear-text;

#############

 

Suppose you want to achieve the same thing with Perl/JUNOScript....

 

####  SNIPPET #2 #########

my $domdoc = new XML::smileyvery-happy:OM::smileyvery-happy:ocument;

my $conf = $domdoc->createElement("configuration");

 

my $sys = $domdoc->createElement("system");

$conf->appendChild($sys);

 

my $serv = $domdoc->createElement("services");

$sys->appendChild($serv);

####  SNIPPET #2 #########

 

Now $conf is the root element of $domdoc and has a child-node $sys who has a child-node $serv.  Now per the SNIPPET #1 at the top of this article, we pass $conf to the query.  A debug on the script shows this:

 

############# 

--- begin request---
<rpc>
  <get-configuration database="committed"

>
<configuration><system><services/></system></configuration>
  </get-configuration>
</rpc>
--- end request ---

 

--- begin reply---
<configuration junos:commit-localtime="2009-10-08 20:07:54 CDT" junos:commit-seconds="1255050474" junos:commit-user="ringwyrm">
    <system>
        <services>
            <ftp>
            </ftp>
            <ssh>
            </ssh>
            <telnet>
            </telnet>
            <xnm-clear-text>
            </xnm-clear-text>
        </services>
    </system>
</configuration>
--- end reply ---

#######################

 

So, as you can see, somewhere the element object passed to the query was turned into the string "<configuration><system><services/></system></configuration>"  Looking at SNIPPET #2 it seems there might be a lot of typing if you want to really pull a very specific section of the configuration out that is multiple levels deep.  So, put this perl sub into your script:

 

###### SNIPPET #3 #################

sub makeDOMpath
{
 my ($domdoc, $confpath) = @_; 
 my @patharray = ('configuration',split(/\//,$confpath)); 
 my $index=0;
 my @indexarray;
 foreach (@patharray) {
  $indexarray[$index] = $domdoc->createElement("$patharray[$index]");
  if ($index > 0) {
   $indexarray[$index-1]->appendChild($indexarray[$index]);
  }
  $index++;
 }
 return $indexarray[0];
}

###### SNIPPET #3 ###############

 

 

And then call it like so:

 

###### SNIPPET #4 #############

my $domdoc = new XML::smileyvery-happy:OM::smileyvery-happy:ocument;
my $conf = makeDOMpath ($domdoc, "system/services");

 

my $query = "get_configuration";
my %queryargs = (configuration => $conf,
                 database      => "committed");

###### SNIPPET #4 #############

 

 

 

Next up:  Getting the JUNOScript API to install and run with ActiveState Perl on Windows...

 

 

Message Edited by ringwyrm on 10-08-2009 08:30 PM
Contributor
wsanders
Posts: 29
Registered: 08-10-2009
0

Re: Perl subroutine to make it easier to pull configs in JUNOScript..

I find it convenient to create an account on the machine and poll machines for configs:

 

        user jconfig {
            full-name "config manager";
            uid 2004;
            class jconfig;
            authentication {

                ssh-rsa "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA31...etc; ## SECRET-DATA
            }
        }

 

Then on a unix host:

 

for device in `cat list-of-devices` ; do

ssh -o ConnectTimeout=10 -i /<Path-to-Key>  -l jconfig $device "show conf" > $device

done

W Sanders
System and Network Administrator
St Marys College of California