BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Telnet Indy + Delphi, how to send commands via example

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock
View previous topic :: View next topic  
Author Message
Eric
Guest





PostPosted: Wed Sep 06, 2006 3:16 pm    Post subject: Telnet Indy + Delphi, how to send commands via example Reply with quote



Hello,

I use the telnet example from Indy.
In this example I can use a commandline field. That's ok, but I would
really like to use this
telnet in another way...
I would like to send commands from within the delphi program, for
example:
When I logon to my router (10.0.0.138) I am asked for my username and
password.
After this, I can give all kinds of commands especially for this
router.
I would like to have a button (later on via radiobuttons and a submit
button) that sends a few commands to the router, so not via the command
line... How to do this?


Eric


This is the telnet example:
---------------------------------------------------------------------------­------------------------------------------------------


unit mainform;


interface


uses
Windows, Messages, Graphics, Controls, Forms, Dialogs, ComCtrls,
StdCtrls, Spin,
SysUtils, Classes, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdTelnet;


type
TfrmTelnetDemo = class(TForm)
Memo1: TRichEdit;
edtServer: TEdit;
lblServer: TLabel;
spnedtPort: TSpinEdit;
lblPort: TLabel;
btnConnect: TButton;
btnDisconnect: TButton;
sbrStatus: TStatusBar;
Label1: TLabel;
edtSendCommand: TEdit;
IdTelnetDemo: TIdTelnet;
procedure btnConnectClick(Sender: TObject);
procedure btnDisconnectClick(Sender: TObject);
procedure Memo1KeyPress(Sender: TObject; var Key: Char);
procedure IdTelnetDemoConnected(Sender: TObject);
procedure edtSendCommandKeyPress(Sender: TObject; var Key: Char);
procedure IdTelnetDemoConnect(Sender: TObject);
procedure IdTelnetDemoDataAvailable(Sender: TIdTelnet;
const Buffer: String);
private
{ Private declarations }
public
{ Public declarations }
end;


var
frmTelnetDemo: TfrmTelnetDemo;


implementation


{$R *.DFM}


procedure TfrmTelnetDemo.btnConnectClick(Sender: TObject);
begin
IDTelnetDemo.Host := edtServer.Text;
IDTelnetDemo.port := spnedtPort.Value;
IdTelnetDemo.Connect;
end;


procedure TfrmTelnetDemo.btnDisconnectClick(Sender: TObject);
begin
IdTelnetDemo.Disconnect;
end;


procedure TfrmTelnetDemo.Memo1KeyPress(Sender: TObject;
var Key: Char);
begin
{we simply send the key stroke to the server. It may echo it back to


us}
if IdTelnetDemo.Connected then
IdTelnetDemo.SendCh(Key);
Key := #0;
end;


procedure TfrmTelnetDemo.IdTelnetDemoConnected(Sender: TObject);
begin
sbrStatus.SimpleText := 'Connected';
end;


procedure TfrmTelnetDemo.edtSendCommandKeyPress(Sender: TObject;
var Key: Char);
var
i : integer;
s : string;
begin
if IdTelnetDemo.Connected then
if (key = #13) then
begin
s := edtSendCommand.text;
for i := 1 to length(s) do
IdTelnetDemo.SendCh(s[i]);
IdTelnetDemo.SendCh(#13);
end;


end;


procedure TfrmTelnetDemo.IdTelnetDemoConnect(Sender: TObject);
begin
sbrStatus.SimpleText := 'Connect';
end;


procedure TfrmTelnetDemo.IdTelnetDemoDataAvailable(Sender: TIdTelnet;
const Buffer: String);
{This routine comes directly from the ICS TNDEMO code. Thanks to
Francois Piette
It updates the memo control when we get data}
const
CR = #13;
LF = #10;
var
Start, Stop : Integer;
begin
if Memo1.Lines.Count = 0 then
Memo1.Lines.Add('');


Start := 1;
Stop := Pos(CR, Buffer);
if Stop = 0 then
Stop := Length(Buffer) + 1;
while Start <= Length(Buffer) do begin
Memo1.Lines.Strings[Memo1.Lines.Count - 1] :=
Memo1.Lines.Strings[Memo1.Lines.Count - 1] +
Copy(Buffer, Start, Stop - Start);
if Buffer[Stop] = CR then begin
Memo1.Lines.Add('');
{$IFNDEF Linux}
SendMessage(Memo1.Handle, WM_KEYDOWN, VK_UP, 1);
{$ENDIF}
end;
Start := Stop + 1;
if Start > Length(Buffer) then
Break;
if Buffer[Start] = LF then
Start := Start + 1;
Stop := Start;
while (Buffer[Stop] <> CR) and (Stop <= Length(Buffer)) do
Stop := Stop + 1;
end;


end;


end.
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Wed Sep 06, 2006 9:46 pm    Post subject: Re: Telnet Indy + Delphi, how to send commands via example Reply with quote



"Eric" <erickpn21 (AT) zonnet (DOT) nl> wrote in message
news:44fe9ff5$1 (AT) newsgroups (DOT) borland.com...

Quote:
When I logon to my router (10.0.0.138) I am asked for my
username and password. After this, I can give all kinds of
commands especially for this router.

Unless the router specifically has a Telnet interface to it, then you
probably have to use HTTP for that instead. Most routers provide an
HTTP-based interface so that users can use web browsers to configure the
router.

Quote:
I would like to have a button (later on via radiobuttons and a
submit button) that sends a few commands to the router, so
not via the command line... How to do this?

You already know the answer to that, because the example you showed is doing
exactly what you are asking for. Look at the OnKeyPress event handler for
the TEdit. It is already sending commands to the server.


Gambit
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.