Getting Ruby to output a windows command to text (log) file.

Discussion in 'Ruby' started by Tony Mcneil, Sep 17, 2008.

  1. Tony Mcneil

    Tony Mcneil Guest

    Hey folks, I'm trying to create a program to ping one of my company's
    computers every minute to measure latency, and output the results to a
    text file. I've been able to figure out / cobble together most of what
    I need (see attached ping.rb file) but the problem i'm having is I
    cannot figure out how to get Ruby to copy the results from the ping to
    the text file that is created.

    What I get currently when I run the program (from the log file):
    ________________________________
    Wed Sep 17 13:52:45 -0500 2008
    System Ping Monitor:

    Wed Sep 17 13:52:45 -0500 2008
    ________________________________

    The "ping" command doesn't output the way I need it to, HALP!!

    Attachments:
    http://www.ruby-forum.com/attachment/2708/ping.rb

    --
    Posted via http://www.ruby-forum.com/.
    Tony Mcneil, Sep 17, 2008
    #1
    1. Advertising

  2. See http://www.ruby-doc.org/core/classes/Kernel.html#M006001 :

    `cmd` =3D> string

    Returns the standard output of running cmd in a subshell. The built-in
    syntax %x{=85} uses this method. Sets $? to the process status.

    `date` #=3D> "Wed Apr 9 08:56:30 CDT 2003\n"
    `ls testdir`.split[1] #=3D> "main.rb"
    `echo oops && exit 99` #=3D> "oops\n"
    $?.exitstatus #=3D> 99

    gegroet,
    Erik V.
    Erik Veenstra, Sep 17, 2008
    #2
    1. Advertising

  3. Tony Mcneil

    Ilan Berci Guest

    If you need to also get at stderr .. take a look at popen3

    ilan


    Tony Mcneil wrote:

    > ________________________________
    > Wed Sep 17 13:52:45 -0500 2008
    > System Ping Monitor:
    >
    > Wed Sep 17 13:52:45 -0500 2008
    > ________________________________
    >
    > The "ping" command doesn't output the way I need it to, HALP!!


    --
    Posted via http://www.ruby-forum.com/.
    Ilan Berci, Sep 17, 2008
    #3
  4. Tony Mcneil

    Tony Mcneil Guest

    Thanks much Erik! I still need to study that link / see if I can figure
    out exactly what that means, but that should work perfectly, thanks
    again!
    --
    Posted via http://www.ruby-forum.com/.
    Tony Mcneil, Sep 17, 2008
    #4
  5. Tony Mcneil

    Tony Mcneil Guest

    Erik Veenstra wrote:
    > See http://www.ruby-doc.org/core/classes/Kernel.html#M006001 :
    >
    > `cmd` => string
    >
    > Returns the standard output of running cmd in a subshell. The built-in
    > syntax %x{�} uses this method. Sets $? to the process status.
    >
    > `date` #=> "Wed Apr 9 08:56:30 CDT 2003\n"
    > `ls testdir`.split[1] #=> "main.rb"
    > `echo oops && exit 99` #=> "oops\n"
    > $?.exitstatus #=> 99
    >
    > gegroet,
    > Erik V.


    hrm, I've tried looking over the documentation in the link you
    provided, but I'm having difficulty understanding it. Could you provide
    an example of the code?

    thanks again,
    ~Tony
    --
    Posted via http://www.ruby-forum.com/.
    Tony Mcneil, Sep 17, 2008
    #5
  6. Tony Mcneil wrote:
    > Erik Veenstra wrote:
    >> See http://www.ruby-doc.org/core/classes/Kernel.html#M006001 :
    >>
    >> `cmd` => string
    >>

    >
    > hrm, I've tried looking over the documentation in the link you
    > provided, but I'm having difficulty understanding it. Could you provide
    > an example of the code?
    >
    > thanks again,
    > ~Tony


    This won't work (even when you remove the typo .to_S):
    f.puts system('ping 192.168.1.74').to_S

    The ping will be executed, but you are logging if it succeeded, not the
    output.
    Erik is pointing to a working solution:

    f.puts `ping 192.168.1.74`

    Note these `` are backticks, not single quotes.

    hth,

    Siep

    --
    Posted via http://www.ruby-forum.com/.
    Siep Korteling, Sep 17, 2008
    #6
  7. Tony Mcneil

    Tony Mcneil Guest

    > This won't work (even when you remove the typo .to_S):
    > f.puts system('ping 192.168.1.74').to_S
    >
    > The ping will be executed, but you are logging if it succeeded, not the
    > output.
    > Erik is pointing to a working solution:
    >
    > f.puts `ping 192.168.1.74`
    >
    > Note these `` are backticks, not single quotes.
    >
    > hth,
    >
    > Siep



    Thank you! that was the bit that made the difference (backtick instead
    of quote) The .to_S was more of an experiment that I forgot to pull
    immediately :)

    thanks for all your help everyone!
    --
    Posted via http://www.ruby-forum.com/.
    Tony Mcneil, Sep 18, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amratash
    Replies:
    0
    Views:
    495
    Amratash
    Apr 13, 2004
  2. Mohit Mehral
    Replies:
    3
    Views:
    1,222
    Nigel Wade
    Mar 24, 2006
  3. Ernesto
    Replies:
    1
    Views:
    757
    Ernesto
    Feb 1, 2006
  4. unomystEz
    Replies:
    0
    Views:
    525
    unomystEz
    Nov 19, 2006
  5. Jack
    Replies:
    2
    Views:
    157
    Bullschmidt
    Mar 7, 2005
Loading...

Share This Page