Board index » delphi » Re: TelNet client demo needed

Re: TelNet client demo needed


2007-05-09 03:07:17 AM
delphi225
theo <XXXX@XXXXX.COM>writes:
Quote
I'm still convinced that Synapse is better suited
for this kind of job.
It's a simple write-read-write-read.. scenario.
So is TIdTCPClient (and most other components) in Indy. TIdTelnet is a special case, as it is a multi-threaded component designed to mimic what the native Telnet app in Windows does - send every character as it is being typed in, and then receive inbound data asynchronously and display it as it arrives, regardless of what the user is doing. TIdTelnet was not designed for the write-read-write-read scenerio that AntonE is trying to implement. The rest of Indy is, though.
Gambit
 
 

Re: TelNet client demo needed

You should probably add some terminator #13#10 or whatever is expected
in Send('')
Here's an example from the Synapse Mailing List, maybe you can get some
idea of it:
var
telnet: TTelnetsend;
begin
memo1.Lines.Clear;
telnet:= TTelnetsend.Create;
try
telnet.TargetHost:='127.0.0.1';
telnet.Login;
telnet.WaitFor('name:');
telnet.Send('root'+#$0d+#$0a);
telnet.WaitFor('word:');
telnet.Send('mysecretpassword'+#$0d+#$0a);
telnet.WaitFor('>');
Memo1.Lines.Text:=telnet.sessionlog;
finally
telnet.Free;
end;
end;
 

Re: TelNet client demo needed

Ha, you were faster! ;-)
But I had to lookup an example...
 

Re: TelNet client demo needed

Quote

So is TIdTCPClient (and most other components) in Indy. TIdTelnet is a special case, as it is a multi-threaded component designed to mimic what the native Telnet app in Windows does - send every character as it is being typed in, and then receive inbound data asynchronously and display it as it arrives, regardless of what the user is doing. TIdTelnet was not designed for the write-read-write-read scenerio that AntonE is trying to implement. The rest of Indy is, though.


Gambit
Yes I know. I should have said Synapse's TTelnetSend is better suited.
Lukas is calling it "telnet script client", that is what I meant.
 

Re: TelNet client demo needed

Quote
TIdTelnet was not designed for the write-read-write-read scenerio that
>AntonE is trying to implement. The rest of Indy is, though.
Ahh. Thanks for that little gem... ;-)
 

Re: TelNet client demo needed

