[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

newbie: how to script windows commands with ruby?

Jason Mayer

11/18/2006 4:50:00 PM

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!

6 Answers

Ken Allen

11/18/2006 5:52:00 PM

0

`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 Mayer wrote:
> 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!
>
>


Paul Lutus

11/18/2006 6:09:00 PM

0

Jason Mayer wrote:

> 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`)?

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.

--
Paul Lutus
http://www.ara...

Parragh Szabolcs

11/18/2006 6:53:00 PM

0

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 Szabolcs
e-mail: parragh@dayka.hu
web: parszab.nir.hu


Paul Lutus

11/18/2006 7:24:00 PM

0

Parragh Szabolcs 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 name@domain.com`

Example 2:

`echo -e "From: me@mydomain.com\nSubject: This is a test\nHere is my message
text." | /usr/sbin/sendmail recipient@server.com`

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 Lutus
http://www.ara...

Parragh Szabolcs

11/18/2006 9:58:00 PM

0

Paul Lutus í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 Szabolcs
e-mail: parragh@dayka.hu
web: parszab.nir.hu


Paul Lutus

11/19/2006 12:04:00 AM

0

Parragh Szabolcs wrote:

> Paul Lutus í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.

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.

--
Paul Lutus
http://www.ara...