[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Remotely passing value on system() or %x[]

Bikas De simex

10/1/2008 3:25:00 PM

Hi,

I want to execute a remote bash script that take one string argument by
using system() or %x[]. But the value argument is not transmited
remotely.

Here my program

def trait_fic(repfic)
Find.find(repfic) do |path|
unless FileTest.directory?(path)
dirc="#{path}".split(/\//)
puts "#{dirc[3]}\n"
system('plink -pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}')
end
end
end

trait_fic('I:/toto')

I have got:

>ruby essai0.rb

PTOTO -------------------------> This is done by puts "#{dirc[3]}\n"
<---
Le batch traité est

..........................

/tmp/mma/essai.ksh[36]: syntax error at line 214 : `"Nous traitons le
batch $BATH ...n"' unexpected

Is there any way to transmit remotely this value. I also try net/ssh
with no succes.
Thanks in Advance
--
Posted via http://www.ruby-....

10 Answers

Stefano Crocco

10/1/2008 4:11:00 PM

0

Alle Wednesday 01 October 2008, Bikas De simex ha scritto:
> Hi,
>
> I want to execute a remote bash script that take one string argument by
> using system() or %x[]. But the value argument is not transmited
> remotely.
>
> Here my program
>
> def trait_fic(repfic)
> Find.find(repfic) do |path|
> unless FileTest.directory?(path)
> dirc=3D"#{path}".split(/\//)
> puts "#{dirc[3]}\n"
> system('plink -pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}')
> end
> end
> end
>
> trait_fic('I:/toto')
>
> I have got:
> >ruby essai0.rb
>
> PTOTO -------------------------> This is done by puts "#{dirc[3]}\n"
> <---
> Le batch trait=C3=A9 est
>
> ..........................
>
> /tmp/mma/essai.ksh[36]: syntax error at line 214 : `"Nous traitons le
> batch $BATH ...n"' unexpected
>
> Is there any way to transmit remotely this value. I also try net/ssh
> with no succes.
> Thanks in Advance

Are you aware that string interpolation doesn't happen in single quoted=20
strings? Since argument to system is a string enclosed in single quotes, th=
e=20
command itself is=20

'plink -pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}'

instead of=20

'plink -pw ppp xxx@yyyy /tmp/essai.ksh PTOTO'

which, I guess, is what you want. To achieve this, you need to use a double=
=20
quoted string:

system("plink -pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}")

I hope this helps

Stefano

Bikas De simex

10/2/2008 12:44:00 PM

0

Stefano Crocco wrote:
> Alle Wednesday 01 October 2008, Bikas De simex ha scritto:
>> unless FileTest.directory?(path)
>> >ruby essai0.rb
>> Is there any way to transmit remotely this value. I also try net/ssh
>> with no succes.
>> Thanks in Advance
>
> Are you aware that string interpolation doesn't happen in single quoted
> strings? Since argument to system is a string enclosed in single quotes,
> the
> command itself is
>
> 'plink -pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}'
>
> instead of
>
> 'plink -pw ppp xxx@yyyy /tmp/essai.ksh PTOTO'
>
> which, I guess, is what you want. To achieve this, you need to use a
> double
> quoted string:
>
> system("plink -pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}")
>
> I hope this helps
>
> Stefano

Ruby refused to interpolate #{dirc[3]} dispite I have tried the
following:

system("plink -pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}")
system('plink -pw ppp xxx@yyyy /tmp/essai.ksh "#{dirc[3]}"')
system("plink -pw ppp xxx@yyyy /tmp/essai.ksh '#{dirc[3]}'")
system(plink '-pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}')
system(plink "-pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}")
system('plink -pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}')

Any suggestion please !

Thanks in advance.
--
Posted via http://www.ruby-....

Stefano Crocco

10/2/2008 12:55:00 PM

0

Alle Thursday 02 October 2008, Bikas De simex ha scritto:
> Stefano Crocco wrote:
> > Alle Wednesday 01 October 2008, Bikas De simex ha scritto:
> >> unless FileTest.directory?(path)
> >>
> >> >ruby essai0.rb
> >>
> >> Is there any way to transmit remotely this value. I also try net/ssh
> >> with no succes.
> >> Thanks in Advance
> >
> > Are you aware that string interpolation doesn't happen in single quoted
> > strings? Since argument to system is a string enclosed in single quotes,
> > the
> > command itself is
> >
> > 'plink -pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}'
> >
> > instead of
> >
> > 'plink -pw ppp xxx@yyyy /tmp/essai.ksh PTOTO'
> >
> > which, I guess, is what you want. To achieve this, you need to use a
> > double
> > quoted string:
> >
> > system("plink -pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}")
> >
> > I hope this helps
> >
> > Stefano
>
> Ruby refused to interpolate #{dirc[3]} dispite I have tried the
> following:
>
> system("plink -pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}")
> system('plink -pw ppp xxx@yyyy /tmp/essai.ksh "#{dirc[3]}"')
> system("plink -pw ppp xxx@yyyy /tmp/essai.ksh '#{dirc[3]}'")
> system(plink '-pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}')
> system(plink "-pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}")
> system('plink -pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}')
>
> Any suggestion please !
>
> Thanks in advance.

The third line should work. To be sure the falut is of missing interpolation,
I'd suggest to break the call to system in two steps: first, build the command
string, then call puts. While this shouldn't affect the result, it allows you
to see which is the command you're going to execute with system. You can do
something like this:

cmd = "plink -pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}"
puts cmd
system(cmd)

What's the output from the lines above?

Stefano

Bikas De simex

10/2/2008 3:02:00 PM

0

Stefano Crocco wrote:
> Alle Thursday 02 October 2008, Bikas De simex ha scritto:
>> > Are you aware that string interpolation doesn't happen in single quoted
>> > which, I guess, is what you want. To achieve this, you need to use a
>> following:
>> Thanks in advance.
> The third line should work. To be sure the falut is of missing
> interpolation,
> I'd suggest to break the call to system in two steps: first, build the
> command
> string, then call puts. While this shouldn't affect the result, it
> allows you
> to see which is the command you're going to execute with system. You can
> do
> something like this:
>
> cmd = "plink -pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}"
> puts cmd
> system(cmd)
>
> What's the output from the lines above?
>
> Stefano

Hi,

I have got:

>ruby veilleur0.rb

PTOTO -----------> This is done by puts "#{dirc[3]}\n"<---
/tmp/mma/essai.ksh[36]: syntax error at line 214 : `"Nous traitons le
batch $BATH ...n"' unexpected
Le batch traité est

..........................

We do not have interpolation on system() since $BATH will be replace by
the value PTOTO.

Thanks a lot

--
Posted via http://www.ruby-....

Bikas De simex

10/2/2008 3:50:00 PM

0

Bikas De simex wrote:
> Stefano Crocco wrote:
>> Alle Thursday 02 October 2008, Bikas De simex ha scritto:
>>> > Are you aware that string interpolation doesn't happen in single quoted
>>> > which, I guess, is what you want. To achieve this, you need to use a
>>> following:
>>> Thanks in advance.
>> The third line should work. To be sure the falut is of missing
>> interpolation,
>> I'd suggest to break the call to system in two steps: first, build the
>> command
>> string, then call puts. While this shouldn't affect the result, it
>> allows you
>> to see which is the command you're going to execute with system. You can
>> do
>> something like this:
>>
>> cmd = "plink -pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}"
>> puts cmd
>> system(cmd)
>>
>> What's the output from the lines above?
>>
>> Stefano
>
> Hi,
>
> I have got:
>
>>ruby veilleur0.rb
>
> PTOTO -----------> This is done by puts "#{dirc[3]}\n"<---
> /tmp/mma/essai.ksh[36]: syntax error at line 214 : `"Nous traitons le
> batch $BATH ...n"' unexpected
> Le batch traité est
>
> ..........................
>
> We do not have interpolation on system() since $BATH will be replace by
> the value PTOTO.
>
> Thanks a lot

Hi,

It was my remote script that was wrong. Interpolation work.

system("plink -pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}") work ....!

Thanks again

--
Posted via http://www.ruby-....

Brian Candler

10/3/2008 7:56:00 PM

0

Bikas De simex wrote:
> system("plink -pw ppp xxx@yyyy /tmp/essai.ksh #{dirc[3]}") work ....!

Even better is:

system("/path/to/plink", "-pw", "ppp", "xxx@yyyy", "/tmp/essai.ksh",
dirc[3])

This is because the argument array is passed as-is to the child process.
There is no shell performing line splitting. This eliminates a number of
potential errors and security holes, e.g. if dirc[3] contains spaces,
quotes, or other special characters subject to interpretation by the
shell.
--
Posted via http://www.ruby-....

Naresh Ramaswamy

1/23/2009 11:13:00 AM

0

I have the similar problem for which I tried the solutions in this mail
thread.

I could not succeed with the results.
Please suggest your solution.

Here is what I tried.

File some.txt contains following two lines
basetest
send REGISTER, recv 200

**********************my script****************************
require 'rio' # get first 4 lines (as always in ruby indexing starts
at 0)

$filename = rio('some.txt').lines[0..0]
$basecase = rio('some.txt').lines[1..1]
temp = "#$basecase"
temp.gsub! /send/, ">"
temp.gsub! /recv/, "<"

#puts "#$filename"
#puts temp
#puts "#{temp}"

run = "sgen -t #$filename #{temp}"
puts run
#puts `run`
*********************end of my script**********************

************************output*****************************
sgen -t basetest
> REGISTER, < 200
***********************************************************

Observed that ruby outputs them into next line :(
Please let me know the solution for the same.

Naresh


--
Posted via http://www.ruby-....

Brian Candler

1/23/2009 11:53:00 AM

0

Naresh Ramaswamy wrote:
> Observed that ruby outputs them into next line :(
> Please let me know the solution for the same.

String#chomp

i.e.

$filename = rio('some.txt').lines[0..0].chomp
... etc
--
Posted via http://www.ruby-....

Naresh Ramaswamy

1/27/2009 8:32:00 AM

0

>
> $filename = rio('some.txt').lines[0..0].chomp
> ... etc
When I tried the above, I am getting the error as follows

rubee.rb:25: private method `chomp' called for ["basetest\n"]:Array
(NoMethodError)

:(
Naresh

--
Posted via http://www.ruby-....

Sandor Szücs

1/27/2009 11:27:00 AM

0


On 27.01.2009, at 09:32, Naresh Ramaswamy wrote:

> rubee.rb:25: private method `chomp' called for ["basetest\n"]:Array
> (NoMethodError)

As Brian already pointed it's String#chomp not Array#chomp.
String#lines gives you an Enumerable::Enumerator.
Your range index will give you an Array:
$filename =3D rio('some.txt').lines[0..0] #=3D> ["basetest\n"]

If you just want "basetest\n" then use:
$filename =3D rio('some.txt').lines[0] #=3D> "basetest\n"
or
$filename =3D rio('some.txt').lines.first #=3D> "basetest\n"

> :(


Cmon, you will get it, if you read the error message carefully.
Try your code in irb and then ask your objects with inspect()
or class() what they really are, maybe you have not the object
you thought.

Regards, Sandor Sz=FCcs
--