Ruby Telnet refuses connection - ruby

I need to make a cache server, one like memcached actually, so I´m trying to start a Telnet server so I can send commands which will then tell me if i need to store data or read from a key. I thought of doing it this way since I need to implement a TCP server and I need to use commands so a normal HTTP server wouldn't be enough (though I could post the data and the address could be the command, I'm still figuring it out).
Now after that background, the real problem is that I can't even start my Telnet server, this is the code I found on the web:
localhost = Net::Telnet::new("Host" => "localhost",
"Timeout" => 10,
"Prompt" => /[$%#>] \z/n)
localhost.login("username", "password") { |c| print c }
localhost.cmd("command") { |c| print c }
localhost.close
I don't really understand much of this, maybe I'm trying to connect to and existing Telnet server with that code and that's why it's not working. Anyway the error I'm getting it's this:
C:/Ruby22-x64/lib/ruby/2.2.0/net/telnet.rb:350:in `initialize': No connection could be made because the target machine actively refused it. - connect(2) for "localhost" port 23 (Errno::ECONNREFUSED)
from C:/Ruby22-x64/lib/ruby/2.2.0/net/telnet.rb:350:in `open'
from C:/Ruby22-x64/lib/ruby/2.2.0/net/telnet.rb:350:in `block in initialize'
from C:/Ruby22-x64/lib/ruby/2.2.0/timeout.rb:88:in `block in timeout'
from C:/Ruby22-x64/lib/ruby/2.2.0/timeout.rb:98:in `call'
from C:/Ruby22-x64/lib/ruby/2.2.0/timeout.rb:98:in `timeout'
from C:/Ruby22-x64/lib/ruby/2.2.0/net/telnet.rb:349:in `initialize'
from C:/Users/Bruno/RubymineProjects/Ruby Server/Server.rb:37:in `new'
from C:/Users/Bruno/RubymineProjects/Ruby Server/Server.rb:37:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
Process finished with exit code 1
Thank you in advance for any help or tip, as I said I'm still trying to figure out how to do this.

What you probably want is this:
localhost = Net::Telnet::new(
"Host" => "localhost",
"Port" => 8899,
"Timeout" => 10,
"Prompt" => /[$%#>] \z/n
)
That will connect to port 8899 on your local machine. As noted the telnet protocol is all but dead and really has no use in 2016.
The telnet client on the other hand is great for connecting to TCP services with a plain-text protocol. This includes HTTP and, as you've observed, Memcache.
I'd recommend using the more generic TCPServer as the basis for your code:
server = TCPServer.new(8899)
Then you can build up your service from there using threads, or if necessary, fibers.
While telnet might be dead, TCP/IP is alive and well. Simple, plain-text protocols are as popular as ever, which is why it's easy to confuse telnet the protocol with telnet the tool.

Related

Remote connect to Heroku pg database error: could not translate host name to address

I am trying to connect to my Heroku hosted postgres database but unfortunately it is returning a "failed to translate host name to address" error. I directly take the DATABASE_URL from heroku as shown in the establish_connection block below
ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:host => `heroku config:get DATABASE_URL -amy_app`,
:database => "my_db",
:username => "postgres",
:password => "password")
The trace of the error is:
C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize': could not
translate host name "postgres://address_to_db.1.amazonaws.com:5432/ (PG::Connecti
onBad)
" to address: Unknown server error
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `new'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initializ
e'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql
_connection'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new
_connection'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `che
ckout_new_connection'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acq
uire_connection'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `blo
ck in checkout'
from C:/Ruby21-x64/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `che
ckout'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `blo
ck in connection'
from C:/Ruby21-x64/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `con
nection'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `ret
rieve_connection'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/connection_handling.rb:87:in `connection'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/attributes.rb:93:in `columns'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/attributes.rb:98:in `columns_hash'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/dynamic_matchers.rb:64:in `block in valid?'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/dynamic_matchers.rb:64:in `each'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/dynamic_matchers.rb:64:in `all?'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/dynamic_matchers.rb:64:in `valid?'
from C:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activerecord-4.2.7.1/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
After looking into it I found this solution to the problem
db_parts = ENV['DATABASE_URL'].split(/\/|:|#/)
username = db_parts[3]
password = db_parts[4]
host = db_parts[5]
db = db_parts[7]
from: Can't connect to PostgreSQL database on Heroku using Ruby - could not translate host name
when I do this I instead receive a NoDatabaseError. I have manually entered the db name without any success. It is unclear to me as to whether this means it managed to find the database.
Does anyone have an idea of how to resolve this? Any help would be greatly appreciated.
Edit: I have no managed to connect by running heroku config:get DATABASE_URL -amy_app and reading the pre url "connection information" into the individual fields manually. It is unclear as to why a) only the url is returned when the request is made through a ruby system call. b) why it doesn't work using the split method
Tl;dr: Heroku's documentation doesn't explain how to connect to your app's database remotely or at least their documentation implies that the host should be the full DATABASE_URL.
Thanks,
SOliver
Try connecting to the server using a GUI client (like PSequel) to see if the database is indeed there.
If it's not, do
bundle exec rake db:create
bundle exec rake db:migrate

