Error in net/ftp while using sendcmd function - ruby

I am new to ruby. I used net/ftp for accessing the remote files. I need to execute the command and get the result from remote machine. How can I do this??
I have tried the following way,
#!/usr/bin/ruby
require 'rubygems'
require 'net/ftp'
require 'fileutils'
URL = 'ip'
username = 'name'
passwd = "pass"
directory = 'path'
filename = 'file'
ftp=Net::FTP.new
ftp.connect(URL,21)
ftp.login(username,passwd)
ftp.chdir(directory)
ftp.sendcmd("fuser filename")
ftp.close
It throw the following error,
/usr/lib/ruby/1.8/net/ftp.rb:243:in `getresp': 500 Unknown command. (Net::FTPPermError)
from /usr/lib/ruby/1.8/net/ftp.rb:264:in `sendcmd'
from /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
from /usr/lib/ruby/1.8/net/ftp.rb:262:in `sendcmd'
from connection.rb:20
How can I solve this error?? Please give some suggestion on this..

As I've just found the answer to your question after I've found your question:
To issue site specific commands, add a "SITE" in front of the command:
ftp.sendcmd("SITE fuser #{filename}")

Related

Error trying to image scrape

I'm trying to make a ruby program which will automatically download the most recent Penny-Arcade. Here's the code I have:
require 'mechanize'
agent = Mechanize.new
date_string = Date.today.to_s
page = agent.get('http://www.penny-arcade.com/comic/')
puts page
art_link = page.at('div#comicFrame > a > img')['src']
File.open(date_string, 'wb') do |fo|
fo.write open(art_link).read
end
And the output I get from running the program is:
$ ruby grab_PA.rb
#<Mechanize::Page:0x007f38bc743af0>
grab_PA.rb:12:in `initialize': No such file or directory # rb_sysopen - http://art.penny-arcade.com/photos/i-QpzhbpN/0/1050x10000/i-QpzhbpN-1050x10000.jpg (Errno::ENOENT)
from grab_PA.rb:12:in `open'
from grab_PA.rb:12:in `block in <main>'
from grab_PA.rb:11:in `open'
from grab_PA.rb:11:in `<main>'
But if I copy that exact link and put it into Firefox, it opens up the image. What's happening here? The program does write an image file to the program's directory with today's date, but the file is empty.
open takes an argument that's a filename, not an URL. If you want to access the URL, you would normally have to do a lot more than simply open a file.
Luckily, Ruby provides a nice wrapper for Net::HTTP, called open-uri.
Just drop the following line at the top of your program and it should work fine:
require 'open-uri'
Get the art_link src (something like art_link.attributes['src']). And than agent.get from the source.
After you'll have only the image at agent.page. Just save it by agent.page.save ('image_path_and_name').

sh: 1: ruby: not found when i am executing ruby from browser

