Friday, January 29, 2010

 

Synapse Based SSH Client


Many times, I needed a way to let my Delphi/FPC applications to connect to an SSH server, execute some commands, and get its results. Now I'm publishing a simple class based on Synapse's TTelnetSend class to do exactly what I needed.

Required ingredients

First of all, you'll need to grab a copy of the latest version of Synapse, now, to connect to an SSH server, the connection must be established by using the SSL protocol, and Synapse allows that kind of connections with plugins that allow filtering the data through OpenSSL, CryptLib and StreamSecII. Here, I'll use CryptLib, so you'll have to get this compiled version of cl32.dll for Windows, if you need the library compiled for Linux search for it in your repository (or use Google).

Now, configure the search paths of your compiler to find both, Synapse source and cryptlib.pas, the wrapper for cl32.dll.

Introducing TSSHClient class

This is a simple class to let you connect to an SSH server in an Object Oriented way, its internal parts where found in the Synapse's newsgroups, tested and arranged in a class.


unit sshclient;

interface

uses
tlntsend, ssl_openssl, ssl_openssl_lib, ssl_cryptlib;

type
TSSHClient = class
private
FTelnetSend: TTelnetSend;
public
constructor Create(AHost, APort, AUser, APass: string);
destructor Destroy; override;
procedure SendCommand(ACommand: string);
procedure LogOut;
function ReceiveData: string;
function LogIn: Boolean;
end;

implementation

{ TSSHClient }

constructor TSSHClient.Create(AHost, APort, AUser, APass: string);
begin
FTelnetSend := TTelnetSend.Create;
FTelnetSend.TargetHost := AHost;
FTelnetSend.TargetPort := APort;
FTelnetSend.UserName := AUser;
FTelnetSend.Password := APass;
end;

destructor TSSHClient.Destroy;
begin
FTelnetSend.Free;
inherited;
end;

function TSSHClient.LogIn: Boolean;
begin
Result := FTelnetSend.SSHLogin;
end;

procedure TSSHClient.LogOut;
begin
FTelnetSend.Logout;
end;

function TSSHClient.ReceiveData: string;
var
lPos: Integer;
begin
Result := '';
lPos := 1;
while FTelnetSend.Sock.CanRead(1000) or (FTelnetSend.Sock.WaitingData>0) do
begin
FTelnetSend.Sock.RecvPacket(1000);
Result := Result + Copy(FTelnetSend.SessionLog, lPos, Length(FTelnetSend.SessionLog));
lPos := Length(FTelnetSend.SessionLog)+1;
end;
end;

procedure TSSHClient.SendCommand(ACommand: string);
begin
FTelnetSend.Send(ACommand + #13);
end;

end.


A sample application

This is an example of how to execute an "df -h" command in an external SSH server, inspirated by a question in StackOverflow.

The example just connects to a server, execute the command and capture its output, just that.


program TestSSHClient;

{$APPTYPE CONSOLE}

uses
sshclient;

var
lSSh: TSSHClient;
begin
lSSh := TSSHClient.Create('[TARGET_HOST_OR_IP_ADDRESS]', '[PORT]', '[USER]', '[PASSWORD]');
if lSSh.LogIn then
begin
Writeln('Connected!.');
(* Get welcome message *)
Writeln(lSSh.ReceiveData);
(* Send command *)
lSSh.SendCommand('df -h');
(* Receive results *)
Writeln(lSSh.ReceiveData);
lSSh.LogOut;
Writeln('Logged out.');
end
else
Writeln('Can''t connect.');
lSSh.Free;
end.


Replace the words between '[' and ']' by the real ones and test it.

See you on my next post.

This page is powered by Blogger. Isn't yours?