tor-privoxy error: connection refused

I'm trying to use tor-privoxy to switch my IP address.
I copied the code from the README, for the most part:
require 'tor-privoxy'
agent ||= TorPrivoxy::Agent.new '127.0.0.1', '', {8123 => 9051} do |agent|
sleep 5
p agent
p "New IP is #{agent.ip}"
end
But when I run it, I got this error:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:641:in `rescue in connection_for': connection refused: 127.0.0.1:8123 (Net::HTTP::Persistent::Error)
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:589:in `connection_for'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:994:in `request'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/mechanize-2.7.3/lib/mechanize/http/agent.rb:259:in `fetch'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/mechanize-2.7.3/lib/mechanize.rb:440:in `get'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/tor-privoxy-0.1.1/lib/tor-privoxy/agent.rb:38:in `ip'
from C:/Users/Lap/Dropbox/Programming_Stuff/Ruby_Stuff/Code/tor and socksify stuff/torPrivoxyTest.rb:6:in `block in <main>'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/tor-privoxy-0.1.1/lib/tor-privoxy/agent.rb:11:in `call'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/tor-privoxy-0.1.1/lib/tor-privoxy/agent.rb:11:in `initialize'
from C:/Users/Lap/Dropbox/Programming_Stuff/Ruby_Stuff/Code/tor and socksify stuff/torPrivoxyTest.rb:3:in `new'
from C:/Users/Lap/Dropbox/Programming_Stuff/Ruby_Stuff/Code/tor and socksify stuff/torPrivoxyTest.rb:3:in `<main>'
[Finished in 8.5s with exit code 1]
What am I doing wrong?
It seems you are using the wrong port. Privoxy uses port 8118 (8123 is polipo). Tor on the other side uses either 9050 (plain Tor) or 9150 (Tor browser bundle). So if you change your settings the communication should work.

Selenium Grid Timeout

The following selenium-client (1.2.18) script times out in our test environment. The script works locally, but not in our test environment. I've only included the bare minimum code necessary to reproduce the timeout (which is why I'm doing a http.post directly).
On the test environment, I've looked at the firefox sessionstore.js and seen the browser is getting to the site, but it seems like it never communicates back to the node.
EDIT: curl 'www.google.com' works from the test environment.
require 'selenium/client'
require 'net/http'
#browser = Selenium::Client::Driver.new(:host => 'localhost',
:port => 4445,
:browser => '*firefox',
:url => 'http://www.google.com/',
:timeout_in_second => 60)
#browser.start_new_browser_session
session_id = #browser.session_id
p "SESSION ID: #{session_id}"
http = Net::HTTP.new('localhost', 4445)
begin
response = http.post('/selenium-server/driver/',"cmd=open&1=landing/summer/index.html&2=true&sessionId=#{session_id}")
p "RESPONSE: #{response}"
ensure
p "CLOSING SESSION..."
#browser.close_current_browser_session
end
Grid hub was started with the following:
selenium-server-standalone-2.25.0.jar -port 4445 -timeout 180 -singleWindow -role hub -browserTimeout 300 -newSessionMaxWaitTimeInSeconds 300 -newSessionWaitTimeout 300 -firefoxProfileTemplate /var/tmp/selenium-grid-start-stop/releases/20130802031034/firefox_profile -trustAllSSLCertificates
Node started with the following:
selenium-server-standalone-2.25.0.jar -role node -port 6666 -hub http://localhost:4445/grid/register -maxSession 1 -firefoxProfileTemplate /var/tmp/selenium-grid-start-stop/releases/20130802031034/firefox_profile -browser browserName=*firefox,seleniumProtocol=Selenium
And running the test script...
$ruby test2.rb
"SESSION ID: 18609e43667a4aa094fe81ef30551b84"
"CLOSING SESSION..."
/usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/protocol.rb:140:in `rescue in rbuf_fill': Timeout::Error (Timeout::Error)
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/protocol.rb:134:in `rbuf_fill'
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/protocol.rb:116:in `readuntil'
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/protocol.rb:126:in `readline'
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:2219:in `read_status_line'
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:2208:in `read_new'
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:1191:in `transport_request'
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:1177:in `request'
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:1170:in `block in request'
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:627:in `start'
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:1168:in `request'
from /usr/local/rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:978:in `post'
Anyone seen this kind of timeout before? The site (google) is obviously up. Perhaps something is misconfigured in the test environment?
I've found the solution. We accidently upgraded the Firefox in our test environment from 10 -> 17.
Our Grid nodes were providing a Firefox prefs.js that did not work with Firefox 17. This allowed the browser to start up and connect to the site successfully, but unable (for whatever reason) to respond back to the node.
The solution was to provide an updated prefs.js that worked for FF 17. We obtained this by running a test without providing the preferences ourselves. This forced Firefox to create a default preferences file we copied and edited.

