|
|
|
|
|
|
|
|
|
|
- Principle
- How to?
- Direct sending of commands
- Dialog with an SSH server
Communicating with an SSH server
SSH (Secure Shell) is a secure network protocol used to establish an encrypted connection between two systems. This protocol is often used for remote administration of servers, as it ensures the confidentiality and integrity of exchanged data, thus protecting against interception and attacks. This protocol makes it possible to securely send commands to remote computers. A server can therefore support the SSH to receive remote commands. WLanguage features SSH functions that can be used to execute commands using this protocol. The protocol supported is SSH-2. Two methods can be used to communicate with an SSH server: Direct sending of commands Commands can be sent directly using SSHCommand. SSH server connection parameters must be defined in a variable of type sshSession. Example: cMySession is sshSession
buffOutput is Buffer
cMySession.Address = "127.0.0.1"
cMySession.Port = 22
cMySession.User = "login"
cMySession.UserPassword = "pass"
nExitCode is int
sOutput is ANSI string
sOutputErr is ANSI string
(nExitCode, sOutput, sOutputErr) = SSHCommand(cMySession, EDT_Command)
IF ErrorOccurred THEN
Error(ErrorInfo(errFullDetails))
RETURN
END
EDT_ExitCode = nExitCode
EDT_StdOut = UTF8ToString(sOutput)
EDT_StdErr = UTF8ToString(sOutputErr)
Note: By default, the SSHCommand command opens the SSH session, executes the command and then closes the SSH session. Subscription-exclusive new featureTo execute multiple SSH commands without closing the SSH session, simply keep the SSH connection active and close it after executing the commands, using the following functions:
| | | Keeps the SSH session active between several commands sent to an SSH server (using the SSHCommand function). This function is only available in subscription-based versions, starting with WINDEV Suite 2025 - Update 2. | | Stops the SSH session that was active to send a series of commands. This function is only available in subscription-based versions, starting with WINDEV Suite 2025 - Update 2. |
Dialog with an SSH server The dialog with the SSH server is performed by the following functions:
The parameters for connecting to the SSH server must be described in an sshSession variable. Step 1: Connecting to the SSH serverConnection to the server is made using the WLanguage SSHConnectShell function. This function expects a variable of type sshSession as a parameter, which contains the SSH server connection details: - server address,
- username,
- password,
- port to use.
gSSHSession is sshSession
gSSHSession.Address = EDT_Server
gSSHSession.Port = EDT_Port
gSSHSession.User = EDT_User
gSSHSession.UserPassword = EDT_Password
SSHConnectShell(gSSHSession)
Remarks: - We recommend changing the default port (port 22). Since SSH is an extremely widespread protocol, it is also the target of numerous attacks.
- It is recommended to filter the IPs authorized to access the SSH protocol.
Connection using a private/public key pairThe connection to the SSH server can also be made using a public/private key pair.
gSSHSession is sshSession
gSSHSession.Address = EDT_Server
gSSHSession.Port = EDT_Port
gSSHSession.User = EDT_User
gSSHSession.PrivateKey = EDT_PrivateKey
gSSHSession.PrivateKeyPassword = EDT_Password
SSHConnectShell(gSSHSession)
In this case, the public key is placed on the server (see SSH server documentation for necessary configuration). The client (application) owns the private key. Authentication is therefore performed by encrypting a text sent to the server. If the server is able to decrypt this text, it means that the client has the private key. This private key can be password protected. Step 2: Sending commandsCommands are sent with SSHWrite. This function expects as parameter: - the SSH connection, specified with a variable of type sshSession,
- the command to be executed, in a variable of type Buffer.
This command must end with <10>.
sCommand is Buffer = EDT_Command_to_send + Charact(10)
SSHWrite(gSSHSession, sCommand)
Step 3: Reading the resultsTo read the output, use the WLanguage SSHRead function. This function reads the text returned by the SSH server. Step 4: DisconnectionTo disconnect from the SSH server, simply use SSHDisconnectShell and pass the connection as a parameter: SSHDisconnectShell(cSSHSession)
Full example:
cMySession is sshSession
cMySession.Address = "127.0.0.1"
cMySession.Port = 22
cMySession.User = "login"
cMySession.UserPassword = "pass"
IF SSHConnectShell(cMySession) THEN
Info("Session started")
bufOutput is Buffer = "data"
SSHWrite(cMySession, bufOutput)
SSHDisconnectShell(cMySession)
END
This page is also available for…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|