Server Automation Interface: some examples to use custom scripts triggered on server/node events (NoMachine 4 or later)
Examples below applies to NoMachine 4 or later.
Example 1:
Use a custom script to run a specific application inside the session, e.g. Firefox
1. Create the runApplication.sh script. The script must be executable.
2. Place the script in a directory that is accessible by the node, i.e. accessible by the connecting user since the node runs as such user.
3. The script should be similar to:
#!/bin/sh
#variables
userName=$2
displayNumber=$4
export HOME=/home/$userName
export DISPLAY=:$displayNumber
export PATH="/bin:/usr/bin"
firefox 0<&- &>/dev/null &
exit 0
4. In the node configuration file, namely /usr/NX/etc/node.cfg on Linux set the following key:
UserScriptAfterSessionStart = "/usr/NX/scripts/custom/runApplication.sh"
Example 2:
Use custom scripts to run a dialog and provide hostname and display info to the end-user
If you want to provide additional information to the end-user you can create a custom script for issuing dialog messages via the NoMachine client.
Example below explains how to show a dialog message providing information about hostname and display. This script will be executed when a new session is started and when a virtual desktop is connected in 'shadow' mode. It works also for custom sessions. In a multi-node environment, you have to apply the custom script on each node.
1. Create the displayMessageStart.sh script. The script must be executable.
2. Place the script in a directory that is accessible by the node, i.e. accessible by the connecting user since the node runs as such user.
3. The script should be similar to:
#!/bin/sh
sessionID=$1
userName=$2
sessionType=$3
displayNumber=$4
host=`hostname`
message="$userName@$host:$displayNumber"
export HOME=/home/$userName
if [ "$sessionType" == "virtualAttach" ] ; then
sessionPath=$(find /usr/NX/var/log/node/ -regextype posix-egrep -type d -regex /usr/NX/var/log/node/C-.*$sessionID 2>/dev/null)
export XAUTHORITY="$sessionPath/authority"
displayNumber=$(grep "shadow=" $sessionPath/options | sed 's/.*shadow=:(.*):.*/1/')
fi
/usr/NX/bin/nxrunner --dialog 'ok' --message "$message" --display :$displayNumber --class 'info' --allowmultiple > /dev/null 2>&1 &
exit 0
Note: for NoMachine versions previous than v8, replace 'nxrunner' with 'nxclient'.
4. In the node configuration file, namely /usr/NX/etc/node.cfg on Linux set the following key:
UserScriptAfterSessionStart = "/usr/NX/scripts/custom/displayMessageStart.sh"
Example 3:
Use custom scripts to run a dialog showing an error message if the remote application fails to run
This example is suitable for custom sessions, but can be used also for other session types. Stderr and Stdout of X client applications is redrected to the clients file log in the session directory created in /usr/NX/var/log/node. This script retrieve possible errors from that file and show them in a dialog displayed to the end-user.
To set-up this custom script, follow the same instructions provided for Example 2, but change the script at point 3 to the example below.
#!/bin/sh
sessionID=$1
userName=$2
sessionType=$3
displayNumber=$4
host=`hostname`
message="$userName@$host:$displayNumber"
export HOME=/home/$userName
if [ "$sessionType" == "virtualAttach" ] ; then
sessionPath=$(find /usr/NX/var/log/node/ -regextype posix-egrep -type d -regex /usr/NX/var/log/node/C-.*$sessionID 2>/dev/null)
export XAUTHORITY="$sessionPath/authority"
displayNumber=$(grep "shadow=" $sessionPath/options | sed 's/.*shadow=:(.*):.*/1/')
fi
appOutput=$(cat "$NX_SYSTEM/var/log/node/C-$host-$displayNumber-$sessionID/clients")
/usr/NX/bin/nxrunner --dialog 'ok' --message "$appOutput" --display :$displayNumber --class 'info' --allowmultiple > /dev/null 2>&1 &
exit 0
Note: for NoMachine versions previous than v8, replace 'nxrunner' with 'nxclient'.
Example 4:
Use a custom script to allow/forbid access to the user based on the client version.
This script retrieves the version of the connecting client and forbids access if it is not v8.
1) Create the custom script, check_client_version.sh.
The custom script must be executable. Custom scripts set-up in server.cfg are common to all the users who are accessing the server and are executed by the nxserver program. Since nxserver is running as the nx user, you have to grant this user the necessary permissions in order to execute the custom script.
The check_client_version.sh custom script will exit with 1 when the version of the connecting client is not v8 and the script fails.
The user will get a message like 'The session negotiation failed. Error: Execution of custom script failed'.
Content of the script:
#!/bin/bash
echo "script before login $* " >> /tmp/log
/usr/bin/env >> /tmp/log
shortversion=$(cut -d '.' -f 1 <<< "$NX_RUNNER_VERSION")
echo "Version is: ${shortversion}" >> /tmp/log
if test "${shortversion}" == "8";
then
exit 0
else
exit 1
fi
2) Edit the /usr/NX/etc/server.cfg file on the server host and enable the UserScriptBeforeLogin key:
- remove the pre-pending # from the key name and
- set there path to the custom script
Note: for NoMachine versions previous than v8, replace 'nxrunner' with 'nxclient'.
