maslas has asked for the wisdom of the Perl Monks concerning the following question:

Hello Everyone, I have searched through several sites and dozens of books to no avail. My script is required to execute FTP commands. Some years ago I wrote it to create an FTP shell script and execute that script on a UNIX platform. Worked fine as long as the NT server I wanted to FTP to had an FTP service running. Now they want the script to be able to remotely check if the NT machine is running the service and start it if it is not. Also they want the user to be notified when the FTP commands fail. So I cannot use the Bourne shell for the FTP commands any longer. I have seen that the FTP process can be done inside the perl script, but not how to run something like a 'ps' command or 'net start' to determine if the service I want is running. I have no clue whether a perl script could cross over from UNIX to NT and run a process. Can anyone please point me in the right direction to find a simple example of this so I can gain some knowledge about how to do this? Thank you, Michael (aka maslas)

Replies are listed 'Best First'.
Re: FTP question.
by BazB (Priest) on Jan 02, 2002 at 21:13 UTC

    Net::FTP will allow you to deal with FTP sessions.

    You can't FTP to a machine that isn't running an FTP server.
    I'm not sure if it's possible to check for the status returned when FTP commands fail - I'm pretty sure it is possible, but I'd have to RTFM.

    Your question is rather unclear - are you trying to connect to a remote box and carry out an FTP transaction, or are you trying to monitor a system locally by FTP'ing to yourself and/or run a command like 'top' or 'ps'?

    Update: Thinking about this, some well placed error checking (eval statements? Don't quote me on that) should let you know if the initial connection attempt to the FTP server failled...


      Thank you,

      I am trying to connect to a remote box and carry out an FTP transaction as well as run other processes on the remote platform. The parent platform is a Solaris OS which I want to connect to an NT server to perform various commands.

      Primarily I want to FTP the files from this machine to another machine and report any errors in the transfer back to the parent.
      Regards,
      Michael

Re: I may be remotely depressed.
by dru145 (Friar) on Jan 03, 2002 at 03:15 UTC
    Here's something basic that should work. It will save the output of a ps -ef command to a file then use a regex to search if ftpd is running.
    #!/usr/bin/perl -w use strict; # See if ftp is running my $result; my $file = "/tmp/df"; my $cmd1 = "ps -ef"; # Run the ps -ef command $result = `$cmd1 >$file`; # Open the file and search if the daemon is running open (FILE, $file) or die "can't open $file: $!"; while (<FILE>){ if (/ftpd/){ print "The ftp daemon is running\n"; } }

    HTP
    -Dru
    Another satisfied monk.
Re: I may be remotely depressed.
by brassmon_k (Sexton) on Jan 02, 2002 at 23:30 UTC
    Perl can execute UNIX commands doing something that took me forever to figure out and is just really weird. I couldn't pray to tell you where to find it anymore but here is the answer. I could let you look for it forever HAHAHA! but I'm nice.
    #!/usr/bin/perl -w open I, "| ps -ef" or die; close I;
    Now remember where it says "open I"....."I" can be anything. It could be "Open PROCESS" it doesn't matter. The "close I" has to be there though or "close PROCESS". You can make that handle anything you want it to be. Then after that you can use any UNIX system command. ls, cd, ls -al, ftp, it will open anything so far as I've used it.

    HAVE FUN!
    The Brassmon_k

      brassmon_k, you are referring to opening a pipe to read process output. Unfortunately the syntax you suggest is the one for *supplying input* to the subprocess, which is not a common usage of pipes (your pipe character is the *first* character of the command).

      Far more common is to run a command producing output which the perl code must process, using the pipe character ('|') as the *last* character of the command (e.g., open PH, "shell command|".).

      Sample code follows:

      open PH, "ps -ef|" or die "$!: opening pipe"; while (<PH>) { # retrieves output from "ps" command one line at a ti +me # .. process output--each line is $_ .. } close PH or die "$!: error closing pipe";

      dmm

      You can give a man a fish and feed him for a day ...
      Or, you can
      teach him to fish and feed him for a lifetime
        What do you mean?

        I tested the little Perl snippet on my SPARC and it produces the output on STDOUT for the "ps -ef" command. I don't get what you mean for "standard input". I know the handle is used for INPUT processing via a file Handle but what I suggested still works. It's probably not syntaticallly correct but it works perfect. Perl doesn't seem to care if the pipe (|) is in front. It does send the "ps -ef" command to STDOUT though I guarantee it...try it. I also found it convienient to give it a handle because then this guy can manipulate the data any way he wants to later on. I might be misunderstanding you though.

        The Brassmon_k