Infoblox Community
Reply

How to access a file in the sandbox using Perl

Techie
Posts: 6
Registered: ‎11-26-2014
RParker_1
Techie
Posts: 6

Hi, Not sure if anyone can suggest how to do this but I have a file that I (want to) store in the Sandbox, containing new passwords against a network domain in CSV format.

 

So, it will be something like, 

domain1,password1

domain2,password2

domain3,password3

 

How can I use Perl to open the file, read and then close the file.

I thought about Lists, and I can do this in Lists & CCS, but I want to keep the passwords hidden from the general user.

I have other uses for stored files but just need a jump start on how to access the files.

 

Thanks in advance.

Re: How to access a file in the sandbox using Perl

Expert
Posts: 97
Registered: ‎05-29-2012
Expert
Posts: 97

Maybe this will help jumpstart you.  This script logs into a router, shows the config and routes and writes them to a file in the sandbox (/mnt/host/data/userdata/DeviceRoutes).  Please note that you will need to create the DeviceRoutes directory first.  This script assumes it is already created.

 

#!/usr/bin/perl -w
# BEGIN-SCRIPT-BLOCK
#
# Script-Filter: $name ne "unknown"
#
# Script-Login:
# true
#
# Script-Variables:
#
# END-SCRIPT-BLOCK
use strict;
use warnings;
use NetMRI_Easy;

my $easy = new NetMRI_Easy ({ api_version => 2.10 });

my $SetTerm = "";
my $RunCfg = "";
my $VRF_List = "";
my $All_Routes = "";

$SetTerm = $easy->send_command("term len 0");
$RunCfg = $easy->send_command("show running-config");
$All_Routes = $easy->send_command("show ip route vrf all");

unless(-e "/mnt/host/data/userdata/DeviceRoutes" or mkdir "/mnt/host/data/userdata/DeviceRoutes") {die "Unable to create DeviceRoutes Dir";}

# Write the Config
my $filename = join("", "/mnt/host/data/userdata/DeviceRoutes/", $main::name, "_config.txt");
open(DATA, ">$filename");
print DATA "$RunCfg";
close DATA;

# Write the Routes
my $filename = join("", "/mnt/host/data/userdata/DeviceRoutes/", $main::name, "_raw_routes.txt");
open(DATA, ">$filename");
print DATA "$All_Routes";
close DATA;

1;

 

Cheers,

Lon.

Re: How to access a file in the sandbox using Perl

Techie
Posts: 6
Registered: ‎11-26-2014
RParker_1
Techie
Posts: 6

Hi Lon,

 

Many thanks for your reply. However I cant seem to get it working and I am not sure why.

I did the following amendments once I realised it wasnt doing what it should.

 

  • confirmed that it was trying to look for the dir (-e)  by forcing an error by changing the directory whilst I had the dir check lines (this worked)
  • removed the dir check lines to simplify things for testing.
  • manually created the directory DeviceRouters in the suggested directory.
  • attached to the Sandbox and changed dir to the DeviceRouters
  • used the exact file path and file name in the open statement.

The script runs without errors but whilst the dir is there, there are no contents in the dir (as seen in the following).

Any ideas what is going wrong?

I even put a simple write statement to write "hello world", (print DATA "Hello World";) but this didnt work.

Running NetMRI Version 7.1.1

 

[root@sandbox ~]# cd host/data/userdata/
[root@sandbox ~]# ls
DeviceRoutes
[root@sandbox ~]#

 

[root@sandbox ~]# pwd
/mnt/host/data/userdata/DeviceRoutes
[root@sandbox ~]# ls
[root@sandbox ~]#

[root@sandbox ~]# ls -all
total 0
drwxr-xr-x 2 nobody nobody 0 Feb 17 12:30 .
drwxrwxrwx 3 root   root   0 Feb 17 12:30 ..
[root@sandbox ~]#

 

Final test script

#!/usr/bin/perl -w
# BEGIN-SCRIPT-BLOCK
#
# Script-Filter: $name ne "unknown"
#
# Script-Login:
# true
#
# Script-Variables:
#
# END-SCRIPT-BLOCK
use strict;
use warnings;
use NetMRI_Easy;

my $easy = new NetMRI_Easy ({ api_version => 2.10 });

my $SetTerm = "";
my $RunCfg = "";

$SetTerm = $easy->send_command("term len 0");
$RunCfg = $easy->send_command("show running-config");

# Write the Config

open(DATA, ">/mnt/host/data/userdata/DeviceRoutes/test.txt");
print DATA "$RunCfg";
close DATA;


Highlighted

Re: How to access a file in the sandbox using Perl

Expert
Posts: 97
Registered: ‎05-29-2012
Expert
Posts: 97

It may be best if you post the log files from the job you are running.  This was an old script I grabbed, but I just tested what you posted and it works on my lab system just fine.

 

Be sure that the device you are selecting for the job is a Cisco device (or one that the set term len 0, and show run will work).

Re: How to access a file in the sandbox using Perl

