Ruby and Telnet

Hello!

I’m having problems with the library ‘net/telnet’.

What I’m trying to do is to re-create a normal telnet email session with
a smtp server.

So, my question is : is it possible to reproduce the HELO, MAIL FROM,
RCPT TO etc in this Ruby module?

What I tried to do is here below, and with this I receive correctly the
220 message, but what I’m seeking is the 250!

require ‘net/telnet’
t = Net::Telnet::new( “Timeout” => 10,
“Prompt” => /%/,
“Host” => “gmail-smtp-in.l.google.com”,
“Port” => 25 )
t.login(“MY ACCOUNT”, “MY PASSWORD”) {|str| print str}
t.cmd(“HELO MY ACCOUNT”) {|str| print str}
t.print(“top”)
process_string = t.waitfor(/\d+ processes/)
t.close

Where am I mistaken?

Ah! Solved!
I messed around with the console and find the key command :

t.cmd(“HELO *your email”) {|str| print str}

Simpler than I had thought :slight_smile:

On Nov 1, 2011, at 3:22 PM, Leo M. wrote:

What I tried to do is here below, and with this I receive correctly the
process_string = t.waitfor(/\d+ processes/)
t.close

Where am I mistaken?

Why aren’t you using net/smtp?

Because I want to perform a smtp reverse lookup!

I got another issue though.

It seems that it doesn’t recognize from the second command onward.

So basically I’m able to perform just the first “helo my email
address
”, but I’m don’t receive any answer for an eventual “mail
from:<email>” or “rcpt to:<email>”.

Any idea?

Leo M. wrote in post #1029741:

Because I want to perform a smtp reverse lookup!

And what do you mean by “a smtp reverse lookup”??

If you want to talk SMTP directly, then you first need to read and fully
understand RFC 2821.

Then use TCPSocket.new(‘smtp.example.com’,25) and use the socket
directly.

There are lots of pitfalls - e.g. remember to send \r\n at the end of
each line, instead of \n, to be properly compatible.

As someone else said: just use Net::SMTP where all this has been taken
care of.

On Nov 2, 2011, at 2:57 AM, Leo M. wrote:

Because I want to perform a smtp reverse lookup!

What is “smtp reverse lookup”?

Do you mean DNS reverse lookup? You can do that with this script:

require ‘resolv’

Resolv::DNS.open do |dns|
res = dns.getresources ARGV.shift, Resolv::DNS::Resource::IN::MX
exchanges = {}
res.each do |r|
exchanges[r.exchange] = r.preference
end

forward = Hash.new { |h, k| h[k] = [] }

exchanges.each_key do |exchange|
res = dns.getresources exchange, Resolv::DNS::Resource::IN::A

res.each do |r|
  forward[exchange] << r.address
end

end

forward.each do |exchange, addresses|
addresses.each do |address|
res = dns.getresources address.to_name,
Resolv::DNS::Resource::IN::PTR

  res.each do |r|
    puts "MX #{exchange} #{exchanges[exchange]}"
    puts "\thas address #{address}"
    puts "\twith reverse #{r.name}"
  end
end

end
end

Hmm this script does not seem to work for me:

-> “550-5.7.1 [178.190.40.208] The IP you’re using to send mail is not
authorized to\r\n”
-> “550-5.7.1 send email directly to our servers. Please use the SMTP
relay at your\r\n”
-> “550-5.7.1 service provider instead.”

On Nov 2, 2011, at 3:27 PM, Marc H. wrote:

Hmm this script does not seem to work for me:

-> “550-5.7.1 [178.190.40.208] The IP you’re using to send mail is not
authorized to\r\n”
-> “550-5.7.1 send email directly to our servers. Please use the SMTP
relay at your\r\n”
-> “550-5.7.1 service provider instead.”

Sounds like too much spam comes from your ISP :frowning:

If you’re using the script I posted or something like it to do spammy
things I imagine it will block you before long.

Ok got it, thank you very much all.
So, to do a DNS reverse lookup the ruby’s gem telnet is not the best
tool. But then I’m wondering : what is useful to?

On Nov 2, 2011, at 3:12 AM, Leo M. wrote:

I got another issue though.

It seems that it doesn’t recognize from the second command onward.

So basically I’m able to perform just the first “helo my email
address
”, but I’m don’t receive any answer for an eventual “mail
from:<email>” or “rcpt to:<email>”.

Any idea?

This script uses Net::SMTP and can send mail to gmail.com accounts

require ‘net/smtp’
require ‘time’

smtp = Net::SMTP.new ‘gmail-smtp-in.l.google.com’, ‘smtp’
smtp.debug_output = $stderr

smtp.start ‘example’ do
smtp.mailfrom ‘[email protected]
smtp.rcptto ‘[email protected]
smtp.data do |body|
body.puts “Subject: test from net/smtp”
body.puts “From: [email protected]
body.puts “Date: #{Time.now.rfc2822}”
body.puts
body.puts “This is how net/smtp works:”
body.puts
IO.readlines(FILE).each do |line|
body.puts " #{line}"
end
end
end

On Thu, Nov 3, 2011 at 11:48 AM, Leo M. [email protected] wrote:

Ok got it, thank you very much all.
So, to do a DNS reverse lookup the ruby’s gem telnet is not the best
tool. But then I’m wondering : what is useful to?

http://ruby-doc.org/stdlib-1.9.2/libdoc/socket/rdoc/Socket.html#method-c-gethostbyaddr

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs