Da Kereoz Blog

To content | To menu | To search

Thursday 29 April 2010

Multiplexing ssh connections in one tunnel

Say you run a script with multiple ssh accesses to the same machine. You don't want to store the public key of the local machine on the server for security reasons, but you don't want to type the password every time a connection is made.

Adding those lines to ~/.ssh/config will do the trick

Host *

  ControlMaster auto
  ControlPath ~/.ssh/master-%r@%h:%p

See man ssh_config (5) for more details.

Tuesday 28 April 2009

Ruby::Array

Here are a few methods to extend the Array class in Ruby. It will let you sum up Arrays, find how many level of Array are nested into each other, and divide all the elements contained in an Array and in all the nested Arrays it might contain.

  1. !ruby
 # A new method to Array to sum elements between two arrays in a new array.
 # It works recursively with arrays of arrays
 class Array
   def add(y)
       return self if y.empty?
       return y if self.empty?
       if self.deepness ==  1 and y.deepness == 1
           a_sum = self.zip(y.flatten).map{|a,b| a.to_f+b.to_f}
           return a_sum
       else
           if y0.is_a? Array and self0.is_a? Array
               res = Array.new
               y.each_index do |i|
                   res.push selfi.add yi
               end
               return res
           end
       end
   end
   
   # Return the deepness of an Array, ie how many Arrays does it 
   # recursively contains ? Note that the computation is only performed on the first item of the array.
   def deepness
       a = self.collect0
           return 1 if not a.is_a? Array
           return 1 + a.deepness if a.is_a? Array
   end
   
   # Division applied recursively on every element
   def div(n)
       a = 
       self.each do |s|
           if not s.is_a? Array
               a.push (s.to_f/n.to_f)
           else
               a.push s.div(n)
           end
       end
       return a
   end
  end

Sunday 26 April 2009

Unison or how to keep your data synchronized

Unison is an opensource file-synchronization tool for Unix and Windows written in CAML. Unlike rsync, it is able to propagate changes in both directions when synchronizing between two machines. I use it to keep my two laptops and my Desktop synchronized. Unison works both locally and remotely. SSH can be used to securely synchronize from anywhere to anywhere.

SSH options can be passed using the -sshargs option, eg:

unison folder1 ssh://machine/folder2 -sshargs '-port 2222'

Symlinks can be followed, and it makes things even easier : you can simply create a 'sync' folder in your home directory, containing symlinks to the data you want to keep in sync. I like to use it for config files such as .vimrc, .emacs.

Vim stuff

Hello world =) Let's start this blog with a short talk about vim. Even though Vim is one of the most powerful text editors ever, it is often underestimated by those who don't use it. The thing is that it takes times to get used to it. It is highly customizable, and scriptable. It even has a native Ruby support.

Some cool tips

  • I recently discovered the BufExplorer plugin. :BufExplorer show a list of buffers, allows to jump between buffers, to delete them etc.. like emac's C-x b so why not adding a key mapping like this ::nmap <C-x>b :BufExplorer <CR>
  • Substituting a string with another one in all opened buffer and save changes can be done with ":bufdo /string | :up". It can be generalized to any other operation. Very useful for core refactoring
  • The current buffer is represented by % in vim commands. For example, :ruby % will execute the current buffer as ruby code.
  • In visual mode, you can execute commands as well with ":!" and get the output of the command. It allows to evaluate code on the fly.

My vimrc is available through http://kereoz.org/.vimrc