Thanks a lot guys. I finally got it mostly working. After stripping #13#0
from the returns, the output looks beautiful. I suppose the #13#0 intends to
overwrite the existing line...?
I any case, that is the least of my problems. Now at least I can START with
the code I really needed to write.
Thanks again for your patience & help.
By the way Remy, I am not giving up on Indy, I'd like to learn the
TCPClient ins and outs eventually.
I included the code for future reference as well as the output below. (Make
message full screen with no word-wrap)
procedure TForm1.BitBtn1Click(Sender: TObject);
var TN : TTelnetSend;
procedure Rd;
var S : String;
procedure strip0;
var I : Integer;
begin
i:=1;
while i<=Length(S) do
begin
if (S[i]=#0)and (S[i-1]=#13)
then System.Delete(S,i-1,2)
else Inc(i);
end;
end;
begin
S:=TN.RecvString;
while S<>'' do
begin
strip0;
Memo1.Lines.Add(S);
S:=TN.RecvString;
end;
end;
begin
TN:=TTelnetSend.Create;
TN.TargetHost:='10.1.12.11';
TN.TargetPort:='23';
TN.Timeout:=1000;
TN.TermType:='dumb';
Memo1.Lines.Clear;
TN.Login;
RD;
TN.Send('admin'#13#10);
RD;
TN.Send('blah'#13#10);
RD;
TN.Send('sys'#13#10);
RD;
TN.Send('pack'#13#10);
RD;
TN.Send('pr'#13#10);
RD;
TN.Logout;
TN.Free;
Memo1.Lines.Add('Done');
end;
MikroTik v2.9.38
Login: Password:
MMM MMM KKK TTTTTTTTTTT KKK
MMMM MMMM KKK TTTTTTTTTTT KKK
MMM MMMM MMM III KKK KKK RRRRRR OOOOOO TTT III KKK KKK
MMM MM MMM III KKKKK RRR RRR OOO OOO TTT III KKKKK
MMM MMM III KKK KKK RRRRRR OOO OOO TTT III KKK KKK
MMM MMM III KKK KKK RRR RRR OOOOOO TTT III KKK KKK
MikroTik RouterOS 2.9.38 (c) 1999-2006 www.mikrotik.com/
Terminal dumb detected, using single line input mode
[admin@ORAM]>
[admin@ORAM]>/system[admin@ORAM]>/system
[admin@ORAM] system>
[admin@ORAM] system>pack[admin@ORAM] system>pack
[admin@ORAM] system package>
[admin@ORAM] system package>pr[admin@ORAM] system package>pr
Flags: X - disabled
# NAME VERSION SCHEDULED
0 routeros-rb500 2.9.38
1 system 2.9.38
2 wireless 2.9.38
3 ntp 2.9.38
4 X rstp-bridge-test 2.9.38
5 routerboard 2.9.38
6 X wireless-legacy 2.9.38
7 X webproxy-test 2.9.38
8 X hotspot 2.9.38
9 X ppp 2.9.38
10 X routing 2.9.38
11 X routing-test 2.9.38
12 security 2.9.38
13 advanced-tools 2.9.38
14 dhcp 2.9.38
Done
 

Re: TelNet client demo needed

Congratulations! ;-)
Quote
Thanks a lot guys. I finally got it mostly working. After stripping #13#0
from the returns, the output looks beautiful.
You could experiment with RecvTerminated instead of RecvString.
RecvString reads Data from the Buffer up to the next occurence of #13#10
afair.
With RecvTerminated you can specify your own terminator sequence.
Something like
While TN.Sock.WaitingData>0 do
S:=TN.RecvTerminated('#13#0'); //or whatever sequence is sent
Just an Idea. Not sure if it works or helps.
 

Re: TelNet client demo needed

Quote
You could experiment with RecvTerminated instead of RecvString.
I would prefer to just read everything that is available and parse/sort it
out myself. There isn't maybe a way to just get whatever is waiting? That's
why I just loop through until I get nothing but it makes it slower because
it must wait for a timeout after every command. The router's responses are
not 100% predictable, as the routers may display additional info sometimes
after a command so I must clear the 'read buffer'.
My app must scan through 50+ routers so it takes a while and as we replace
older equipment with the new mikrotik routers, it will grow to about 150+ in
the next year and keep growing, so I would like it to be as fast as possible
although automating it already saves us LOTS of time!
Thanks a lot for your help. It was great. This will open up a new world for
us i.t.o management of hardware. There are many, many uses that we can now
start to implement, e.g. scanning all wireless routers to see if a client's
signal strength is lower than usual to warn of maybe equipment water damage,
distributing of firewall rules, distributed software upgrades, etc. etc.
etc.
Oh behave! ;-)
 

Re: TelNet client demo needed

"AntonE" <XXXX@XXXXX.COM>writes
Quote
I suppose the #13#0 intends to overwrite the existing line...?
I suggest you read the Telnet specification for details:
www.ietf.org/rfc/rfc0854.txt
In particular, the section titled "THE NVT PRINTER AND KEYBOARD"
describes what #13#0 actually represents.
Gambit
 

Re: TelNet client demo needed

Quote

I would prefer to just read everything that is available and parse/sort it
out myself. There isn't maybe a way to just get whatever is waiting?
With TN.Sock you can do whatever you want with the underlying TBlockSocket.
Try for example:
TN.Sock.RecvPacket(..
See the source code it is well documented.