cli ssh client

Discussion of open issues, suggestions and bugs regarding network security and data protection solution - SecureBridge
Post Reply
dwriedel
Posts: 8
Joined: Wed 20 Jan 2016 18:46

cli ssh client

Post by dwriedel » Sun 31 Mar 2019 18:08

hello guys!

I try to create a ssh client. Goal is connect to ssh server and execute a command.
Unfornately I am failing before creating a connecting.

Code: Select all

program sshcli;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils,
  Classes,
  ScTypes,
  ScUtils,
  ScBridge,
  ScSSHClient;



var
  searchResult: TSearchRec;

  ScSSHClient: TScSSHClient;
  ScFileStorage: TScFileStorage;

  Key, NewServerKey: TScKey;




begin

  try
    

    ScFileStorage := TScFileStorage.Create(nil);
    ScSSHClient := TScSSHClient.Create(nil);
    ScSSHClient.KeyStorage := ScFileStorage;
    ScSSHClient.HostName := '192.168.x.y';
    ScSSHClient.User := 'user';
    ScSSHClient.Password := 'verysecret';

    Key := ScSSHClient.KeyStorage.Keys.FindKey(ScSSHClient.HostName);

    //Key := TScKey.Create(nil);
      try
        Key.Assign(NewServerKey);
        Key.KeyName := ScSSHClient.HostName;
        ScSSHClient.KeyStorage.Keys.Add(Key);
      except
        Key.Free;
        raise;
      end;

    ScSSHClient.Connect;



  except
    on E: Exception do
      writeln(E.ClassName, ': ', E.Message);
  end;

  ScSSHClient.DisConnect;
  Readln;

end.
I am using latest secure bridge version (Feb. 2019).

Regards,
dw

ViktorV
Devart Team
Posts: 2780
Joined: Wed 30 Jul 2014 07:16

Re: cli ssh client

Post by ViktorV » Mon 01 Apr 2019 11:46

To handle the TScSSHClient.OnServerKeyValidate event, use the following code:

Code: Select all

program sshcli;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils,
  Classes,
  ScTypes,
  ScUtils,
  ScBridge,
  ScSSHClient;

type
  TMyClass = class
  class procedure ScSSHClientServerKeyValidate(Sender: TObject;
    NewServerKey: TScKey; var Accept: Boolean);
  end;

var
  searchResult: TSearchRec;
  ScSSHClient: TScSSHClient;
  ScFileStorage: TScFileStorage;

class procedure TMyClass.ScSSHClientServerKeyValidate(Sender: TObject;
  NewServerKey: TScKey; var Accept: Boolean);
var
  Key: TScKey;
  fp, msg: string;
begin
  Key := ScFileStorage.Keys.FindKey(ScSSHClient.HostName);
  if (Key = nil) or not Key.Ready then begin
    NewServerKey.GetFingerPrint(haMD5, fp);
    Key := TScKey.Create(nil);
    try
      Key.Assign(NewServerKey);
      Key.KeyName := ScSSHClient.HostName;
      ScFileStorage.Keys.Add(Key);
    except
      Key.Free;
      raise;
    end;

    Accept := True;
  end;
end;

begin
  try
    ScFileStorage := TScFileStorage.Create(nil);
    ScSSHClient := TScSSHClient.Create(nil);
    ScSSHClient.KeyStorage := ScFileStorage;
    ScSSHClient.HostName := '192.168.x.y';
    ScSSHClient.User := 'user';
    ScSSHClient.Password := 'verysecret';
    ScSSHClient.OnServerKeyValidate := TMyClass.ScSSHClientServerKeyValidate;

    ScSSHClient.Connect;

  except
    on E: Exception do
      writeln(E.ClassName, ': ', E.Message);
  end;

  ScSSHClient.DisConnect;
  Readln;

end.

dwriedel
Posts: 8
Joined: Wed 20 Jan 2016 18:46

Re: cli ssh client

Post by dwriedel » Mon 01 Apr 2019 19:55

Thank you for your fast reply. Connection is working.
I enhanced now the code with SSHShell.

Code: Select all

var:
  searchResult: TSearchRec;
  ScSSHClient: TScSSHClient;
  ScFileStorage: TScFileStorage;
  ScSSHShell :   TScSSHShell;
 
 
 {….} 
  
begin
  try
    ScFileStorage := TScFileStorage.Create(nil);
    ScSSHClient := TScSSHClient.Create(nil);
    ScSSHShell  := TScSSHShell.Create(nil);
    ScSSHClient.KeyStorage := ScFileStorage;
    ScSSHClient.HostName := '192.168.x.y';
    ScSSHClient.User := 'user';
    ScSSHClient.Password := 'verysecret';
    ScSSHClient.OnServerKeyValidate := TMyClass.ScSSHClientServerKeyValidate; 

    ScSSHClient.Connect;
   
    //ScSSHShell.Connect;    
    ScSSHShell.ExecuteCommand('uname -a');



  except
    on E: Exception do
      writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
  ScSSHClient.DisConnect;
  Readln;

end.  
This produce an excepetion : "EScError: Client not defined"

Your Online Help is telling me "There are two ways to use this component. The first way is to execute a command with the ExecuteCommand method. " https://www.devart.com/sbridge/docs/ind ... ommand.htm

It seems that my method call is wrong. (In your Example App, your are using: ScSSHShell.WriteString.)

Thank you for help in advance!

ViktorV
Devart Team
Posts: 2780
Joined: Wed 30 Jul 2014 07:16

Re: cli ssh client

Post by ViktorV » Tue 02 Apr 2019 11:26

To solve the issue with "EScError: Client not defined", in your code sample, please try to replace line

Code: Select all

  ScSSHShell.ExecuteCommand('uname -a');
with

Code: Select all

  ScSSHShell.Client := ScSSHClient;
  ScSSHShell.ExecuteCommand('uname -a');

dwriedel
Posts: 8
Joined: Wed 20 Jan 2016 18:46

Re: cli ssh client

Post by dwriedel » Tue 02 Apr 2019 19:50

Thank you for your help.
Yes. It is working. I can connect to a linux host system and excute a command.

ViktorV
Devart Team
Posts: 2780
Joined: Wed 30 Jul 2014 07:16

Re: cli ssh client

Post by ViktorV » Wed 03 Apr 2019 07:00

It is good to see that the problem has been solved.
Feel free to contact us if you have any further questions about our products.

Post Reply