SSH Client for Windows
Home Download Purchase Support Manual Screenshots Media About

4.5 Usage Examples

Sample scripts that are described here are given in the text form. To add a script into Private Shell, copy it from this manual and then use the Edit as text button in the Private Shell Script Editor dialog to paste it.

1. Connecting to a server

	Load profile (Profile="");
	Connect ();
	End script ();

This sample script has only 3 actions. The first action loads profile into the current settings, the second action connects to the server using the current settings (i.e. the settings that have just been loaded from the profile) and the third one ends script execution.

Finally you can assign a hot key to this script and thus connect to the server on a hot key press.

2. Opening SFTP window in the current terminal folder

It is possible that while working in the terminal you need to open an SFTP client window in the folder that is current for your terminal session. You can do this using the following script.

	Put ($Text="pwd\\n");
	Wait for (Text="(?m)^(\\/.*?)[\\r\\n]+", Regexp=1, Timeout=2000, Label="end");
	New window (Type=1, Tab=0, Switch=1, Connection=0, Label="end");
	Change folder ($Folder="$1");
	End script ();

The first action puts pwd\n into the terminal command line (this command prints the current terminal folder). The second action waits for the pwd response and filters out the current folder using a regular expression. If no appropriate response is received during 2 second interval, script execution ends.

The third action opens a new SFTP window and tells Private Shell to end script execution in the parent window. And the final (fourth) action changes current SFTP folder.

Please note that string preprocessing must be turned on for the first and the fourth actions in order \n to work as the Enter key and substitution of the $1 variable. When a script is saved to a text file, string preprocessing is denoted by dollar sign before action parameters (i.e. ($Folder="$1") in this example).

3. Performing the same task on multiple servers

It is possible that you need to perform the same task on multiple servers. It is a routine work if you do it yourself, but with Private Shell scripting you can do it on a hot key press.

First, you need to create a text file that contains all server names you are going to connect to. Each server name must be on its own line and there must not be any spaces. This file may looks as follows:

Then save this file on a local drive. For example, it can be c:\servers.txt. The next important thing is that you need to set up the same authentication on all servers you are going to connect to. It can be public key authentication with the same key on all servers. Here we assume that there is a Private Shell profile named MyProfile that allows connecting to all servers and successfully authenticates on them all.

Finally, use the following script to achieve the goal:

	Set Value (Var="fileName", Value="c:\\srv.txt");
	Load profile (Profile="1");
	Read file ($File="$fileName", Regexp="([^\\\\r\\\\n]+)", Begin=1, Label="end");
	Change parameter ($Name="Server name", Value="$1");
	Connect ();
# Perform here any actions you need
# *********************************
	Put ($Text="ls -al\\n");
	Delay (Delay=2000);
# *********************************
	Disconnect ();
	Read file ($File="$fileName", Regexp="([^\\\\r\\\\n]+)", Begin=0, Label="end");
	Goto (Label="loop");
	End script ();

First we assign the file name to a variable and load our profile into the current Private Shell settings. Then we read one line from the file without CR/LF into the special variable $1 using a regular expression and set the Server name parameter in the current Private Shell settings to the value being read.

Then we connect to the server and perform necessary actions (you can add/replace actions with that you need within the **..** lines). And finally we disconnect from the server, read next line from the file and continue our loop.

The script stops when it goes through the entire file (the execution control is transferred to the end label when it is not possible to read the next server name from the file).

Comment On This Topic: