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

B

Bikas De simex

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
 
S

Stefano Crocco

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
 
B

Bikas De simex

Stefano said:
Alle Wednesday 01 October 2008, Bikas De simex ha scritto:
unless FileTest.directory?(path)
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.
 
S

Stefano Crocco

Alle Thursday 02 October 2008, Bikas De simex ha scritto:
Stefano said:
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
 
B

Bikas De simex

Stefano said:
Alle Thursday 02 October 2008, Bikas De simex ha scritto:
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
 
B

Bikas De simex

Bikas said:
Stefano said:
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
 
B

Brian Candler

Bikas said:
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.
 
N

Naresh Ramaswamy

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
 
B

Brian Candler

Naresh said:
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
 
N

Naresh Ramaswamy

$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
 
S

Sandor Szücs

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top