Unit1.pas:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls, Vcl.ComCtrls, Unit2;
type
TForm1 = class(TForm)
Edit1: TEdit;
Memo1: TRichEdit;
ButtonSend: TButton;
procedure WmAfterShow(var Msg: TMessage); message WM_User;
procedure FormShow(Sender: TObject);
procedure ButtonSendClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure Memo1Change(Sender: TObject);
private
Telnet: TTelnet;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Position := poScreenCenter;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
OnShow := nil;
PostMessage(Handle, WM_User, 0, 0);
end;
procedure TForm1.WmAfterShow(var Msg: TMessage);
begin
Telnet := TTelnet.Create(Self);
with Telnet do
begin
ConsoleText := Memo1.Lines;
Host := '192.168.100'; //需改为实际ip或计算机名
Port := 23; //需改为实际端口
Connect;
if not Connected then
ShowMessage('Not Connected.')
else if not Login('administrator', 'passwordxx') then //需改为实际用户和密码
ShowMessage('Login Failed.');
end;
end;
procedure TForm1.Memo1Change(Sender: TObject);
begin
Memo1.SetFocus;
Memo1.SelStart := Length(Memo1.Lines.Text);
SendMessage(Memo1.Handle, WM_VSCROLL, SB_BOTTOM, 0);
Application.ProcessMessages;
Edit1.SetFocus;
end;
procedure TForm1.ButtonSendClick(Sender: TObject);
begin
ButtonSend.Enabled := False;
Edit1.Enabled := False;
Telnet.SendCmd(Edit1.Text);
//Edit1.Text := '';
ButtonSend.Enabled := True;
Edit1.Enabled := True;
Application.ProcessMessages;
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if Key = #13 then
begin
Key := #0;
ButtonSendClick(nil);
end;
end;
end.
Unit1.dfm:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 508
ClientWidth = 663
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnShow = FormShow
DesignSize = (
663
508)
PixelsPerInch = 96
TextHeight = 13
object Memo1: TRichEdit
Left = 8
Top = 8
Width = 647
Height = 451
Anchors = [akLeft, akTop, akRight, akBottom]
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
ParentFont = False
ScrollBars = ssVertical
TabOrder = 0
Zoom = 100
OnChange = Memo1Change
end
object Edit1: TEdit
Left = 176
Top = 473
Width = 193
Height = 21
Anchors = [akLeft, akBottom]
TabOrder = 1
Text = 'dir'
OnKeyPress = Edit1KeyPress
end
object ButtonSend: TButton
Left = 384
Top = 471
Width = 75
Height = 25
Anchors = [akLeft, akBottom]
Caption = #25191#34892
TabOrder = 2
OnClick = ButtonSendClick
end
end
Unit2.pas:
unit Unit2;
interface
uses Vcl.Forms, System.SyncObjs, System.Classes, System.SysUtils,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdTelnet,
IdGlobal;
type
TTelnet = class(TIdTelnet)
private
LastStr: String;
function WaitFor(S: String; TimeOut: Integer = 10000): Boolean; overload;
function WaitFor(C: Char ; TimeOut: Integer = 10000): Boolean; overload;
procedure TelnetDataAvailable(Sender: TIdTelnet; const Buffer: TIdBytes);
public
ConsoleText: TStrings;
function Login(aUser, aPassword: String): Boolean;
function SendCmd(const S: String): Boolean;
constructor Create(AOwner: TComponent = nil);
end;
implementation
const
CRLF = #13#10;
constructor TTelnet.Create(AOwner: TComponent);
begin
inherited;
OnDataAvailable := TelnetDataAvailable;
ThreadedEvent := False;
LastStr := '';
end;
function TTelnet.SendCmd(const S: String): Boolean;
begin
SendString(S + CRLF);
Result := WaitFor('>');
end;
function TTelnet.Login(aUser, aPassword: String): Boolean;
begin
Result := False;
if not WaitFor('login: ', 15000) then Exit;
SendString(aUser + CRLF);
if not WaitFor('password: ') then Exit;
SendString(aPassword + CRLF);
if not WaitFor('>') then Exit;
Result := True;
end;
function TTelnet.WaitFor(S: String; TimeOut: Integer): Boolean;
var
I, Count: Integer;
begin
Result := False;
Count := TimeOut div 25;
for I := 1 to Count do
begin
if LastStr = S then
begin
Result := True;
Exit;
end;
Sleep(25);
Application.ProcessMessages;
end;
end;
function TTelnet.WaitFor(C: Char; TimeOut: Integer): Boolean;
var
I, Count: Integer;
begin
Result := False;
Count := TimeOut div 25;
for I := 1 to Count do
begin
if LastStr[LastStr.Length] = C then
begin
Result := True;
Exit;
end;
Sleep(25);
Application.ProcessMessages;
end;
end;
procedure TTelnet.TelnetDataAvailable(Sender: TIdTelnet; const Buffer: TIdBytes);
var
S: String;
Count, I, P: Integer;
begin
S := TEncoding.ANSI.GetString(Buffer);
Count := ConsoleText.Count;
if Count = 0 then
ConsoleText.Add(S)
else
ConsoleText[Count-1] := ConsoleText[Count-1] + S;
P := 0;
for I := S.Length Downto 1 do
begin
if S[I] = #13 then
begin
P := I;
if (P < S.Length) and (S[P+1] = #10) then Inc(P);
Break;
end;
end;
if P > 0 then
begin
LastStr := Copy(S, P+1, S.Length);
end
else
begin
LastStr := LastStr + S;
end;
end;
end.