Newbie: how to script windows commands with ruby?

Hi!

I’ve been learning ruby for a little while, but I’m having trouble
figuring out how to do this (and I must be using the wrong keywords in
my google search). I do tech support, and I’m looking to automate
some basic troubleshooting over a web interface for my coworkers and
new hires.

What I’d like to do is script the windows ping command and scrape the
results. Anyone know where I can find good references on this?

So I guess my question could be clarified as How do I script windows
command line commands via ruby (I understand that in linux I could
just use ping xxx)?

The second question would be ‘How do I scrape the output of a windows
cmd.exe command and use it in the rest of my script?’

Thanks in advance!

ping xxx will work in windows as well.
Try String#scan, String#split for scraping, as well as the rest of
String’s methods - they’re very useful.
ping www.google.com.split("\r\n") will get you an array of the lines
of output.

I can’t really say more without knowing more about what you want to
scrape out.

Ken

Jason M. wrote:

So I guess my question could be clarified as How do I script windows
command line commands via ruby (I understand that in linux I could
just use ping xxx)?

Yes, that’s right, and it works in Windows (under Ruby) the same way.
But
clearly this didn’t work for you. So, what did you try, and what was the
result?

Have you tried:

data = ping -n 1 hostname.chomp

The second question would be ‘How do I scrape the output of a windows
cmd.exe command and use it in the rest of my script?’

This should work:

data = cmd.exe /C command.chomp

Again, it would help to see what you tried and what you got.

I feel ashamed of myself, but I hadn’t actually tried using what I
thought
to be the *nux only command.

Your example worked flawlessly, thanks! (and now I get to start on my
project!)

Hi,
could someone pls help me out a bit: I’d like send out e-mails from a
script on our server, but not with smtp, rather giving them to the
postfix pickup transport (like mailx or mutt does it), calling
/sbin/sendmail or the like. The trick is, that at the time of sending it
the full mail string is ready with headers and all.

What is the easiest ruby way to do it?

Thx in advance:
Szab

Parragh S. wrote:

Hi,
could someone pls help me out a bit: I’d like send out e-mails from a
script on our server, but not with smtp, rather giving them to the
postfix pickup transport (like mailx or mutt does it), calling
/sbin/sendmail or the like. The trick is, that at the time of sending it
the full mail string is ready with headers and all.

You aren’t being very clear about what you do and don’t want, but in any
case, try this:

Example 1:

echo "Here is my message" | /usr/sbin/sendmail [email protected]

Example 2:

echo -e "From: [email protected]\nSubject: This is a test\nHere is my message text." | /usr/sbin/sendmail [email protected]

The point is you can pipe all the required information to sendmail, one
way
or another. You could also use “popen” instead of the above approach.

Paul L. írta:

You aren’t being very clear about what you do and don’t want, but in any
case, try this:

Thanks for the reply.

I would like to do the following steps:

  1. Get an e-mail from an account by pop3.
  2. Alter the headers in that mail (simple text processing).
  3. Send out that same mail to a few dozen addresses that I get form a
    database. The mail should be the one that was pop3-ed, I must not create
    a new multipart message. So after the text processing (step 2) I
    consider the mail text (= ruby string) ready for sending, no further
    processing needed.

For sending.

– I could use Net::SMTP.start… – but I don’t want to use SMTP on
localhost, I’d prefer the pickup transport.

– and I could use:

File.popen(/usr/sbin/sendmail, “w”) do
| pipe |
pipe.puts …

But thought there is a nicer way to do this… Is there?

Szab

Parragh S. wrote:

database. The mail should be the one that was pop3-ed, I must not create
a new multipart message. So after the text processing (step 2) I
consider the mail text (= ruby string) ready for sending, no further
processing needed.

Okay, I get it, you are designing a mail relay bot that receives a
message,
clones it, and sends it out in large volumes.

I wish you the best of luck.