Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
496 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (5)


Remote Execute Function (Unix REXEC)Format this article printer-friendly!Bookmark function is only available for registered users!
RExec()
Product:
Delphi 5.x (or higher)
Category:
Communication
Skill Level:
Scoring:
Last Update:
04/09/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet REXEC UNIX "REMOTE EXECUTE"
Times Scored:
4
Visits:
8323
Uploader: Mike Heydon
Company: EOH
Reference: mheydon@eoh.co.za
 
Question/Problem/Abstract:
This function will execute a command to a Unix box (or any TCP connection that supports REXEC - port 512) and return the display results in a file. I currently use it on HP and SUN systems.

The parameters to RExec() are

HostIP : string               // eg. '196.11.121.160'          
UserID : string               // eg. 'root'
Password : string           // eg. 'fraqu34'
Command : string          // eg. 'export TERM=vt100; dv'
ResultFilename : string   // eg. 'c:\temp\uxresult.txt'

The function returns true if sucessful, else false.

The command may contain multiple statements separrated by semi-colons. REMEMBER : REXEC does not run the user .profile, so NO user environments are set. You can export  any environment settings in this parameter.

eg. 'export TERM=vt100; export APP=baan; run_mycommand'

An example of use is ....
(change to directory /var and return a dir listing and return results in file c:]temp\ux.txt)

procedure TForm1.Button1Click(Sender: TObject);
begin
  RExec('196.11.121.162',
             'root','passwd342',
             'cd /var; ls -1',
             'c:\temp\ux.txt');

  Memo1.Lines.LoadFromFile('c:\temp\ux.txt');
end;

Answer:



uses ScktComp;

function RExec(const HostIP : string; const UserID : string;
               const Password : string; const Command : string;
               const ResultFilename : string) : boolean;
var TCP : TClientSocket;
    i : integer;
    TxOut : File;
    Buffer,Cr,Lf : byte;
    Failed : boolean;
begin
  Failed := true;   // Assume initial error state
  Cr := 13;         // Carriage Return Char
  Lf := 10;         // Line Feed Char
  TCP := TClientSocket.Create(nil);

  try
    TCP.Address := HostIP;
    TCP.ClientType := ctBlocking;
    TCP.Port := 512; // REXEC port
    TCP.Open;

    // Give time to connect
    for i := 1 to 500 do if not TCP.Active then Sleep(100) else break;

    // If TCP opened OK then send the command to host
    // and write results to specified file
    if TCP.Active then begin
       AssignFile(TxOut,ResultFileName);
       Rewrite(TxOut,1);
       TCP.Socket.SendText('0' + #0);
       TCP.Socket.SendText(UserID + #0);
       TCP.Socket.SendText(Password + #0);
       TCP.Socket.SendText(Command + #0);
       TCP.Socket.SendText(#13);
       Sleep(20);  // Give a gap to respond

       // Wait for resonse from Host
       // You may want to check for timeout here using
       // a TTimer. My complete function does this, but
       // have omitted for sake of clarity.
       while (TCP.Socket.ReceiveBuf(Buffer,1) <> 1) do
             Application.ProcessMessages;

       // Write host byte stream to file
       while TCP.Socket.ReceiveBuf(Buffer,1) = 1 do begin
          if (Buffer = 10) then begin
             BlockWrite(TxOut,Cr,1);
             BlockWrite(TxOut,Lf,1);
          end
          else
             BlockWrite(TxOut,Buffer,1);
       end;

       TCP.Close;
       CloseFile(TxOut);
       Failed := false;
    end;
  finally
    TCP.Free;
  end;

  Result := not Failed;
end;






Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Please help
    Vikas Nagpal (Aug 1 2006 2:08PM)

I have tried running this program to run the script on server and download the resultant file, but it gives error:

rexecd: Couldn't look up address for your host

Any comments.

Regards
Respond

RE: Please help
Mike Heydon (Aug 1 2006 2:40PM)

Use the IP address as opposed to hostname eg. 10.0.1.154
Respond

RE: RE: Please help
Vikas Nagpal (Aug 1 2006 2:46PM)

Hello Mike,

I have used the IP address only. This program runs fine on my colleagues computer. But gives error to me. Do I need to do some setting on my laptop.

Regards
Respond

RE: RE: RE: Please help
Mike Heydon (Aug 1 2006 2:57PM)

Not sure what your problem is.
Do you have permission to REXEC on the target host?
Check the /.rhosts and /etc/hosts.equiv to see if the server is authenticating rexec and rlogin.
Check your C:\WINNT\SYSTEM32\DRIVERS\ETC\HOSTS file to see if you have not got some funny entries in it.
Is your network/gateway/dns settings the same as your collegue?
Respond

RE: RE: RE: RE: Please help
Vikas Nagpal (Aug 1 2006 3:18PM)

We have resolved the error. The settings were wrong for my computer on the server.

Thanks and regards
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
Peganza
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)