From: Robert Klemme Date: 2013-04-23T16:14:01+09:00 Subject: Re: Problem with array variable --20cf3074d320e7c00a04db01ed7e Content-Type: text/plain; charset=ISO-8859-1 On Tue, Apr 23, 2013 at 7:41 AM, fox foxmaster wrote: > Hi all I have a problem with a small project I am working on. > I use putty tools to do the ssh stuff (net-ssh/scp) doesn't work because > of bad ssh server on the target. > > Anyway here is the code I made so far. > > cmd = ["pcapdump -cleanup", "pcapdump -start -size=500", "pcapdump > -stop", "pcapdump -write"] > #Loop throu the commands > i = 0 > until i == 2 > command1 = %x[plink.exe -P #{port} -pw #{pw} #{user}@#{ip} "#{cmd[i]}" > < yes.txt] > i += 1 > end > #This is to get the file names > command3 = %x[plink.exe -P #{port} -pw #{pw} #{user}@#{ip} "#{cmd3}"] > #Grab the file names for downloading and store them in a array > output = command3.scan(/"([^"]*)"/) > #Count the items in the array > puts output > howmany = output.count > #Fetch the file's > i = 0 > until i == howmany > file = output[i] > download = %x[pscp.exe -P #{port} -pw #{pw} #{user}@#{ip}:#{file} .] > #download = system("pscp.exe -P #{port} -pw #{pw} > #{user}@#{ip}:#{file} .#{file}") > puts file > puts download > i += 1 > end > > now when I run it I get [] surrounding variable file. > So I put in some puts to se what the variable is: > If1.cap > If2.cap > scp: [If1.cap]: No such file or directory > If1.cap > > scp: [If2.cap]: No such file or directory > If2.cap > > When the pscp command runs the [] shows but not before or after. > How can I remove them? > I do not have a solution handy but the first thing I'd do is replace %x[] with invocations of system() or IO.popen() where all arguments are passed individually. That avoids weird effects and errors from parsing all arguments out of a single string. You have them separately anyway. command1 = %x[plink.exe -P #{port} -pw #{pw} #{user}@#{ip} "#{cmd[i]}" -> command1 = IO.popen(["plink.exe", "-P", port.to_s, "-pw", pw, "#{user}@#{ip}", cmd[i]]) {|io| io.read} and since you do not use the output IO.popen(["plink.exe", "-P", port.to_s, "-pw", pw, "#{user}@#{ip}", cmd[i]]) {|io| io.each {|line|}} Also, your looping is overly complicated and not how we do it in Ruby usually. The first loop should be cmd.each do |c| IO.popen(["plink.exe", "-P", port.to_s, "-pw", pw, "#{user}@#{ip}", c]) {|io| io.each {|line|}} end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --20cf3074d320e7c00a04db01ed7e Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Tue, Apr 23, 2013 at 7:41 AM, fox foxmaster &l= t;lists@ruby-foru= m.com> wrote:
Hi all I have a problem with a small project I am working = on.
I use putty tools to do the ssh stuff (net-ssh/scp) doesn't work becaus= e
of bad ssh server on the target.

Anyway here is the code I made so far.

cmd =3D ["pcapdump -cleanup", "pcapdump -start -size=3D500&q= uot;, "pcapdump
-stop", "pcapdump -write"]
#Loop throu the commands
i =3D 0
until i =3D=3D 2
=A0 command1 =3D %x[plink.exe -P #{port} -pw #{pw} #{user}@#{ip} "#{cm= d[i]}"
< yes.txt]
=A0 i +=3D 1
end
#This is to get the file names
command3 =3D %x[plink.exe -P #{port} -pw #{pw} #{user}@#{ip} "#{cmd3}&= quot;]
#Grab the file names for downloading and store them in a array
output =3D command3.scan(/"([^"]*)"/)
#Count the items in the array
puts output
howmany =3D output.count
#Fetch the file's
i =3D 0
until i =3D=3D howmany
=A0 file =3D output[i]
=A0 download =3D %x[pscp.exe -P #{port} -pw #{pw} #{user}@#{ip}:#{file} .]<= br> =A0 #download =3D system("pscp.exe -P #{port} -pw #{pw}
#{user}@#{ip}:#{file} .#{file}")
=A0 puts file
=A0 puts download
=A0 i +=3D 1
end

now when I run it I get [] surrounding variable file.
So I put in some puts to se what the variable is:
If1.cap
If2.cap
scp: [If1.cap]: No such file or directory
If1.cap

scp: [If2.cap]: No such file or directory
If2.cap

When the pscp command runs the [] shows but not before or after.
How can I remove them?
I do not have a solution handy but the first thing I'd do is rep= lace %x[] with invocations of system() or IO.popen() where all arguments ar= e passed individually. =A0That avoids weird effects and errors from parsing= all arguments out of a single string. =A0You have them separately anyway.<= /div>

command1 = =3D %x[plink.exe -P #{port} -pw #{pw} #{user}@#{ip} "#{cmd[i]}"
->
co= mmand1 =3D IO.popen(["plink.exe", "-P", port.to_s, &quo= t;-pw", pw, "#{user}@#{ip}", cmd[i]]) {|io| io.read}
and since you do not use the output<= /div>

IO.popen(["plink.exe", "-P", port.to_s, "-pw= ", pw, "#{user}@#{ip}", cmd[i]]) {|io| io.each {|line|}}

Also, your looping is overly complicated and not how we do it in Ruby usua= lly. =A0The first loop should be

=
cmd.each do |c|
=A0 IO.popen(["plink.exe", "-P", port.to_s,= "-pw", pw, "#{user}@#{ip}", c]) {|io| io.each {|line|}= }
end
=
Kind regards

robert


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
--20cf3074d320e7c00a04db01ed7e--