Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2009-03

how to communicate with a ssh server

3 messages · 1 person · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dave Coventry

I need some advice on how to communicate with a ssh server.

I thought that I would take a short cut and use plink to do the donkey work, just using TProcess to communicate with Plink, and this has worked, up to a point.

However, it is largely unreliable.

Installing LNet has failed with the error message which says: Error: Identifier not found "__front_type__"
(v0.9.27 r18951 i386-win32-win32/win64, fpc version 2.2.5 [2009/03/11]). Similarly Indy has difficulty locating the project files, ICS doesn't support ssh and Synapse doesn't connect. (Synapse also needs DLLs)

I am writing to Plink like this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TForm1.SendData(sendstring: string);
begin
  plink.input.write(sendstring[1],length(sendstring));
end;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
And I am reading from Plink like this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function TForm1.readplink: string;
var Buffer: string;
  BytesAvailable:DWord;
  bufsize,BytesRead: integer;
  pC: PChar;
begin
  BytesAvailable := PLink.Output.NumBytesAvailable;
  BytesRead:=0;
  Result:='';
//Sleep(100)<-- If not commented out, some output is retrieved
  while BytesAvailable>0 do
  begin
    SetLength(Buffer, BytesAvailable);
    BytesRead := PLink.OutPut.Read(Buffer[1], BytesAvailable);
    Result:=Result+Copy(Buffer,1,BytesRead);
    BytesAvailable := PLink.Output.NumBytesAvailable;
    NoMoreOutput := false;
  end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I can connect okay, but if I send Plink an instruction:

SendData('cd /opt/myapp'+#10);
But when try to retrieve the output:
  repeat
    NoMoreOutput := true;
    plinkData:=plinkData+readplink;
  until noMoreOutput;
Followed by:
SendData('ls -la'+#10);
and, again, retrieving the output:
  repeat
    NoMoreOutput := true;
    plinkData:=plinkData+readplink;
  until noMoreOutput;

No output is retrieved from PLink.

However, if I put a sleep(100) at the beginning of the readplink function, then I get some output (but, interestingly, the output from the first command only appears with the output of the second command). Also, using the debugger produces another set of results entirely.

This is all far too unpredictable to be of any use to me.

Can anyone give me any advice on this?

Reply by email

Dave Coventry
to Dave Coventry

Funny how after one goes into a really longwinded explanation of what one is trying to do and how it is not working that the answer presents itself.

I have set up a separate thread to keep polling the PLink process and, sure enough, eventually it all came out....

Sorry to have wasted anyone's time on this!

Although, having said that, I would much rather solve the problem without having to go to an external app like Plink, so if anyone has any suggestions.....

Thanks again.

Reply by email

Dave Coventry
to Dave Coventry

As you were. :(

The Plink output IS coming through, but whole sections of it are missing. Presumably when the thread is processing the information it cannot continue to accept the output from the pipe and that output is 'lost'. This explanation is not really consistent with some of the earlier output still being available, but it's the best I can think of.

Does anyone have any solutions?

Reply by email