|
|
|
|
|
- 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 enables remote commands to be executed securely on these machines. A server can therefore support the secure SSH protocol to receive remote commands. In WLanguage, SSH functions can be used to execute commands via this protocol. The protocol is SSH-2. Two methods can be used to communicate with an SSH server: Direct sending of commands The direct sending of commands is performed by SSHCommand. The parameters for connecting to the SSH server must be described in an sshSession variable. 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)
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: Connect to the SSH serverConnection to the server is made using the WLanguage function SSHConnectShell. This function expects a sshSession variable as a parameter, which contains the SSH server connection information: - server address,
- username,
- password,
- port to use.
gSessionSSH is sshSession
gSessionSSH.Address = SAI_Server
gSessionSSH.Port = SAI_Port
gSessionSSH.User = SAI_User
gSessionSSH.UserPassword = SAI_Password
SSHConnectShell(gSessionSSH)
Remarks: - We recommend changing the default port (port 22). Since SSH is an extremely widespread protocol, it is also much "attacked".
- It is advisable to filter the IPs authorized to access the SSH protocol.
Connection via a private/public key pairYou can also connect to the SSH server using a public/private key pair.
gSessionSSH is sshSession
gSessionSSH.Address = SAI_Server
gSessionSSH.Port = SAI_Port
gSessionSSH.User = SAI_User
gSessionSSH.PrivateKey = SAI_PrivateKey
gSessionSSH.PrivateKeyPassword = SAI_Password
SSHConnectShell(gSessionSSH)
In this case, the server has the public key (see your SSH server documentation for the necessary configuration). The client (the connecting 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 ordersCommands are sent via the SSHWrite function. This function expects as parameter: - SSH connection, through variable type sshSession,
- the command to be executed, in a Buffer variable.
The command sent must end with a <10>.
sOrder is TO Buffer = SAI_Commande_ à_envoyer + Charact(10)
SSHWrite(gSessionSSH, sOrder)
Step 3: Reading the resultsResults and outputs are read out using the WLanguage function SSHRead. This function reads the text returned by the SSH server. Step 4: DisconnectingDisconnecting from the SSH server is very simple, using the WLanguage function SSHDisconnectShell and passing the: SSHDisconnectShell(gSessionSSH)
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…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|