title: use ruby Net::SSH to read a remote file via sudo
body: <p>I have to read the contents of a remote file I have permissions to (sudo) read with
  cat,less or tail.</p>
  
  <p>I am going to be doing this in Ruby so I assume I should be using Net::SSH to do it. </p>
  
  <p>The file is a log file so it can be quite big. </p>
  
  <p>This is the code I am trying now:</p>
  
  <pre><code>require 'rubygems'
  require 'net/ssh'
  
  cmd = "sudo cat /var/logs/httpd/ACCESS_log.2012.03.23"
  
  Net::SSH.start( "SERVER" , "USER", :password =&gt; "PASSWORD") do |ssh|
    ssh.open_channel do |channel|
      channel.request_pty
       channel.exec(cmd);
  
       channel.on_close do
         puts "shell terminated"
       end
      channel.on_eof do |ch|
        puts "remote end is done sending data"
      end
      channel.on_extended_data do |ch, type, data|
        puts "got stderr: #{data.inspect}"
      end
      channel.on_data do |channel, data|
        if data =~ /^\[sudo\] password for USER:/
          puts "data works"
          channel.send_data 'PASSWORD'
        end
       channel.on_data do |ch,data|
          puts "in third"
          puts data.inspect
       end
      end
     channel.on_process do |ch|
       puts "in process"
     end
    ssh.loop
    end
  end
  </code></pre>
  
  <p>When I run that I get the following output:</p>
  
  <p>in process
  in process
  in process
  data works
  in process
  in process
  in process
  in third
  "\r\n"
  remote end is done sending data
  shell terminated</p>
  
  <p>The log actually currently has a few thousand lines of data in it, because I can read it from the actual server using putty. </p>
  
  <p>How do I get that out from channel.on_data ?</p>
  
  <p>Thanks</p>