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
- NSH scripting elements
- Miscellaneous Tips and Tricks
- Where to go from here
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/).
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 |
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:
| host% nmem -h
|
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] &" |
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..{..} |
|
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> |
nexec -D //<server>/var/tmp/stage <server> ls
uname -D //<server> -a
nexec -D //<server>/C/Temp <server> cmd /c "cscript /nologo myScript.vbs" |
Reference the host that NSH was launched from | //@ |
cd //@
cd //@/tmp
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