I am creating a PHP website which have to execute a ruby command from terminal for that i am using :-
1)
$output = shell_exec('ruby emailConverter.rb 2>&1');
var_dump($output);
This throws the error
string(23) "sh: 1: ruby: not found"
or
2)
exec("ruby emailConverter.rb", $output, $return);
var_dump($output);
UPDATE
AFTER doing this as #psal suggested
$path = '/home/vishal/.rvm/rubies/ruby-2.1.2/bin/ruby'
$output = shell_exec($path.' emailConverter.rb 2>&1');
var_dump($output);
getting this error
string(308) "/home/vishal/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in require: cannot load such file -- premailer (LoadError)
from /home/vishal/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in require'
from emailConverter.rb:4:in `<main>'
but when i execute this any of the above command(1 and 2) directly from terminal, it works fine...
what is the problem with that?
Any help?
In my case,
I added
#!/usr/bin/env /usr/local/rvm/wrappers/ruby-2.4.1/ruby
on top of the ruby script file.
ie:
#!/usr/bin/env /usr/local/rvm/wrappers/ruby-2.4.1/ruby
require 'jwt'
...
As #simonwo said, this error happend because Ruby is not in your PATH. Find the ruby binary using which then add it to your script. Usually it's in /usr/bin/.
Example on Ubuntu :
$ which ruby
/usr/bin/ruby
Once you have the path, add it to your shell_exec :
$rubyBin = '/usr/bin/ruby';
shell_exec("$rubyBin emailConverter.rb 2>&1");
// or
exec("$rubyBin emailConverter.rb", $output, $return);

Ruby 'require' errors with Selenium webdriver script

I'm trying to execute a script to test some user credentials, and I keep getting errors relating to 'require'. The script takes a csv of usernames and passwords, logs them into a site, and groups them into pass/fail lists.
I confess I didn't write the script- it was provided to us by an outside coder. I've taken a few CS101 classes, and can parse what it's doing and modify it for different domains and credential files, but I'm a total ruby beginner.
=========================
require 'selenium-webdriver'
require 'rubygems'
require 'csv'
pass_file = File.open('pass.txt', 'w')
fail_file = File.open('fail.txt', 'w')
in_file = File.open('test credentials.csv', 'r')
pass_file.puts "xxxxxx"
fail_file.puts "xxxxxx"
CSV.foreach(in_file, :col_sep => ',') do |row|
driver = Selenium::WebDriver.for :firefox
driver.get 'https://www.xxxxxx.com/'
driver.find_element(:id => 'UserNameEntry').send_keys row[0]
driver.find_element(:id => 'UserPasswordEntry').send_keys row[1]
driver.find_element(:id => 'xxxxxxlNameEntry').clear
driver.find_element(:id => 'xxxxxxNameEntry').send_keys "xxxxxx"
driver.find_element(:xpath => '//*[#id="cmdLoginButton"]/span').click
driver.switch_to.window(driver.window_handles[1])
if driver.title == "Welcome!"
pass_file.puts row[0]
elsif driver.title == "Login Unsuccessful"
fail_file.puts row[0]
end
driver.quit
end
==================
When I run it, at first I was getting "`require': no such file to load -- selenium-webdriver (LoadError)
from xxxxx.rb:1
Then I read on a forum that the requirement for gems should come first. After putting require 'rubygems' before require 'selenium-webdriver', now I get this error:
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/csv.rb:308:in initialize': can't convert File into String (TypeError)
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/csv.rb:308:inopen'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/csv.rb:308:in open_reader'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/csv.rb:94:inforeach'
I'm feeling so confused...any help appreciated.
You need to install the gems before you can require them. For example, the selenium-webdriver gem can be found here
Selenium WebDriver
You run gem install selenium-webdriver in a terminal program.
Try to remove space from csv file name, for example:
in_file = File.open('test_credentials.csv', 'r')
As I can see from the stacktrace, error occurs when reading csv file.
If there is not space between test credentials.csv then remove that. Check that there is _ present or not
If there is a space between test credentials.csv then write file name in your code as follows in_file = File.open("test\ credentials.csv", 'r')
If you use Bundler, add require bundler/setup at the top.
Please try this.
gem update --system

include file to ruby testScript

I have multiple ruby test cases for selenium-webdriver and all the files are sharing the same user name and password to login to my account. Is there a way to create a global file and include that file in these test cases instead of typing them over and over again, something like #include?
Here is the part of the code that needs to be shared between other test cases:
def setup
#driver = Selenium::WebDriver.for :firefox
#base_url = "http://localhost:3000/"
#driver.manage.timeouts.implicit_wait = 30
#verification_errors = []
#facebook_ID = "xxxxxxxxxx#xxx.xxx"
#facebook_password = "xxxxxxx"
#facebook_receiver_friend = "John Smith"
end
There are multiple ways to do it.
You could use
require 'setup'
where setup.rb is the common file that has all the setting up variables/functions.
You could also use a YAML file. More information here. Where all your config attributes can be defined.
Also put this is at the top of the file $:.unshift '.' . This is so that your test file can "discover" your setup.rb file which is routable from the home directory.
Note - Even though you will save the file as setup.rb you will require as only setup.
I got my answer:
eval File.open('setup.dat').read
This will insert what I have in setup.dat as a text wherever I need it.

net/ftp invalid argument error

I have a relatively simple script that was working. Nothing in the script changed and I'm using ruby 1.8.6.
require 'net/ftp'
ftp = Net:: FTP.new(ip)
ftp.login(user=name,passwd=pass)
ftp.chdir(pathHere)
ftp.gettextfile('onhandapt.txt', File.basename('onhandapt.txt'))
ftp.close
I know from running through the steps in irb that I can login successfully, and even issue a ftp.list command to get the current directory, but ftp.chdir is where the scripts fails. In irb, the ftp.chdir command yields 'nil'. Double checked the path on the server.
The script produces this error:
c:/ruby/lib/ruby/1.8/net/ftp.rb:211:in readline': Invalid argument (Errno::EINVAL)
from c:/ruby/lib/ruby/1.8/net/ftp.rb:211:ingetline'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:221:in getmultiline'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:235:ingetresp'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:251:in voidresp'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:274:invoidcmd'
from c:/ruby/lib/ruby/1.8/monitor.rb:242:in synchronize'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:290:insendport'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:298:in makeport'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:329:intransfercmd'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:421:in retrlines'
from c:/ruby/lib/ruby/1.8/monitor.rb:242:insynchronize'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:419:in retrlines'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:518:ingettextfile'
Ideas welcome.
make sure pathHere contains a valid pathname and not set to nil
Figured it out. We had some office network changes. I now need to use passive mode, which can be set with this command: ftp.passive=true

Resources