NSH cheat sheet


This topic provides examples of frequently used NSH commands and script elements that can help you get started with writing NSH scripts.

Frequently used NSH commands

The following table lists few examples of NSH commands and their results. Note that because NSH is based on ZSH, you can find some helpful information by searching the web for ZSH guides (for example http://zsh.sourceforge.net/Guide/).

Note

To view a complete description of a command or utility, see the man page. To display a man page while using NSH, enter man <command>, such as man nsh.

Whenever you include the host name of a server as an argument in a command, ensure that your input matches the name specified when the server was added or enrolled. For example, if the server was added as server1.example.com (that is, using its fully qualified domain name), do not use server1 as the input in your NSH command or script.

To take this action

Use this command

Connect to a remote server.

host% cd //hostname host% uname -a

List directory contents on one or many remote machines.

host% ls -l //host1/directory
host% ls -l //host1/directory //host2/directory

Take actions on a remote file.

host% vi //host1/dir/file 
host% more //host1/dir/file 
host% tail -f //host1/dir/file 
host% cat //host1/dir/file 
host% md5sum //host1/dir/file

Copy one or more file or directories from one machine to another.

host% cp /directory/file //host2/directory
host% cp -r //host1/directory //host2/directory
host% cp //host1/directory/file //@/directory
host% ncp //host1/directory/file -d //host1/directory1 //host2/directory2

Compare a file that exists on multiple hosts

host% diff //host1/directory/file //host2/directory/file

Use n commands to retrieve pertinent os-level information about servers:

  • ndf — runs "df" across many machines
  • nps — runs "ps" across many machines
  • ntop — runs "top" across many machines
  • nover — provides system-level overview across many machines
  • nmem — summary of memory across many machines
  • nnet — summary of network info across many machines
  • nstats — system statistics across many machines

host% nmem -h 
host1 host% ndf -h 
host1 host2 host% ntop -h 
host1 host2 -t

 

 

Convert CSV output to XML using pipe

host% ndf --- h host1 host2 -c | csv2xml

Remotely execute a command that lives locally on the target server.

host% nexec <redhat_host> rpm -qa
host% nexec <solaris_host> pkginfo -l
host% nexec <windows_host> ipconfig -all

Note: When using nexec, you can use an ampersand (&) character at the end of the command line to execute your command in the background on a UNIX or Linux machine. For example: nexec <Target> "<command> [args] &"
This feature is not available on Windows operating systems.

Execute a command against many servers.

host% runcmd -h host 1 host 2 -e rm /file_being_deleted

Verify a process exists across many servers.

host% runcmd -f /hostlist -e ps -ef | grep process

Execute an NSH script across many servers

host% runscript -NH -V -f /hostlist -e //host/script

Verify the RSCD agent version, mapping, and license status.

host% agentinfo host1

View RSCD agent logfile contents.

host% logman cat -h host1 | tail

Execute a WMIC command on a remote Windows server.

nexec -ncq <windows_host> cmd /c '<WMIC_command>'

Examples:

host% nexec -ncq <windows_host> cmd /c 'echo test"test'
host% nexec -ncq <windows_host> cmd /c 'wmic path win32_groupuser
where (groupcomponent="win32_group.name=
\"Administrators\",domain=\"%COMPUTERNAME%\"")'

To view summarized descriptions of commands and utilities, see the alphabetized table in Summarized-descriptions-of-Network-Shell-commands.

NSH scripting elements

The following table provides descriptions and examples of commonly used elements within an NSH Script. It provides information on basic scripting elements such as checking if a file or directory exists, creating a for loop, or creating an if else condition.

To do this

Use this syntax

Examples / Comments

Identify the script as an NSH script

#!/bin/nsh

This should be the first line of an NSH script when run on a UNIX/Linux host to identify it as NSH.

Check to see if a file exists


[ -f <file> ]

Note: To see if a file does not exist, use an exclamation mark as in the following command:

[ ! -f <file> ]

(From the command line)

host% [ -f //@/c/WINNT/win.ini ]
host% echo $? 0
host% [ -f //@/c/WINNT/winx.ini ]
host% echo $? 1

Check to see if a directory exists

[ -d <directory> ]

Note: To see if a directory does not exist, use an exclamation mark as in the following command:

[ ! -d <directory> ]

(From the command line)

host% [ -d //@/c/WINNT ]
host% echo $? 0 host% [ -d //@/c/WIN ]
host% echo $? 1

Loop through a list, performing the same actions on each item

for X in $X..do..done

Another method for looping through a list, performing the same actions on each item. 

foreach X in $X..{..}
  • for TARGET in $TARGET_LIST do echo $TARGET done
  • foreach TARGET in $TARGET_LIST { echo TARGET }
  • {{code language="none"}}
    while read i

    {{/code}}
    do

    echo "${i}"
    done < server.txt
    where server.txt contains a list of host names (for example), one per line.

Check conditions and perform actions depending on results.

if..then..else..fi

 

if [ ! -d /tmp/blade]
then mkdir /tmp/blade
else echo "Directory already exists." fi

Miscellaneous Tips and Tricks

The following table lists various tips and tricks for using NSH:

To do this

Use this syntax

Examples / Comments

Execute a command in a remote path on a server

-D //<server>/<directory>

  • To run the OS ls command in /var/tmp/stage on <server>:

 nexec -D //<server>/var/tmp/stage <server> ls

  • To run the uname -a command against <server> without "cding" into <server>:
uname -D //<server> -a
  • To call cscript and run the myScript.vbs that is in the /C/Temp directory on the target without having to translate the NSH path /C/Temp to Windows path to C:\Temp:
nexec -D //<server>/C/Temp <server> cmd /c "cscript /nologo myScript.vbs"

Reference the host that NSH was launched from

//@

  • To cd back to the root directory of the system running NSH

 cd //@

  • To cd back to /tmp on the system running NSH
cd //@/tmp
  • To copy /tmp/file.txt from <server> to /tmp on the system running NSH

 cp //<server>/tmp/file.txt //@/tmp

Disconnect NSH from a system

disconnect <server>

To cd back to the system running NSH and closes the NSH connection to <server> instead of letting it idle out:

 cd //@;disconnect <server>

Where to go from here

Adding-a-Network-Shell-script

Creating-Network-Shell-Script-Jobs

 

Tip: For faster searching, add an asterisk to the end of your partial query. Example: cert*