Working with Type 2 NSH Scripts

Version 3
    Share:|

    BladeLogic supports four different types of NSH Scripts. A Type 2 NSH Script is passed the list of server targets by BladeLogic, and is responsible for controlling the execution of tasks for each server.

    The Type 4 Script is very similar to the Type 2 Script, but runs in Perl.

     

    When to use a Type 2 NSH Script

     

    Unlike Type 1 or Type 3 scripts, the Type 2 NSH Script does not itself attempt to connect to the agent on the remote server.

    A type 2 script is well suited to performing a blcli operation against a number of servers, perhaps retrieving property values.

    A type 2 script is also well suited for integrating to non BladeLogic applications, such as VMware. A good example would be a type 2 script that executes PowerCLI scripts against a Virtual Center Server. In this case operations on a target would not contact the actual target, but would ask Virtual Center to do that operation.

    Finally, a type 2 script is the option to use when running a script that doesn't have targets.

     

    How to create a Type 2 NSH Script.

     

    As with all NSH Scripts in BladeLogic, the script needs to be created. This can be achieved by using notepad, vi or a similar editor in NSH or a shell on either the local desktop computer or on a server managed by BladeLogic. Once the actual script has been created, then it can be edited in BladeLogic.

    The following screenshot shows the creation of a typical Type 2 NSH Script in BladeLogic

    temp.png

    A Type 2 NSH Script passes the list of servers either as a variable number of arguments (This happens if the %h macro is used), or as a file containing the server list (if the %f macro is used).

     

    Example of the %h macro

     

    In this case, the %h macro is specified as a parameter in BladeLogic

    temp.png

    The script to execute this script would typically look like this:

    for target in ${@}

        do

        echo "(${target})doing something\n"

    done

     

    Enclosing the server name in “()” means that BladeLogic will log output against that server. This can be useful when trying to decipher logfiles. Any log output without the server name will go against the overall job.

     

    Example of the %f macro

     

    The %f macro is similar to the %h macro except that BladeLogic creates a file containing the list of servers and provides an NSH path to that file.

    temp.png

    The script to interpret the host list using the %f macro would look like this:

    for target in `cat "${1}"`

        do

        echo "(${target})doing something\n"

    done


    Using getopts to process arguments

     

    Network Shell provides getopts to process script arguments. This can make your scripts look more professional and to be more robust in checking for errors.

    Documentation for getopts can be found on the web. I found: http://wiki.bash-hackers.org/howto/getopts_tutorial to be useful.

    Example script using getopts to use the %f macro. First of all, change the parameter so that the “flag” is set to “f”. This means that bladelogic will call the script with the “-f” flag.

    temp.png

    The script to process this flag looks like this:

    while getopts f: Option

    do

          case $Option in

                f) SERVERLIST=`cat "$OPTARG"`

                   ;;

                \?)   echo "Incorrect command line options."

                      echo "Usage: sample.nsh -f <file>"

                      exit 1;;

          esac

    done

     

     

    for server in $SERVERLIST

    do

        echo "(${server}) doing something"

    done