*WEB/SFTP |
Interface with SFTP Server |
The *web/sftp utility is used to interface with a server that is running an SFTP server. It can be used to send files to, receive files from, delete files from, or get a directory listing from an SFTP server.
(The *web/sftp utility was added in PxPlus 2017.)
1. |
CALL "*web/sftp;send", server$, user$, password$, key_path$, key_passphrase$, insecure, timeout, files_to_send$, upload_dir$, case$, result$ |
2. |
CALL "*web/sftp;receive", server$, user$, password$, key_path$, key_passphrase$, insecure, timeout, files_to_receive$, download_dir$, case$, result$ |
3. |
CALL "*web/sftp;delete", server$, user$, password$, key_path$, key_passphrase$, insecure, timeout, files_to_delete$, case$, result$ (added in PxPlus 2018) |
4. |
CALL "*web/sftp;list", server$, user$, password$, key_path$, key_passphrase$, insecure, timeout, recursion, result$ |
Where:
server$ |
(Input) Contains the name of the SFTP server. It should be in the form of SERVER;SOCKET. Example: If no socket number is specified, then a default SFTP port of 22 is used. An SFTP sub-directory can be appended to the SERVER portion. See Important Note. Example: Important Note:
| ||||||
user$ |
(Input) Contains the user ID. Maximum length of user$ is 64 characters. | ||||||
password$ |
(Input) Contains the user password. If key_path$ is not null, this will be ignored. Maximum length of password$ is 64 characters. See SSH Authentication below. | ||||||
key_path$ |
(Input) Contains the full path of the OpenSSH Private key file to be used for authentication. If present, password$ will be ignored. If null, password authentication will be used. See SSH Authentication below. | ||||||
key_passphrase$ |
(Input) The passphrase for the OpenSSH Private key, if any. See SSH Authentication below. | ||||||
insecure |
(Input) If non-zero, this will avoid known_hosts checking. If zero, it will check for the server's Public key in the known_hosts file. See SSH Authentication below. | ||||||
timeout |
(Input) If non-zero, this parameter specifies the timeouts for communicating with an SFTP server. If zero, the timeout value is set to 60 seconds. | ||||||
files_to_send$ |
(Input) List of the files to send to an SFTP server or a pattern matching the files to send to an SFTP server. Maximum length of files_to_send$ is 32,000 characters. See Usage Details below. Note: | ||||||
files_to_receive$ |
(Input) List of the files to receive from an SFTP server or a pattern matching the files to receive from an SFTP server. Maximum length of files_to_receive$ is 32,000 characters. See Usage Details below. Note: | ||||||
files_to_delete$ |
(Input) List of the files to delete from an SFTP server or a pattern matching the files to delete from an SFTP server. Maximum length of files_to_delete$ is 32,000 characters. See Usage Details below. Note:
| ||||||
upload_dir$ |
(Input) Contains the path and the directory name on an SFTP server to use to store files. If null, the upload directory is the root directory of the SFTP server. | ||||||
download_dir$ |
(Input) Contains the path and the directory name on a client machine to use to store files. If null, the download directory is the working directory of the machine. | ||||||
case$ |
(Input) Possible values are:
| ||||||
recursion |
(Input) If non-zero, the directory listing returned will include the listing of all sub-directories as well. If zero, the directory listing will only list the current directory. | ||||||
result$ |
(Output) If doing a LIST (see
Format 3 above), the program loads this variable with the directory list results. |
SFTP uses SSH for communication. SSH allows for Private/Public key authentication, as well as password authentication. Private/Public key authentication will be attempted first before password authentication.
Creating a Private/Public Key
To create a Private/Public key pair for SSH authentication, follow the steps below:
Windows
|
1. |
Download and install putty (if not already installed) by visiting: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html. |
|
2. |
Launch puttygen.exe. |
|
3. |
Click the Generate button. |
|
4. |
Move the mouse around randomly in a blank area. |
|
5. |
Modify the default key comment to something like your_name@server_name. |
|
6 |
(Optional) Specify a key passphrase for added security. |
|
7. |
Click the Conversions menu item and select Export OpenSSH key. |
|
8. |
Copy the Public key from the screen area labeled Public key for pasting into the authorized_keys file in the SSH directory. Once the key has been copied, puttygen can be closed. |
UNIX/Linux
|
1. |
At the Command prompt, type: ssh-keygen -t rsa -b 2048 -C "your_email@example.com". |
|
2. |
When prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location. Alternatively, you could specify the location and filename of your key. |
|
3. |
(Optional) At the prompt, type a secure passphrase for added security. |
|
4. |
At the Command prompt, type: cat pub_key_path. Copy the output. |
Adding the Public Key to the Server
Now that a Public/Private key pair has been created, add your Public key to the server's authorized keys file using the steps below, assuming you have control over the server. (If you do not have control, the server administrator will need to add your Public key.)
|
1. |
Access the server. |
|
2. |
Navigate to the SSH directory, i.e. /root/.ssh. |
|
3. |
Open the authorized_keys2 file for editing. |
|
4. |
Append your Public key to the end of the file. |
|
5. |
Save the changes. |
Adding the Server's Public Key to the Client
The final step is to add the server's Public key to the client machine's known_hosts file using the steps below, assuming you have control over the server. (If you do not have control over the server, obtain the server's Public key from the server administrator.)
This final step is optional. However, if you do not perform this step, then you must set the insecure option to non-zero when using *web/sftp. Keep in mind that things will not be as secure, as the client will not be verifying the identity of the server.
|
1. |
Access the server. |
|
2. |
Navigate to the SSH configuration directory; i.e. /etc/ssh. |
|
3. |
At the Command prompt, type: cat pub_key_path. Copy the output. (The Public key is usually named something like ssh_host_rsa_key.pub.) |
|
4. |
On the client machine, open/create the known_hosts file for editing. (On UNIX/Linux, this is usually /root/.ssh/known_hosts. On Windows, this is %USERPROFILE%\AppData\Roaming\_ssh\known_hosts.) |
|
5. |
Navigate to the end of the file and start a new line. Then, insert the hostname or IP address, insert a space, and then append the server's Public key. |
|
6. |
Save the changes. |
files_to_send$ / files_to_receive$ / files_to_delete$
Wild characters are accepted in file names (unless MSK( ) regular expression pattern matching is enabled):
|
? |
Any one character |
|
* |
Any number of any characters |
Arguments can contain more than one file name. File names should be separated using one of the following: $8A$, $0A$, $0D0A$, or ; (semi-colon).
A number of options may be specified after a file name. Options should be separated from the file name by a | (pipe). Options are:
Overwrite |
0 = Always overwrite (default) |
Ignored in files_to_delete$. |
Recursion |
R = Recursion ON |
In files_to_delete$, only applies to pattern matching. |
Regular Expression Pattern Matching |
M = Enable MSK( ) regular expression pattern matching |
|
Example:
file_name1|1;c:\tmp\*.*|0R;C:\\reports\\report[0-9]+\.(pdf|doc|docx)|M
In this example, file_name1 will not be transferred if it currently exists. The directory c:\tmp and all files with it will be transferred and will always overwrite any existing file of the same name. It will also recurse through all sub-directories of c:\tmp and transfer all files in every sub-directory. All files in c:\reports that start with the name report, end with a number and have the extension .pdf, .doc or .docx will be transferred and will always overwrite any existing file of the same name.
MSK( ) Scan String for Mask
Interface with FTP Server