Trying to download file from FTP results in an "500 Illegal PORT command" error

If I execute this locally, everything works fine:
require 'net/ftp'
ftp=Net::FTP.new("myftpserver.com", "username", "password")
ftp.getbinaryfile("/myfile.zip","localfile.zip")
ftp.close
If I attempt to execute it on the Linux server I am using, the result is:
/usr/local/lib/ruby/1.9.1/net/ftp.rb:273:in `getresp': 500 Illegal
PORT command. (Net::FTPPermError) from
/usr/local/lib/ruby/1.9.1/net/ftp.rb:281:in `voidresp' from
/usr/local/lib/ruby/1.9.1/net/ftp.rb:304:in `block in voidcmd' from
/usr/local/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize' from
/usr/local/lib/ruby/1.9.1/net/ftp.rb:302:in `voidcmd' from
/usr/local/lib/ruby/1.9.1/net/ftp.rb:317:in `sendport' from
/usr/local/lib/ruby/1.9.1/net/ftp.rb:325:in `makeport' from
/usr/local/lib/ruby/1.9.1/net/ftp.rb:358:in `transfercmd' from
/usr/local/lib/ruby/1.9.1/net/ftp.rb:420:in `block (2 levels) in
retrbinary' from /usr/local/lib/ruby/1.9.1/net/ftp.rb:166:in
`with_binary' from /usr/local/lib/ruby/1.9.1/net/ftp.rb:419:in `block
in retrbinary' from /usr/local/lib/ruby/1.9.1/monitor.rb:201:in
`mon_synchronize' from /usr/local/lib/ruby/1.9.1/net/ftp.rb:418:in
`retrbinary' from /usr/local/lib/ruby/1.9.1/net/ftp.rb:539:in
`getbinaryfile'
What could be the problem?
I found the answer at http://www.ruby-forum.com/topic/161274:
Beyond firewalls, active ftp won't work behind a NAT device. Ftp
servers sometimes say illegal port command if you tell them that your
address is a private ip address like 192.168.x (your address on the
network behind the nat device)
Adding:
ftp.passive = true
fixed it.

Ruby networking problem on windows

I am running windows XP with ruby 1.8.6 patchlevel 111. I am using HTTP to connect to a remote server and it has been running fine. All of a sudden it started to through the exception listed below (I did not change any code since the last time I ran it successfully). Does anybody know what is going on?
c:/ruby/lib/ruby/1.8/timeout.rb:54:in `rbuf_fill': execution expired (Timeout::E
rror)
from c:/ruby/lib/ruby/1.8/timeout.rb:56:in `timeout'
from c:/ruby/lib/ruby/1.8/timeout.rb:76:in `timeout'
from c:/ruby/lib/ruby/1.8/net/protocol.rb:132:in `rbuf_fill'
from c:/ruby/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
from c:/ruby/lib/ruby/1.8/net/protocol.rb:126:in `readline'
from c:/ruby/lib/ruby/1.8/net/http.rb:2029:in `read_status_line'
from c:/ruby/lib/ruby/1.8/net/http.rb:2018:in `read_new'
from c:/ruby/lib/ruby/1.8/net/http.rb:1059:in `request'
... 19 levels...
from c:/ruby/lib/ruby/1.8/test/unit/autorunner.rb:216:in `run'
from c:/ruby/lib/ruby/1.8/test/unit/autorunner.rb:12:in `run'
from c:/ruby/lib/ruby/1.8/test/unit.rb:278
from c:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader
.rb:5
rake aborted!
Command failed with status (3): [c:/ruby/bin/ruby -Ilib;test "c:/ruby/lib/r...]
Maybe the remote host is down? Or a new firewall has been put between your machine and the remote host?
"Timeout::Error" usually points to that direction.
besides the obvious (firewall, you got blacklisted for bad user-agent or ignoring robots.txt), you can try curl
http://curl.haxx.se/libcurl/ruby/
OR increase net/http timeout to say, 30+ seconds
http://groups.google.com/group/rubyonrails-talk/msg/cc89e8ae6703d6fb
It could be related to this known Ruby bug where Timeout::Error does not subclass Exception. (fixed in 1.9.2 I believe)
http://lindsaar.net/2007/12/9/rbuf_filltimeout-error
It can be fixed by rescuing from Timeout::Error like rescue Timeout::Error => e

Resources