Net::SSH gem non_interactive invalid option? - ruby

I have Ruby code like so:
def make_ssh user, pass
begin
Net::SSH.start(#host, user, :password => pass, :non_interactive => true) do |session|
#ssh = session
puts #ssh
end
rescue Exception => e
puts e
end
end
In the Net::SSH library source/repo, the :non_interactive command is supposed to negate the password prompt (and I'm assuming use the key/value :password => pass for authentication), but everytime I try to run my code, I am getting an "invalid option" error.
I have tried this with and without the ssh block, and also as
Net::SSH.start(#host, user, {:password => pass, :non_interactive => true})
but nothing seems to work. What am I doing wrong here?

I got the same eror as yours. This option is added in net-ssh 2.10.0. You are probably using the lower version.

Related

Preventing an Instance Method from Printing to Console

I am writing a simple Ruby script that utilizes the mysql2 gem.
In order to properly terminate a connection with the database and avoid the Too many connections error, I store my connection into the variable mysql like so:
mysql = Mysql2::Client.new(:host => hst, :username => usr, :password => pass, :database => db, :connect_timeout => 30)
and then I close the connection:
mysql.close
When this occurs, I get:
closed MySQL connection
in the console.
How can I implement the Instance Method #close found here without closed MySQL connection showing up in the terminal?
you can do this:
def silence_stdout
$stdout = File.new( '/dev/null', 'w' )
yield
ensure
$stdout = STDOUT
end
and do the close with that method
silence_stdout{mysql.close}

EM::Synchrony.defer with fiber aware database call causes FiberError exception

I'm trying to use EM-Synchrony for concurrency in an application and have come across an issue with my use of deferred code and Fibers.
Any calls to the database within either EM.defer or EM::Synchrony.defer results in the application crashing with the error can't yield from root fiber
Below is a very trimmed down runnable example of what I'm trying to accomplish. The first print works and displays [:first, 1] but the second is where I crash with the error mentioned above.
require 'mysql2'
require 'em-synchrony/activerecord'
ActiveRecord::Base.establish_connection(
:adapter => 'em_mysql2',
:username => 'user',
:password => 'pass',
:host => 'localhost',
:database => 'app_dev',
:pool => 60
)
class User < ActiveRecord::Base; end
EM.synchrony do
p [:first, User.all.count]
EM::Synchrony.defer do
p [:second, User.all.count]
end
end
My first thought was perhaps the Fiber.current and Fiber.yield within EM::Synchrony.defer meant I could fix the problem with an extra Fiber.new call
EM::Synchrony.defer do
Fiber.new do
p [:second, User.all.count]
end.resume
end
This fails to run as well but this time I get the error fiber called across threads.

Are there any basic examples of Rack::Session::Cookie usage?

I can't find any simple examples for using Rack::Session::Cookie and would like to be able to store information in a cookie, and access it on later requests and have it expire.
These are the only examples I've been able to find:
How do I set/get session vars in a Rack app?
http://rack.rubyforge.org/doc/classes/Rack/Session/Cookie.html
Here's what I'm getting:
use Rack::Session::Cookie, :key => 'rack.session',
:domain => 'foo.com',
:path => '/',
:expire_after => 2592000,
:secret => 'change_me'
And then setting/retrieving:
env['rack.session'][:msg]="Hello Rack"
I can't find any other guides or examples for the setup of this. Can someone help?
You have already setup cookie in your question. I am not sure if you means something else by "setup".
Instead of env['rack.session'] you can use session[KEY] for simplification.
session[:key] = "vaue" # will set the value
session[:key] # will return the value
Simple Sinatra example
require 'sinatra'
set :sessions, true
get '/' do
session[:key_set] = "set"
"Hello"
end
get "/sess" do
session[:key_set]
end
Update
I believe it wasn't working for you because you had set invalid domain. So I had to strip that off :domain => 'foo.com',. BTW Sinatra wraps Rack cookie and exposes session helper. So above code worked fine for me. I believe following code should work as expected.
require 'sinatra'
use Rack::Session::Cookie, :key => 'rack.session',
:expire_after => 2592000,
:secret => 'change_me'
get '/' do
msg = params["msg"] || "not set"
env["rack.session"][:msg] = msg
"Hello"
end
get "/sess" do
request.session["msg"]
end
set session value msg access root or / defaults to 'not set' if you pass ?msg=someSTring it should set msg with new value.
access /sess to check whats in session.
You can take some cues from How do I set/get session vars in a Rack app?
Check the example below. It might give you good idea
http://chneukirchen.org/repos/rack/lib/rack/session/cookie.rb

AuthenticationFailed net-ssh ruby

When I'm trying to Net::SSH.start to my debian ssh server and transfer a files, every time I've a very strange error message - `start': Net::SSH::AuthenticationFailed, but all the authentication data are correct, I don't know what a problem is. Does anyone faced same problem?
The code was written on ruby and net/ssh module are in use, here is a code:
require 'rubygems'
require 'net/ssh'
def copy_file(session, source_path, destination_path=nil)
destination_path ||= source_path
cmd = %{cat > "#{destination_path.gsub('"', '\"')}"}
session.process.popen3(cmd) do |i, o, e|
puts "Copying #{source_path} to #{destination_path}... "
open(source_path) { |f| i.write(f.read) }
puts 'Done.'
end
end
Net::SSH.start("192.168.112.129",
:username=>'username',
:password=>'password') do |session|
copy_file(session, 'D:/test/1.txt')
copy_file(session, '/home/timur/Documents/new_file.rb"')
end
There is no :username option in net/ssh 2.6, you can set it like parameter:
Net::SSH.start('192.168.112.129', 'username', password: 'password') do |ssh|
foo
end

RSpec - Check condition at the time a method is called?

Right now I assert that a method is called:
Code:
def MyClass
def send_report
...
Net::SFTP.start(#host, #username, :password => #password) do |sftp|
...
end
...
end
end
Test:
it 'successfully sends file' do
Net::SFTP.
should_receive(:start).
with('bla.com', 'some_username', :password => 'some_password')
my_class.send_report
end
However, I also want to check that a given condition is true at the time Net::SFTP.start is called. How would I do something like this?
it 'successfully sends file' do
Net::SFTP.
should_receive(:start).
with('bla.com', 'some_username', :password => 'some_password').
and(<some condition> == true)
my_class.send_report
end
You could provide a block to should_receive, which will execute at the time the method is called:
it 'sends a file with the correct arguments' do
Net::SFTP.should_receive(:start) do |url, username, options|
url.should == 'bla.com'
username.should == 'some_username'
options[:password].should == 'some_password'
<some condition>.should be_true
end
my_class.send_report
end
you can use expect
it 'successfully sends file' do
Net::SFTP.
should_receive(:start).
with('bla.com', 'some_username', :password => 'some_password')
my_class.send_report
end
it 'should verify the condition also' do
expect{ Net::SFTP.start(**your params**) }to change(Thing, :status).from(0).to(1)
end
Thanks #rickyrickyrice, your answer was almost correct. The problem is that it doesn't validate the correct number of arguments passed to Net::SFTP.start. Here's what I ended up using:
it 'sends a file with the correct arguments' do
Net::SFTP.should_receive(:start).with('bla.com', 'some_username', :password => 'some_password') do
<some condition>.should be_true
end
my_class.send_report
end

Resources