Tuesday 7 July 2015

FPC/Lazarus Telnet client For FlightGear (using Synapse).


This software is not in  production status , it's just a sample and may be subject for further development !
Recently , I searched the internet for an example of telnet client written in FPC ,
(that's my way to do things, search for the sample and improve it ) , I used the synapse library
many times for my network based applications , but for a telnet application ,there are few examples and no one worked for me . so I tried to fix one of them and share  the working example with the wide community of FPC/ Lazarus or Delphi users .
The example is originally extracted from the Lazarus wiki pages,  and especially made to work with the FlightGear internal Telnet server ,


The client interface is fully designed for Flightgear server but it can be used with any Telnet server you want , and (of course )you can edit it to fit your needs .
Again, Note that it uses the telnetsshclient unit Here is the client code :

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, telnetsshclient;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button_Connect: TButton;
    Button_SendCmd: TButton;
    Edit_Port: TEdit;
    Edit_Cmd: TEdit;
    Edit_HostIP: TEdit;
    Image1: TImage;
    Label_FG: TLabel;
    Label_Port: TLabel;
    Label_Cmd: TLabel;
    Label_Log: TLabel;
    LabelHost: TLabel;
    Memo_Log: TMemo;
    StaticText1: TStaticText;
    procedure Button_ConnectClick(Sender: TObject);
    procedure Button_SendCmdClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  comm: TTelnetSSHClient;
  Command: string;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button_ConnectClick(Sender: TObject);
begin
  comm:=TTelnetSSHClient.Create;
  comm.HostName:= Edit_HostIP.Text;
  if comm.HostName='' then
  begin
    ShowMessage('Please specify hostname .');
    exit;
  end;
  comm.TargetPort:= Edit_Port.Text;
  if comm.TargetPort ='' then
  begin
    ShowMessage('Please specify target port .');
    exit;
  end;
  comm.ProtocolType:=Telnet;
  Memo_Log.Append('connecting ..' + comm.Connect);
  if comm.Connected then
  begin
    Memo_Log.Append('Connected :)');
    Memo_Log.Append('Server: ' + comm.HostName + ':'+comm.TargetPort);

  end;unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, telnetsshclient;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button_Connect: TButton;
    Button_SendCmd: TButton;
    Edit_Port: TEdit;
    Edit_Cmd: TEdit;
    Edit_HostIP: TEdit;
    Image1: TImage;
    Label_FG: TLabel;
    Label_Port: TLabel;
    Label_Cmd: TLabel;
    Label_Log: TLabel;
    LabelHost: TLabel;
    Memo_Log: TMemo;
    StaticText1: TStaticText;
    procedure Button_ConnectClick(Sender: TObject);
    procedure Button_SendCmdClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  comm: TTelnetSSHClient;
  Command: string;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button_ConnectClick(Sender: TObject);
begin
  comm:=TTelnetSSHClient.Create;
  comm.HostName:= Edit_HostIP.Text;
  if comm.HostName='' then
  begin
    ShowMessage('Please specify hostname .');
    exit;
  end;
  comm.TargetPort:= Edit_Port.Text;
  if comm.TargetPort ='' then
  begin
    ShowMessage('Please specify target port .');
    exit;
  end;
  comm.ProtocolType:=Telnet;
  Memo_Log.Append('connecting ..' + comm.Connect);
  if comm.Connected then
  begin
    Memo_Log.Append('Connected :)');
    Memo_Log.Append('Server: ' + comm.HostName + ':'+comm.TargetPort);

  end;

end;

procedure TForm1.Button_SendCmdClick(Sender: TObject);
begin
  Command:= Edit_Cmd.Text+ #10 + #13;
  Memo_Log.Append('*** Sending ' + Command);
  Memo_Log.Append('*** Begin result****');
  Memo_Log.Append(comm.CommandResult(Command));
  Memo_Log.Append('*** End result****');
  Memo_Log.Append('');

end;

end.

end;

procedure TForm1.Button_SendCmdClick(Sender: TObject);
begin
  Command:= Edit_Cmd.Text+ #10 + #13;
  Memo_Log.Append('*** Sending ' + Command);
  Memo_Log.Append('*** Begin result****');
  Memo_Log.Append(comm.CommandResult(Command));  // Here we send the command via our socket .
  Memo_Log.Append('*** End result****');
  Memo_Log.Append('');

end;

end.


Download :  the only currently compiled version is a 64 bit executable for Linux based systems (Download here ), for other systems you can download the source and
compile to your OS , (Tell me if you want a pre-compiled windows version ) .
Get the project source code from here .

No comments:

Post a Comment