On Tue, Apr 23, 2013 at 7:41 AM, fox foxmaster <lists / ruby-forum.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 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/