DX NetOps

Expand all | Collapse all

how to use vnmsh from perl?

Jump to Best Answer
  • 1.  how to use vnmsh from perl?

    Posted 06-12-2015 09:34 AM

    Hi community

     

    I'm trying to build a script using perl which uses the vnmsh. The script should connect using $SPECROOT/vnmsh/connect and later read some attributes from some models for further analisis. Now I've got the script almost operational but some actions seem to fail in bash so I go to perl. I'm not a scripting/bash/perl guru but typically I get around just enough to simplify the job I'm doing.

     

    this is the script so far, it should only give back the modelhandles for all models in a certain collection. The seek command works fine if I execute it from a bash script.

     

    #!/usr/bin/perl

     

    $CLIPATH="/opt/Spectrum/vnmsh";

     

    system("$CLIPATH/connect ");

     

    system("CLISESSID=$$");        # Define the CLISESSID variable to a

    system("export CLISESSID");    # unique value and export it.

     

    print "DEB: connected to database\n";

     

    # get the list of interface model handles and store in @modelhandle

     

    @modelhandle = `$CLIPATH/seek attr=0x12adb,val=*zzz-somecollectioname* | tail -n +2 | awk '{print $1}'`;

     

    system("$CLIPATH/disconnect");

    print "DEB: disconnected from database\n";

     

     

    I run the script and get the following output

     

    connect: successful server-xyzxyz

    current landscape is 0x300000

     

    WARNING:  CLI is a powerful tool that allows a user to make changes

    directly to the SPECTRUM knowledge-base without the error checking

    provided by OneClick.  Please read the accompanying CLI user

    documentation before using the create, destroy, or update commands.

    DEB: connected to database

    Please connect first

     

    disconnect: successful from server-xyzxyz - connected for 0 hours, 0 minutes

    DEB: disconnected from database

     

     

    I split the output in 3 parts,

    1st sections shows that I'm connected,

    2nd section wants to do the seek command but fails with reason "Please connect first"

    3rd section disconnects successfully (even while one line  before I get an error stating that I need to connect first??)

     

    I'm a bit lost here, any a hint or even better, the answer?

     

    thanks in advance

    Hans



  • 2.  Re: how to use vnmsh from perl?

    Broadcom Employee
    Posted 06-12-2015 09:42 AM

    Someone wrote a Perl module for Spectrum CLI years ago:

     

    http://pages.cs.wisc.edu/~plonka/Spectrum-CLI/

     

    That might make things easier.



  • 3.  Re: how to use vnmsh from perl?

    Posted 06-12-2015 09:48 AM

    I saw the module as well but wasn't sure if it was still valid. It was written for Spectrum 5.0 which is kinda a bit of a while ago.

     

    Do you have experience with the module in combination with spectrum 9.4?



  • 4.  Re: how to use vnmsh from perl?

    Broadcom Employee
    Posted 06-12-2015 10:02 AM

    I haven't used the module in sometime, so it's possible that it doesn't work properly against Spectrum 9.4 although there hasn't been much in the way of big changes to CLI.  I suspect that the issue with your Perl script is that $CLISESSID isn't set in your seek command because each system call is a separate child process.  You can confirm by trying to print the value of $CLISESSID after you set it and see what comes back.  I think you'll need to set $CLISESSID using ${ENV}.



  • 5.  Re: how to use vnmsh from perl?

    Posted 06-12-2015 10:09 AM

    I've found out that it is the pipe which is causing the error. If I limit the command to this:

     

    @modelhandle = `$CLIPATH/seek attr=0x12adb,val=*zzz-somecollectioname*


    and this works fine.

     

    Some googling on "perl executing external commands pipe" gave me the solution. The pipe starts new child processes for the commands after the pipe (or something along that line) and those are not connected to the database.




  • 6.  Re: how to use vnmsh from perl?
    Best Answer

    Posted 06-12-2015 04:08 PM

    Robert is right. If you run your seek command like that:

     

    @modelhandle = `CLISESSID=$$ $CLIPATH/seek attr=0x12adb,val=*zzz-somecollectioname* | tail -n +2 | awk '{print \$1}'`;


    It should work. You also need to escape your \$1 variable or Perl will try to interpolate it.



  • 7.  Re: how to use vnmsh from perl?

    Posted 06-26-2015 09:49 AM

    While the perl module was written back in Spectrum 5, it should still work with current CLI.  Dave Plonka did a nice job of building the module so that it wraps around the CLI command set.  That way, it simply gives you a clean way to use perl with CLI.  The module itself is not large, and pretty easy to follow the code.

     

    For myself, I'm not doing much with CLI or perl any longer.  I've moved to using the REST interface where ever I can and programming more in Ruby these days. 



  • 8.  Re: how to use vnmsh from perl?

    Posted 06-12-2015 05:57 PM

    Thanks both!! I stand corrected, It works indeed.