# Newbie: how to script windows commands with ruby?

**URL:** <https://www.ruby-forum.com/t/newbie-how-to-script-windows-commands-with-ruby/76427>\
**Category:** Ruby\
**Created:** [November 18, 2006, 5:50pm UTC](https://www.ruby-forum.com/t/newbie-how-to-script-windows-commands-with-ruby/76427 "2006-11-18T17:50:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [November 18, 2006, 5:50pm UTC](https://www.ruby-forum.com/t/newbie-how-to-script-windows-commands-with-ruby/76427/1 "2006-11-18T17:50:56Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [November 18, 2006, 6:53pm UTC](https://www.ruby-forum.com/t/newbie-how-to-script-windows-commands-with-ruby/76427/2 "2006-11-18T18:53:06Z")

</div>

`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

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [November 18, 2006, 7:11pm UTC](https://www.ruby-forum.com/t/newbie-how-to-script-windows-commands-with-ruby/76427/3 "2006-11-18T19:11:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [November 18, 2006, 7:47pm UTC](https://www.ruby-forum.com/t/newbie-how-to-script-windows-commands-with-ruby/76427/4 "2006-11-18T19:47:14Z")

</div>

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!)

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [November 18, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/newbie-how-to-script-windows-commands-with-ruby/76427/5 "2006-11-18T19:53:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [November 18, 2006, 8:25pm UTC](https://www.ruby-forum.com/t/newbie-how-to-script-windows-commands-with-ruby/76427/6 "2006-11-18T20:25:19Z")

</div>

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 removed_email_address@domain.invalid`

Example 2:

`echo -e "From: removed_email_address@domain.invalid\nSubject: This is a test\nHere is my message text." | /usr/sbin/sendmail removed_email_address@domain.invalid`

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.

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [November 18, 2006, 10:59pm UTC](https://www.ruby-forum.com/t/newbie-how-to-script-windows-commands-with-ruby/76427/7 "2006-11-18T22:59:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [November 19, 2006, 1:07am UTC](https://www.ruby-forum.com/t/newbie-how-to-script-windows-commands-with-ruby/76427/8 "2006-11-19T01:07:44Z")

</div>

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.
