# Problem with ruby telnet really need help now plz

**URL:** <https://www.ruby-forum.com/t/problem-with-ruby-telnet-really-need-help-now-plz/161329>\
**Category:** Ruby\
**Created:** [February 25, 2009, 3:13pm UTC](https://www.ruby-forum.com/t/problem-with-ruby-telnet-really-need-help-now-plz/161329 "2009-02-25T15:13:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rayon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@rayon](https://www.ruby-forum.com/u/rayon)\
**Post date:** [February 25, 2009, 3:13pm UTC](https://www.ruby-forum.com/t/problem-with-ruby-telnet-really-need-help-now-plz/161329/1 "2009-02-25T15:13:43Z")

</div>

i have been using ruby telnet class to run a dump in a northtel switch  
and capture it to a file.  
here is the error .  
Renameing GSMFILES

Download GPRS

Download BSVC

Download SSOP

An established connection was aborted by the software in your host  
machine.

c:/ruby/lib/ruby/1.8/net/telnet.rb:601:in `syswrite’: An established  
connection was aborted by the software in your host machine.  
(Errno::ECONNABORTED)

```
    from c:/ruby/lib/ruby/1.8/net/telnet.rb:601:in `write'

    from c:/ruby/lib/ruby/1.8/net/telnet.rb:625:in `print'

    from c:/ruby/lib/ruby/1.8/net/telnet.rb:634:in `puts'

    from C:\Documents and

```

Settings\rhunte\Desktop\telnetmonster\lib/telnet.rb:30:in `telnet’

```
    from C:/Documents and

```

Settings/rhunte/Desktop/telnetmonster/lib/main.rb:63

and here the class code:

require “rubygems”  
require “net/telnet”  
#================================

class Telnet  
attr\_accessor :host , :user ,:pass , :cmd ,:cmd2 ,:cmd3,:outfile,:ex

def initialize

end

def telnet  
begin  
t = Net::Telnet::new( “Host” =\> @host,“Timeout” =\> 200000,  
“Telnetmode”=\> true,“Waittime”=\>2000)  
t.puts @user  
t.puts @pass  
t.puts @cmd  
t.puts @cmd2  
t.puts @cmd3  
t.puts(“logout”)  
File.open(@outfile,“w”)  
t.cmd("") do |data| print data  
File.open(@outfile, “a”) { |i|  
i.print(data)  
}  
end  
rescue StandardError =\> @ex  
puts @ex  
end  
ensure  
t.puts(“logout”)  
end

end

---

<div class="post-metadata">

**Author:** ![rayon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@rayon](https://www.ruby-forum.com/u/rayon)\
**Post date:** [February 25, 2009, 4:32pm UTC](https://www.ruby-forum.com/t/problem-with-ruby-telnet-really-need-help-now-plz/161329/2 "2009-02-25T16:32:19Z")

</div>

Well, ECONNABORTED is unusual, and is probably some sort of firewalling  
problem. What happens if you are on the same machine as your Ruby  
program and telnet to the same target host?

Try running

tcpdump -i eth0 -n -s0 -X host x.x.x.x

to look at the traffic to/from host x.x.x.x. (Or Wireshark if this is a  
Windows box).

I can see a few other problems.

> ```
> t = Net::Telnet::new( "Host" => @host,"Timeout" => 200000,
> 
> ```
> 
> “Telnetmode”=\> true,“Waittime”=\>2000)

These timeouts are in seconds, not milliseconds

> ```
> t.puts @user
> t.puts @pass
> t.puts @cmd
> t.puts @cmd2
> t.puts @cmd3
> t.puts("logout")
> 
> ```

You are blasting all these strings to the device, without waiting for  
prompts first.

If t.login doesn’t work for you, do it by hand:

log = lambda { |c| STDERR.print c }

t.waitfor(/ogin:/, &log)  
t.puts @user  
t.waitfor(/assword:/, &log)

> ```
> File.open(@outfile,"w")
> 
> ```

Here you open the file, but discard the file handle completely, so it  
will be closed again when the garbage collector next runs.

> ```
> t.cmd("") do |data| print data
> File.open(@outfile, "a") { |i|
> i.print(data)
> }
> end
> 
> ```

This re-opens the outfile for every response or part-response received  
from the target device. Much better to open it once, e.g.

log = nil  
File.open(@outfile,“a”) do |f|  
log = lambda { |c|  
STDERR.print c  
f.print c  
}  
end

Another less painful way to get logging is to use the Output\_log option,  
e.g.

```
      t = Net::Telnet.new( "Host" => @host, "Output_log" => 

```

@outfile)

or

```
      t = Net::Telnet.new( "Host" => @host, "Output_log" => 

```

“/dev/stderr”)

(but this won’t write to both, of course)

---

<div class="post-metadata">

**Author:** ![rayon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@rayon](https://www.ruby-forum.com/u/rayon)\
**Post date:** [February 25, 2009, 11:13pm UTC](https://www.ruby-forum.com/t/problem-with-ruby-telnet-really-need-help-now-plz/161329/3 "2009-02-25T23:13:38Z")

</div>

Can you connect normally?

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![rayon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@rayon](https://www.ruby-forum.com/u/rayon)\
**Post date:** [February 26, 2009, 2:38am UTC](https://www.ruby-forum.com/t/problem-with-ruby-telnet-really-need-help-now-plz/161329/4 "2009-02-26T02:38:03Z")

</div>

Julian L. wrote:

> Can you connect normally?
> 
> Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
> Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

yes i can and catpute the file using cross talk.  
i am trying to automate that process  
here is the modifed code  
still faling btw

# To change this template, choose Tools | Templates

# and open the template in the editor.

require “rubygems”  
require “net/telnet”  
#================================

class Telnet  
attr\_accessor :host , :user ,:pass , :cmd ,:cmd2 ,:cmd3,:outfile,:ex

def initialize

end

def telnet  
begin  
t = Net::Telnet::new( “Host” =\> @host,“Timeout” =\> 50000000,  
“Telnetmode”=\> true,“Waittime”=\>2000,“Output\_log”=\> @outfile)  
t.puts @user  
t.puts @pass  
t.puts @cmd  
t.puts @cmd2  
t.puts @cmd3  
t.puts(“logout”)  
f = File.open(@outfile, “w”)  
t.cmd(“”)  
t.write(“bottom”)  
f.close  
end

rescue StandardError =\> @ex  
puts @ex  
ensure

```
t.puts("logout")

```

end  
end

the error message  
An established connection was aborted by the software in your host  
machine.

c:/ruby/lib/ruby/1.8/net/telnet.rb:601:in `syswrite’: An established  
connection was aborted by the software in your host machine.  
(Errno::ECONNABORTED)

```
    from c:/ruby/lib/ruby/1.8/net/telnet.rb:601:in `write'

    from c:/ruby/lib/ruby/1.8/net/telnet.rb:625:in `print'

    from c:/ruby/lib/ruby/1.8/net/telnet.rb:634:in `puts'

    from C:\Documents and 

```

Settings\rhunte\Desktop\telnetmonster\lib/telnet.rb:34:in `telnet’

```
    from C:/Documents and 

```

Settings/rhunte/Desktop/telnetmonster/lib/main.rb:51

---

<div class="post-metadata">

**Author:** ![rayon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@rayon](https://www.ruby-forum.com/u/rayon)\
**Post date:** [February 26, 2009, 12:46pm UTC](https://www.ruby-forum.com/t/problem-with-ruby-telnet-really-need-help-now-plz/161329/5 "2009-02-26T12:46:40Z")

</div>

Rayon Hunte wrote:

> Julian L. wrote:
> 
> > Can you connect normally?
> > 
> > Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
> > Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)
> 
> yes i can and catpute the file using cross talk.  
> i am trying to automate that process  
> here is the modifed code  
> still faling btw
> 
> # To change this template, choose Tools | Templates
> 
> # and open the template in the editor.
> 
> require “rubygems”  
> require “net/telnet”  
> #================================
> 
> class Telnet  
> attr\_accessor :host , :user ,:pass , :cmd ,:cmd2 ,:cmd3,:outfile,:ex
> 
> def initialize
> 
> end
> 
> def telnet  
> begin  
> t = Net::Telnet::new( “Host” =\> @host,“Timeout” =\> 50000000,  
> “Telnetmode”=\> true,“Waittime”=\>2000,“Output\_log”=\> @outfile)  
> t.puts @user  
> t.puts @pass  
> t.puts @cmd  
> t.puts @cmd2  
> t.puts @cmd3  
> t.puts(“logout”)  
> f = File.open(@outfile, “w”)  
> t.cmd(“”)  
> t.write(“bottom”)  
> f.close  
> end
> 
> rescue StandardError =\> @ex  
> puts @ex  
> ensure
> 
> ```
> t.puts("logout")
> 
> ```
> 
> end  
> end
> 
> the error message  
> An established connection was aborted by the software in your host  
> machine.
> 
> c:/ruby/lib/ruby/1.8/net/telnet.rb:601:in `syswrite’: An established  
> connection was aborted by the software in your host machine.  
> (Errno::ECONNABORTED)
> 
> ```
> from c:/ruby/lib/ruby/1.8/net/telnet.rb:601:in `write'
> 
> from c:/ruby/lib/ruby/1.8/net/telnet.rb:625:in `print'
> 
> from c:/ruby/lib/ruby/1.8/net/telnet.rb:634:in `puts'
> 
> from C:\Documents and 
> 
> ```
> 
> Settings\rhunte\Desktop\telnetmonster\lib/telnet.rb:34:in `telnet’
> 
> ```
> from C:/Documents and 
> 
> ```
> 
> Settings/rhunte/Desktop/telnetmonster/lib/main.rb:51

i have made some more changes and here it is

# To change this template, choose Tools | Templates

# and open the template in the editor.

require “rubygems”  
require “net/telnet”  
#================================

class Telnet  
attr\_accessor :host , :user ,:pass , :cmd ,:cmd2 ,:cmd3,:outfile,:ex

def initialize

end

def telnet  
begin  
t = Net::Telnet::new( “Host” =\> @host,“Timeout” =\> 200000,  
“Telnetmode”=\> true,“Waittime”=\>2000,“Output\_log” =\> @outfile)  
t.puts @user  
t.puts @pass  
t.puts @cmd  
t.puts @cmd2  
t.puts @cmd3  
t.puts(“logout”)

# File.open(@outfile,“w”)

```
t.cmd("")

```

end  
rescue StandardError =\> @ex  
puts @ex

ensure  
t.puts(“logout”)  
end

end

i am now using the telnet output\_log to write the files bu i am still  
getting the error it runs for about a hour or so and then comes up with  
the error and not always that the same place. it’s freaking me out

---

<div class="post-metadata">

**Author:** ![rayon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e99b99/32.png) [@rayon](https://www.ruby-forum.com/u/rayon)\
**Post date:** [February 26, 2009, 2:17pm UTC](https://www.ruby-forum.com/t/problem-with-ruby-telnet-really-need-help-now-plz/161329/6 "2009-02-26T14:17:37Z")

</div>

Rayon Hunte wrote:

> > def telnet  
> > begin  
> > t = Net::Telnet::new( “Host” =\> @host,“Timeout” =\> 50000000,  
> > “Telnetmode”=\> true,“Waittime”=\>2000,“Output\_log”=\> @outfile)  
> > t.puts @user  
> > t.puts @pass  
> > t.puts @cmd  
> > t.puts @cmd2  
> > t.puts @cmd3  
> > t.puts(“logout”)  
> > f = File.open(@outfile, “w”)  
> > t.cmd("")  
> > t.write(“bottom”)  
> > f.close  
> > end

You’re still not doing this right. You’re blatting a load of lines to  
the server, in the hope that it will accept them all, and logging out  
immediately in the hope that you will capture the data before it logs  
out.

If you do it one line at a time - wait for prompt, send line, wait for  
prompt, send line etc - then you will be able to find out at which point  
it is failing.

Also, try using the Dump\_log option. This is like Output\_log but it  
gives you a raw hex dump of data in both directions.
