 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
erict21 Guest
|
Posted: Sat Sep 02, 2006 3:45 pm Post subject: Telnet Indy + Delphi, how to send commands via example |
|
|
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 |
|
 |
Riki Wiki Guest
|
Posted: Wed Sep 06, 2006 7:45 am Post subject: Re: Telnet Indy + Delphi, how to send commands via example |
|
|
Hoi Eric
You need to repost your message on the Borland news server to make
everybody see it and possibly answer your message.
How to post to Delphi newsgroups:
<http://delphi.wikia.com/wiki/Delphi_Newsgroups> |
|
| Back to top |
|
 |
erict21 Guest
|
Posted: Wed Sep 06, 2006 3:17 pm Post subject: Re: Telnet Indy + Delphi, how to send commands via example |
|
|
Riki Wiki schreef:
Riki,
Thanks for your reply!
I just did...
Eric |
|
| Back to top |
|
 |
|
|
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
|
|