- Subscribe to RSS Feed
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Perl subroutine to make it easier to pull configs in JUNOScript ..
[ Edited ]
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Flag for a Moderator
10-08-2009 08:29 PM - last edited on 10-08-2009 08:30 PM
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:
evice(%deviceinfo);
unless ( ref $jnx ) {
die "ERROR: $deviceinfo{hostname}: failed to connect.\n";
}
# send the command and receive a XML:
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/jun
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:
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:
OM:
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></confi
</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></conf
###### 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++;
}
return $indexarray[0];
}
###### SNIPPET #3 ###############
And then call it like so:
###### SNIPPET #4 #############
my $domdoc = new XML:
OM:
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...
Re: Perl subroutine to make it easier to pull configs in JUNOScript ..
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Flag for a Moderator
10-29-2009 11:52 AM
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
System and Network Administrator
St Marys College of California

