Problem with array variable

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?

/Fox

On Tue, Apr 23, 2013 at 7:41 AM, fox foxmaster [email protected]
wrote:

until i == 2
howmany = output.count
end

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

On Tue, Apr 23, 2013 at 11:06 AM, fox foxmaster
[email protected]wrote:

so beautiful simple.
I have modified it a bit to see what values I have in the variables, but
the output is abit strange:
If1.cap
#IO:0x2845d70
If2.cap
#IO:0x28456f8

I store output in a array, so I wounder is a array item always
surrounded by [item]? Is it possible to show the download in another
format not IO:0x… more like the actual command?

Well, you can use

{|io| io.read}

instead of

{|io| io.each {|line|}}

but that will only give you the output from pscp. And apparently that
contains [] because it’s an error message. You probably want to look at
the other variants of popen to properly evaluate the result code of the
external command and react appropriately.
http://rdoc.info/stdlib/open3/1.9.3/Open3#popen3-class_method

Cheers

robert

That was a sweet bit of code, I have scripted some in bash and thats
maybe why the looks like it does :slight_smile: .

But I have applied the “ruby” style to the code as you suggested and it
works good for this part:
cmd.each do |c|
command1 = IO.popen([“plink.exe”, “-P”, port.to_s, “-pw”, pw,
“#{user}@#{ip}”, c]) {|io| io.each {|line|}}
end

so beautiful simple.

but when I get to the part where I want to download the files I get
stuck:
output.each do |f|
download = IO.popen([“pscp.exe”, “-P”, port.to_s, “-pw”, pw,
“#{user}@#{ip}:f”]) {|io| io.each {|line|}}
puts f
puts download
end

I have modified it a bit to see what values I have in the variables, but
the output is abit strange:
If1.cap
#IO:0x2845d70
If2.cap
#IO:0x28456f8

I store output in a array, so I wounder is a array item always
surrounded by [item]? Is it possible to show the download in another
format not IO:0x… more like the actual command?

Thanks