Sign in Join

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

Mailing lists / lazarus / 2009-03

TProcess 2-way conversation

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Dave Coventry

Hi,

I'm writing a quick an dirty app to automate some commands which would normally be issued by a terminal something like PuTTy.

To do this I'm using TProcess to call plink.exe and then trying to ssh into the remote box and issue commands.

In short I want to log into the remote box and execute commands 'cd /opt/someappdir' and './someapp', taking the input through one memo and using another memo to issue the commands.

Can anyone point me in the right direction? I can open the terminal:

  PLink.CommandLine := 'plink.exe -ssh -pw
'+options.Values['Password']+' -batch
'+options.Values['User']+'@'+options.Values['Server'];
but sending subsequent calls fail:
  PLink.CommandLine := 'cd /opt/someappdir/ta';

Error message: 'Failed to execute cd /opt/someappdir/ta'

If I omit the subsequent calls it works roughly as expected:

  Plink.Options := [poUsePipes];
  Memo1.Append('-- executing --');
  Screen.Cursor:=crHourGlass;
  PLink.Execute;
  M.SetSize(BytesRead + READ_BYTES);
  n := PLink.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
  if n > 0
  then begin
    S.LoadFromStream(M);
    Memo1.Append(S.Text)
  end;
  S.Free;
  M.Free;

and I get the following in memo1:

-- executing --
Last login: Fri Mar 13 02:49:21 2009 from 192.168.37.43

Y?J??9?a?f ??/F?#2??/??Y.KDPx?R?`?8?\.ow?z%

Can anybody suggest something here? Or point me in a better direction.

Reply by email

Burkhard Carstens
to Dave Coventry
Dave Coventry wrote 21 lines
Hi, I'm writing a quick an dirty app to automate some commands which would normally be issued by a terminal something like PuTTy. To do this I'm using TProcess to call plink.exe and then trying to ssh into the remote box and issue commands. In short I want to log into the remote box and execute commands 'cd /opt/someappdir' and './someapp', taking the input through one memo and using another memo to issue the commands. Can anyone point me in the right direction? I can open the terminal: PLink.CommandLine := 'plink.exe -ssh -pw '+options.Values['Password']+' -batch '+options.Values['User']+'@'+options.Values['Server']; but sending subsequent calls fail: PLink.CommandLine := 'cd /opt/someappdir/ta'; Error message: 'Failed to execute cd /opt/someappdir/ta'

Your process is plink.exe. any further communication should go through plink.input and plink.output, e.g. (not tested):
...
plink.execute;
{ read welcome message from plink.output }
remoteCommand:='cd /opt/someappdir/ta'#10;
plink.input.write(remoteCommand[1],length(remoteCommand)); { read (echo and?) result of previous command}

regards
Burkhard

Reply by email

Vincent Snijders
to Dave Coventry: I'm writing a quick an dirty app to automate some commands which would normally be issued…
Dave Coventry wrote 13 lines
Hi, I'm writing a quick an dirty app to automate some commands which would normally be issued by a terminal something like PuTTy. To do this I'm using TProcess to call plink.exe and then trying to ssh into the remote box and issue commands. In short I want to log into the remote box and execute commands 'cd /opt/someappdir' and './someapp', taking the input through one memo and using another memo to issue the commands. Can anyone point me in the right direction? I can open the terminal:

See the process example:

http://wiki.lazarus.freepascal.org/Executing_External_Programs#Using_input_and_output_of_a_TProcess and
https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/examples/process/

Vincent

Reply by email

Dave Coventry
to Vincent Snijders: See the process example: http://wiki.lazarus.freepascal.org/Executing_External_Programs#Us…

Hi Vincent,

2009/3/13 Vincent Snijders :

Show quoted text 7 lines

That was the article on which I based my code.

I'm not having much luck.

  PLink.CommandLine := plink.exe -ssh -pw
'+options.Values['Password']+' -batch
'+options.Values['User']+'@'+options.Values['Server'];
  Plink.Options := [poUsePipes];
  Memo1.Append('-- executing --');
  PLink.Execute;
  M.SetSize(BytesRead + READ_BYTES);
  n := PLink.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
  if n > 0 then
  begin
    S.LoadFromStream(M);
    for i:=0 to S.Count-1 do
    begin
      Memo1.Lines.Append(S[i]);
    end;
  end
  else
  begin
    Memo1.Lines.Append('Unable to open '+options.Values['Server']);
  end;
  remoteCommand:='ls -l'+#10;
  plink.input.write(remoteCommand[1],length(remoteCommand));
  M.SetSize(BytesRead + READ_BYTES);
  n := PLink.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
  if n > 0 then
  begin
    S.LoadFromStream(M);
    for i:=0 to S.Count-1 do
    begin
      Memo1.Lines.Append(S[i]);
    end;
  end;

I would expect the output of 'ls -l' to give me quite a lot of information but S.LoadFromStream(M); doesn't contain anything at all.

Reply by email