Techie
Posts: 6
Registered: ‎11-26-2014
RParker_1
Techie
Posts: 6

Thanks for looking Lon,

Here are the logs. Funny thing is that the status.log doesnt reference the print statement.

Not sure if it should though.

I can see the running config in the session.log (last 500 lines).

Am I missing something more obvious. I did a Chmod 777 on the directory but no effect. The ownership is nobody:nobody.

 

+++ Looking up device information ...................................... OK
+++ Looking up device information ...................................... OK
+++ Looking up job specification information ........................... OK
+++ Loading ccs file ................................................... OK

+++ Script: 1_Q6_Write_Read_Sandbox_File
+++ Script-Filter ...................................................... MATCH
+++ Looking up device information ...................................... OK
+++ Looking up device information ...................................... OK
+++ Looking up job specification information ........................... OK
+++ Looking up authentication information .............................. OK
+++ Opening ssh session with 10.26.0.23 ................................ OK
+++ Got password prompt, sending '********' ............................ OK
+++ Prompt detect round [1] ............................................ OK
+++ Prompt type [0] round times [1] [1] ................................ OK
+++ Prompt [LHR8545NORT-A2#] verify [LHR8545NORT-A2#] .................. OK
+++ Length limit prompt to [LHR8545NOR] ................................ OK
+++ Sending 'enable' ................................................... OK
+++ Enable prompt detect round [1] ..................................... OK
+++ Prompt type [0] round times [1] [1] ................................ OK
+++ Prompt [LHR8545NORT-A2#] verify [LHR8545NORT-A2#] .................. OK
+++ Length limit prompt to [LHR8545NOR] ................................ OK
+++ Sending 'terminal no monitor' ...................................... OK
+++ Sending 'terminal no editing' ...................................... OK
+++ Sending 'terminal length 0' ........................................ OK
+++ 1. Sending 'term len 0' ............................................ OK
+++ 1. Sending 'show running-config' ................................... OK
+++ Closing session .................................................... OK

 

Last few lines of session.log

monitor session 1 source interface Fa1/0/1
monitor session 1 destination interface Fa1/0/4
ntp clock-period 36029355
ntp server 10.26.1.253
mac address-table aging-time 4000
end

LHR8545NORT-A2#exit
Connection to 10.26.0.23 closed.

*** Job Completed Successfully ***

Re: How to access a file in the sandbox using Perl

Expert
Posts: 97
Registered: ‎05-29-2012
Expert
Posts: 97

Do the credentials your are using to run the job have access to the sandbox?  You don't need sysadmin creds, but try using a sysadmin account and see if that gets you anywhere.

 

Other than this, I can't help further.  You can maybe wait and see if anyone else on the forum has an idea, or open a case and do a webex.

 

Re: How to access a file in the sandbox using Perl

Techie
Posts: 6
Registered: ‎11-26-2014
RParker_1
Techie
Posts: 6

Hi Lon,

Just tried as Admin but still no output file :-(

 

Will raise a call.

 

Many thanks for your help though & I will post back once I resolve.

 

Best regards

 

Russ

Re: How to access a file in the sandbox using Perl

Authority
Posts: 35
Registered: ‎11-05-2015
Authority
Posts: 35

I changed the following 

 

open(DATA, ">/mnt/host/data/userdata/DeviceRoutes/test.txt");

 

to

 

open(DATA, ">", "/mnt/host/data/userdata/DeviceRoutes/test.txt");

 

and your script worked like a charm in the lab

Re: How to access a file in the sandbox using Perl

Expert
Posts: 97
Registered: ‎05-29-2012
Expert
Posts: 97

Wow Rajiv, you'll need to explain that one to me .  Both formats should be correct as I recall, and the syntax where the Mode is with the filename is working fine on my system.  I just double checked, in case I'm having a senile moment.  So, I'm a bit puzzled.  Below is my exact code that worked also:

 

# Write the Config

 

open(DATA, ">/mnt/host/data/userdata/DeviceRoutes/test.txt");

print DATA "$RunCfg";

close DATA;

 

Is there an older version of Perl that doesn't let you do this?  If I do a perl -v in the sandbox, it says I'm on perl 5, version 14.  Not sure if there'd be a different version used if it was running as a job in NetMRI.

Re: How to access a file in the sandbox using Perl

Authority
Posts: 35
Registered: ‎11-05-2015
Authority
Posts: 35

Hey Lon, There is no arguing with you on Perl script's syntax Smiley Happy You're always right on that !

 

Both syntax should be correct. However, one worked for me in the Lab and other didn't. I used a 7.1.3 appliance to test this. So the possibilities would be

  1. May be a NetMRI bug in this version (related to quoting/unquoting)
  2. Perl Version specific (as you mentioned)
  3. Or may be an issue with my Lab appliance's sandbox

Either be the case, I will spend some time on this and get to the core of it. In the meanwhile, since the other syntax worked in the lab, this may resolve the issue that Russ is facing.

 

Irrespective of that, it is worth checking why the other syntax failed in the lab