Monday, January 9, 2012

Capture system call output in ruby...The easy way.

I scoured the web trying to find good examples of how to do this. There were quite a few suggestions, but none of them captured the output of this system command:

fping -C 5 -q www.example.com

In the end, I found a sure way to capture it:

f = IO.popen("date; fping -C 5 -q www.example.com 2>&1").readlines

when I call puts f:

["Mon Jan 9 01:29:22 PST 2012\n", "www.example.com : 56.61 58.85 59.06 56.91 72.41\n"]

Perfect - a nice array with my 2 command's output captured nice and neat in an array!!

Hope it helps.

No comments:

Post a Comment