Ruby Net::SCP download asking for password then failing - ruby

I'm trying to download a file to an in-memory buffer from a unix box then upload the file to another unix box, but when I try to download the file it asks for my password then fails even if I put it in. I've tried both:
data = Net::SCP.download!("remote.host.com", "username", "/remote/path", :password => password)
and
data = Net::SCP::download!("remote.host.com", "username", "/remote/path", :password => password)
but I get:
Active Directory Password:
Then when I enter my password:
Net::SCP::Error: SCP did not finish successfully (1)
ruby 1.8.7 (2010-12-23 patchlevel 330) [i386-mingw32]
net-scp (1.0.4)

Try to use it like this:
require 'net/scp'
data = nil
Net::SCP.start("remote.host.com", "username", :password => "password") do |scp|
data = scp.download!("/remote/path")
end
puts data
EDIT: I only tried this on ruby ruby-1.9.3, sorry

Looks like Net::SFTP will be what I'm actually going to use, its not much more and is probably what I'd end up with anyway since I didn't realize I need to 'write' the #data before putting it on another server.
data = nil
Net::SFTP.start('remoteHost1', 'username', :password => 'password') do |sftp|
data = sftp.download!("filepath1")
end
Net::SFTP.start('remoteHost2', 'username', :password => 'password') do |sftp|
sftp.file.open("filePath2", "w") do |f|
f.puts data
end
end

Related

Upload file to server and delete original using NET::SCP gem in Ruby

I'm using the net/scp gem to upload a file from my machine to a remote Linux server.
require 'net/ssh'
require 'net/scp'
Net::SCP.upload!(10.125.0.0,
user,
local_path,
remote_dir,
:ssh => { :password => psw,
:key_data => keys})
This works perfectly, however I'd like to be able to 'move' the file instead of effectively copying it across.
Is there some scpparameter that will delete the original file?
Net::SCP doesn't have any such parameter:
https://github.com/net-ssh/net-scp/blob/master/lib/net/scp.rb#L259-L267
Since the only difference between a copy and a move is just a final deletion of the source, you should do this manually after upload!.
File.delete(local_path)
You most likely will want to make sure that Net::SCP has finished before doing so:
require 'net/ssh'
require 'net/scp'
channel = Net::SCP.upload!(10.125.0.0,
user,
local_path,
remote_dir,
:ssh => { :password => psw,
:key_data => keys})
channel.wait
File.delete(local_path)

Ruby hidden input password

I wrote a Ruby script that's trying to connect to a Postgres database hosted on Heroku.
If I use a hardcoded password, or if I load the password using gets, everything works fine.
However, if I load the password using IO.noecho, I get the following exception:
storing.rb:11:in `initialize': FATAL: password authentication failed for user "***" (PG::ConnectionBad)
FATAL: no pg_hba.conf entry for host "****", user "***", database "***", SSL off
from storing.rb:11:in `new'
from storing.rb:11:in `create_conn'
from fetch_currencies.rb:11:in `<main>'
Here's my code:
def create_conn(password)
conn = PGconn.connect(
:host => '***',
:port => 5432,
:dbname => '***',
:user => '***',
:password => password)
return conn
end
puts 'Postgres DB password:'
pass = STDIN.noecho(&:gets)
conn = create_conn(pass)
I tried printing the password after loading it, as well as checking whether it's a String, and everything seems to be fine. What could be the problem?
The problem, of course, was that I didn't chomp the input, so I guess the terminating new line character was also passed as part of the password.
The right way to go is then
pass = STDIN.noecho(&:gets).chomp

Ruby Net-SFTP capture and log exceptions

