unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, BMDThread, DartTelnet_TLB, OleCtrls, DartVt_TLB;
type
TForm1 = class(TForm)
Button1: TButton;
Vt1: TVt;
Telnet1: TTelnet;
Telnet2: TTelnet;
Telnet3: TTelnet;
BMDThread1: TBMDThread;
BMDThread2: TBMDThread;
BMDThread3: TBMDThread;
BMDThreadGroup1: TBMDThreadGroup;
Edit1: TEdit;
procedure BMDThread1Execute(Sender: TObject; Thread: TBMDExecuteThread;
var Data: Pointer);
procedure BMDThread2Execute(Sender: TObject; Thread: TBMDExecuteThread;
var Data: Pointer);
procedure BMDThread3Execute(Sender: TObject; Thread: TBMDExecuteThread;
var Data: Pointer);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Telnet1Receive(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.BMDThread1Execute(Sender: TObject;
Thread: TBMDExecuteThread; var Data: Pointer);
var
Data, Token: OleVariant;
msg: string;
begin
if gUser = '' then exit;
try
begin
// Set Timeout 60秒连接 . Scripting operations will occur in
// blocking mode(阻塞模式). Switch back to async after done with script
Telnet1.Timeout := 60000;
// Connect to the Server
Telnet1.AutoOption := True;
Telnet1.Connect('10.0.0.141', 23, '', 0);
end
except
on E:Exception do
begin
Application.MessageBox(PChar(E.Message), 'Connect Failed', MB_ICONEXCLAMATION);
exit;
end;
end;
try
begin
// only 等待20 秒登录界面
Telnet1.Timeout := 10000;
// Search for "ogin:" 搜索login
Data := '';
Token := 'ogin:';
Telnet1.Search(Data, Token);
Vt1.Display(Data);
// Send User name
Data := 'root'#13#10;
Telnet1.Send(Data,0); //发用户名
// Search for "assword:" 搜索密码输入
Data := '';
Token := 'assword:';
Telnet1.Search(Data, Token);
Vt1.Display(Data);
// Send Password
Data := '1234'#13#10;
Telnet1.Send(Data,0); //发送密码
Data := '';
Token := '#'; //搜索命令提示符
Telnet1.Search(Data, Token);
Vt1.Display(Data);
// Send CMD
Data := 'ls -a'#13#10;
Telnet1.Send(Data,0); //发送ls命令
end
except
on E:Exception do
begin
msg := 'Scripted Login failed. A prompt was not found.'#13#10;
msg := msg + 'Check the code in ''DoScriptLogin'' and verify that you are searching ';
msg := msg + 'for the correct prompts.';
Application.MessageBox(PChar(msg), 'Prompt Not Found', MB_ICONEXCLAMATION);
end;
end;
// Switch back to async 转回非阻塞模式
Telnet1.Timeout := 0;
// 这里发送一个命令让telnet1接收长时间不断输出的数据
Data := 'CMD xxxxx'#13#10;
Telnet1.Send(Data,0); //发送
[red]//运行到这里是否已经退出thread?? 怎样waitfor一直让thread接收服务器送出的数据??(我要接收4小时才收完)[/red]
end;
//telnet接收的过程,23端口有数据时就触发,显示在VT上(相当于接收数据显示在Memo上,这个关系不大)
procedure TForm1.Telnet1Receive(Sender: TObject);
var
Data : OleVariant;
ba:array of byte;
begin
// If blocking, receive will occur in the function that is blocking
// if Telnet1.Timeout > 0 then exit;
// telnet has data ready to receive
// receive the data via a byte array
SetLength(ba,Telnet1.ReceiveBufferCount);
Data := ba;
Telnet1.Receive( Data);
Vt1.Display(Data);
end;
[purple]我不用thread做时完全正常,但用thread后在发完CMD xxxxxx后就收不到服务器回送的长数据了,是否要把Telnet1Receive的过程写在thread的excute里??
还是telnet1要在thread1excute里定义?还是不要用async(非阻塞)模式?但telnetTimeoute的时间不可能设到4小时啊!请帮忙[/purple]
[?][?]