Known Hosts Issue With Net::SSH
This issue bit me twice within a month, and each time I had to spend a couple of hours to figure out the cause of the problem, mostly due to the obscurity of the error messages.
So I was using net-ssh-2.0.15 and jruby-openssl-0.5.1 for a piece of code that’s supposed to perform some remote operations. The Net::SSH usage itself was straightforward…
Net::SSH.start(@host, @username, :keys => @keys) do |ssh|
...
end
When I ran the above snippet on Windows XP, it gave this error message without any further information…
The system cannot find the path specified
Not exactly helpful, eh? And after some sleuthing around, it came down to add(host, key) method in lib/net/ssh/known_hosts.rb :
def add(host, key)
File.open(source, "a") do |file|
blob = [Net::SSH::Buffer.from(:key, key).to_s].pack("m*").gsub(/\s/, "")
file.puts "#{host} #{key.ssh_type} #{blob}"
end
end
This method is trying to add an entry to the known_hosts file, which location is stored in source variable. In my case, the variable resolved to C:/.ssh/known_hosts . But the problem was that the .ssh directory didn’t exist. So I simply created it.
Lo and behold, when I ran this piece of code on a Solaris box weeks later, I hit another error message…
No such file or directory (IOError)
which turned out to be caused by the exact same thing, but this time the location is <script_home>/.ssh/known_hosts . The workaround was the same, I simply created the .ssh directory.
So there you go, hopefully it helps whoever else was confused by the unclear error messages.
And in terms of a long term solution, it would be nice if add(host, key) checks or even creates the .ssh directory before attempting to write known_hosts file.
Image Saving In Watir
So I got the chance to use Watir again on a short project at work. The last time I used it was about 1.5 years ago, and I was glad to find out that Watir is still a nice library to use. Watir simply works without much hassle.
The only issue I had was with image saving. From Image class documentation (Watir 1.6), it wasn’t obvious that save can only be called when image element is directly contained within a browser element.
I was trying to save the first image within a div,
$browser.div(:id, 'foobar').images[1].save('d:\\temp')
which resulted in this error
NoMethodError: undefined method `goto' for #<Watir::Div:0x3fa5d7c>
d:/dev/ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/image.rb:113:in `save'
Looking at the implementation of save in image.rb,
def save(path)
require 'watir/windowhelper'
WindowHelper.check_autoit_installed
@container.goto(src)
begin
thrd = fill_save_image_dialog(path)
@container.document.execCommand("SaveAs")
thrd.join(5)
ensure
@container.back
end
end
it shows that save relies on the existence of goto and back methods within the container of the element, meaning that calling save method will tell the browser to go to the image src value, save it, and then click the back button. Hence image must be contained directly within a browser element.
I ended up having to use XPath because the image element doesn’t have any id or class that allows me to directly reference it from a browser element.
$browser.image(:xpath, "//div[@id='foobar']/img").save("d:\\temp")
It would be nicer if @container can be replaced by something like find_root_container or something that traverses the ancestor container elements and eventually finds a browser element.
Another nice improvement to the save method implementation would be instead of going to the image src and then clicking back, which could potentially lose the state of a page, it would be nicer to open a new window and close it afterward.