I have a simple SFTP script that I am testing to connect to a server and download a file, or files, with a specific date in the file name.
I am using rufus/scheduler to start the SFTP portion of the script every X minutes to see if a new file is on the server.
It all seems to work until I intentionally force an error, such as provide incorrect login credentials. Then I want to be able to capture the exact error or exception and log it using logger. I am not getting error detail or I am not using rescue correctly:
scheduler = Rufus::Scheduler::PlainScheduler.start_new(:frequency => 3.0)
log = Logger.new('sftp.log')
log.level = Logger::INFO
begin
log.info 'starting sftp'
Net::SFTP.start(HOST, ID, :password => PW ) do |sftp|
sftp.dir.glob("./", "20120820*") do |entry|
puts entry.name
file = entry.name
success = sftp.download!(file, file)
end
end
rescue Exception => e
puts e.message # Human readable error
log.error ("SFTP exception occured: " + e.message)
end
scheduler.join
Does adding :verbose => Logger::DEBUG work ?
Net::SFTP.start(HOST, ID, :password => PW, :verbose => Logger::DEBUG ) do |sftp|

ruby net-ssh calling bash script with interactive prompts

I have a problem that I hope you can help me with
I’m trying to use ruby to ssh onto a machine and run a bash script, this part is fairly easy but the bash script requires me to entry a username and password interactively and this is where I’m stuck
So if I run the script manually I see:-
./run_file.sh
Enter username:
Enter password:
So at the enter Username prompt I have to enter the username etc
I’ve got a simple method I use to make the connection and my idea was to pass in an array made up of
[‘command to run’, ‘username’, ‘password’]
but I don’t know how to extend the Net::SSH call to respond to the prompts
# ssh conectivity method
def command_ssh(host, cmd)
require 'net/ssh'
user = LocalConfig::SSH_DETAILS[:user]
pass = LocalConfig::SSH_DETAILS[:pass]
Net::SSH.start(host, user, :password => pass, :paranoid => false, :auth_methods => ['password'], :timeout => 10 )do |ssh |
output = (ssh.exec!(cmd[0]))
return output
end
end
anyone got any ideas
managed to fix this by using the channel function, here's the method I use now
def connect(host,command)
require 'rubygems'
require 'net/ssh'
user = LocalConfig::SSH_DETAILS[:user]
pass = LocalConfig::SSH_DETAILS[:pass
o = ""
Net::SSH.start(host, user, :password => pass, :paranoid => false, :auth_methods => ['password'], :timeout => 30 )do |ssh |
channel = ssh.open_channel do |ch|
ch.exec(command) do |ch2, success|
ch2.send_data "myUserName\n"
ch2.send_data "mPassWord\n"
ch.on_data do |ch2, data|
o += data
end
end
end
channel.wait
return o.to_s
end
end

Trying to get xauth to work with Ruby and the Readability API

I'd like to use the Readability API through the Readit gem; however, I've been having some trouble trying to get an access token through XAuth. Here's the code that I have:
require 'highline/import'
require 'yaml'
require 'oauth'
require 'readit'
config = YAML.load_file("config/readability.yaml")
uname = ask ("Username: ")
passwd = ask ("Password: ") {|q| q.echo = false}
consumer = OAuth::Consumer.new(config["-consumer_key"], config["-consumer_secret"], :site => "https://www.readability.com/api/rest/v1/oauth/access_token/")
access_token = consumer.get_access_token(nil, {}, {:x_auth_mode => 'client_auth', :x_auth_username => uname, :x_auth_password => passwd})
However, when I try to run this, I get the following:
/Users/mike/.rvm/gems/ruby-1.9.3-p125/gems/oauth-0.4.5/lib/oauth/consumer.rb:219:in `token_request': 404 NOT FOUND (OAuth::Unauthorized)
from /Users/mike/.rvm/gems/ruby-1.9.3-p125/gems/oauth-0.4.5/lib/oauth/consumer.rb:109:in `get_access_token'
from instab.rb:11:in `<main>'
Can someone explain to me what I am doing wrong?
You should write as follows:
consumer = ::OAuth::Consumer.new(Readit::Config.consumer_key,Readit::Config.consumer_secret,:site=>"https://www.readability.com/", :access_token_path => "/api/rest/v1/oauth/access_token/")

Resources