I want to use ruby, but I don't think I can... help

M

Michael Hale

This email is sort of a public cry for help. As a language I really
love ruby and I would like nothing better than to be able to use it for
all my scripting needs, but I keep running into obstacles that keep me
from doing so.

Most recently I wanted to script a telnet session to automate
configuring a network device. The scripts were written in TCL and I
figured it would be an easy job to convert it to ruby. I didn't count
on running into so many socket issues with ruby on windows. In ruby
sockets are not responsive. I have to close the socket before I get
any output. TCL works fine.

I wanted to validate an xml schema using ruby but again I could not
make this work under windows.

I tried connecting to a third party library smartlib from Spirent. I
developed all of my code in linux and it worked fine, but when I tried
to move it to windows (our primary development platform) it was unable
to establish a tcp connection to the test device.

I guess maybe the answer is fill in the holes with "C". The only
problem is I have never been a "C" programmer. I came to ruby as a
java programmer, which in my particular case means I can't get very far
at all with "C". I defiantly don't have the expertise to create a
working sockets implementation.

I have tried many of the various ruby to java integration solutions,
but none of them work well enough to be useful in my situations.

What should I do? Any sage words of wisdom? Technical advice? Any
help is welcome

-- Frustrated (Michael Hale)
 
A

Ara.T.Howard

This email is sort of a public cry for help. As a language I really love
ruby and I would like nothing better than to be able to use it for all my
scripting needs, but I keep running into obstacles that keep me from doing
so.

you should post them as you find them - this group is generally extremely
helpful. ;-)
Most recently I wanted to script a telnet session to automate configuring a
network device. The scripts were written in TCL and I figured it would be
an easy job to convert it to ruby. I didn't count on running into so many
socket issues with ruby on windows. In ruby sockets are not responsive. I
have to close the socket before I get any output. TCL works fine.

what do you mean 'not responsive'? here is a little program which connects to
my local wiki server and displays the output - it seems to get output before
the connection is closed either manually or automatically before the program
exits...

jib:~ > cat a.rb
require 'net/telnet'
tn = Net::Telnet.new({'Port'=>4242}){|s| print s}
tn.cmd(""){|s| print s}
puts 'sleeping...'
sleep 4.2


jib:~ > ruby a.rb
Trying localhost...
Connected to localhost.
HTTP/1.1 400 Bad Request
Connection: close
Date: Fri, 06 Aug 2004 15:19:15 GMT
Content-Type: text/html
Server: WEBrick/1.3.1 (Ruby/1.8.1/2003-12-25)
Content-Length: 288

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD><TITLE>Bad Request</TITLE></HEAD>
<BODY>
<H1>Bad Request</H1>
bad Request-Line `'.
<HR>
<ADDRESS>
WEBrick/1.3.1 (Ruby/1.8.1/2003-12-25) at
jib.ngdc.noaa.gov:4242
</ADDRESS>
</BODY>
</HTML>
sleeping...


again - let us see your code...

I wanted to validate an xml schema using ruby but again I could not make
this work under windows.

I tried connecting to a third party library smartlib from Spirent. I
developed all of my code in linux and it worked fine, but when I tried to
move it to windows (our primary development platform) it was unable to
establish a tcp connection to the test device.

I guess maybe the answer is fill in the holes with "C". The only problem is
I have never been a "C" programmer. I came to ruby as a java programmer,
which in my particular case means I can't get very far at all with "C". I
defiantly don't have the expertise to create a working sockets
implementation.

I have tried many of the various ruby to java integration solutions, but
none of them work well enough to be useful in my situations.

What should I do? Any sage words of wisdom? Technical advice? Any help is
welcome

-- Frustrated (Michael Hale)

sounds like platform dependant issues have crept into your code or into the
3rd party code, but these are a varied set of problems. why don't you start a
thread on some (each) of them and see what the group finds - maybe you'll just
need to put in an initial effort to 'get over the hump' and then things will
be smooth sailing. make an effort to distill each problem to it's smallest
possible representative peice of code and post as much information as possible
- i'd be suprised if most of these issues were not resolvable. i'm learning
ocaml at the moment and having similar feelings to what you are having - i
think it natural when you learn a new language to get hung up on little
things. fortunately, with ruby you have a very responsive group to help you
get through it. the ocaml group is not nearly so active so i'm having to
actually rtfm ;-)

cheers.

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 
M

Mark Probert

Michael said:
Most recently I wanted to script a telnet session to automate
configuring a network device. ... I have to close the socket before I get any
output. TCL works fine.

Hi, Michael

Post the problem. There are many people on the list that can help with
various issues. Personally, I've done a fair amount of work doing
exactly what you are trying to do -- scripting network devices (Nortel
products mainly, though some others as well). I moved to Ruby from Perl
due to problems in Perl's telnet library and haven't looked back.

So, when in doubt, post the code.

Regards,

-mark.
 
L

Lennon Day-Reynolds

Michael,

I'm not sure what could be causing your networking problems on Windows
-- I certainly have had good results writing network code in Ruby, and
moving between Windows, Linux, and OS X with no changes. If you have
an example code snippet that's causing you problems on Windows, I'm
sure we could take a look at it on the list and see what's going
wrong.

Looking at the product info Spirent has for smartlib, though, it
appears that what you're trying to do may be more complex than simple
network connectivity. In that case, you may need to drop down to C
code to wrap the smartlib API for use in Ruby. Don't let that scare
you too much -- I started out with Java, too, and learned C mostly as
a gateway to C++ (and writing Ruby and Python bindings for C
libraries), but the Ruby C API is pretty easy to work with.

Lennon
 
D

Daniel Berger

Michael Hale said:
This email is sort of a public cry for help. As a language I really
love ruby and I would like nothing better than to be able to use it for
all my scripting needs, but I keep running into obstacles that keep me
from doing so.

Most recently I wanted to script a telnet session to automate
configuring a network device. The scripts were written in TCL and I
figured it would be an easy job to convert it to ruby. I didn't count
on running into so many socket issues with ruby on windows. In ruby
sockets are not responsive. I have to close the socket before I get
any output. TCL works fine.

This *could* be an issue with sockets and blocking. Jean-Francois
Nadeau posted a rather large patch for Win32 and sockets, though I
don't know whether it will make it into 1.8.2.

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/3154

I'm not sure if that's the issue or not, though. As others have
suggested, please post some code snippets to help us help you.

Regards,

Dan
 
M

Michael Hale

Thanks for all the replies and suggestions. They were very
encouraging. Here is my code:

require "net/telnet"

def telnet_session(host_name, user, password, reboot=false)
puts ""
puts "****************************\\/"
puts "started session #{host_name}\\/"
puts "****************************\\/"
puts ""

host = Net::Telnet::new("Host" => host_name,
"Output_log" => "output_log",
"Timeout" => 300)

host.login("LoginPrompt" => /Username[: ]*\z/n,
"Name" => user,
"Password" => password) {|c| print c }
begin
yield(host) if block_given?
ensure
begin
if(reboot)
host.cmd("String" => "reboot", "Prompt" => "")
else
#this should be smart enough to exit all the way out.
host.cmd("String" => "exit", "Prompt" => "")
end
rescue Exception => e
p e, 'Unable to exit/reboot.'
ensure
host.close

# for some reason the telnet library on windows
# needs some time to reset itself before I create another conneciton.
sleep 1

puts ""
puts "****************************/\\"
puts "ended session #{host_name} /\\"
puts "****************************/\\"
puts ""
end
end
end

debugLogIn = "debug"
debugPassword = "passwd"
ip = "192.168.2.54"

telnet_session(ip, debugLogIn, debugPassword) {|host|
host.cmd("con t") {|c| print c }
sleep 1
host.cmd("int m") {|c| print c }
sleep 1
host.cmd("exit") {|c| print c }
sleep 1
host.cmd("exit") {|c| print c }
}

Which gives me the following... all at once. Not in 1 second
increments as I would have hoped.

***********************************\/
started session 192.168.2.54 \/
***********************************\/


User Access Verification

Username: debug
Password:
debug> con t
config> int m
config-ifMan> exit
config> exit
debug>
***********************************/\
ended session 192.168.2.54 /\
***********************************/\

I too figured it was an issue with ruby win32 and sockets, so I tried
applying Jean's patch to my cvs version of ruby, but it still waits
until I close the socket before printing the output.
 
M

Michael Hale

I think I just figured out my own problem. I needed to do a
$stdout.flush after each command to update the console in an
interactive manner. Telnet now works as expected. Yeah!

Thanks for all the replies and suggestions. They were very
encouraging. Here is my code:

require "net/telnet"

def telnet_session(host_name, user, password, reboot=false)
puts ""
puts "****************************\\/"
puts "started session #{host_name}\\/"
puts "****************************\\/"
puts ""

host = Net::Telnet::new("Host" => host_name,
"Output_log" => "output_log",
"Timeout" => 300)

host.login("LoginPrompt" => /Username[: ]*\z/n,
"Name" => user,
"Password" => password) {|c| print c }
begin
yield(host) if block_given?
ensure
begin
if(reboot)
host.cmd("String" => "reboot", "Prompt" => "")
else
#this should be smart enough to exit all the way out.
host.cmd("String" => "exit", "Prompt" => "")
end
rescue Exception => e
p e, 'Unable to exit/reboot.'
ensure
host.close

# for some reason the telnet library on windows
# needs some time to reset itself before I create another
conneciton.
sleep 1

puts ""
puts "****************************/\\"
puts "ended session #{host_name} /\\"
puts "****************************/\\"
puts ""
end
end
end

debugLogIn = "debug"
debugPassword = "passwd"
ip = "192.168.2.54"

telnet_session(ip, debugLogIn, debugPassword) {|host|
host.cmd("con t") {|c| print c }
sleep 1
host.cmd("int m") {|c| print c }
sleep 1
host.cmd("exit") {|c| print c }
sleep 1
host.cmd("exit") {|c| print c }
}

Which gives me the following... all at once. Not in 1 second
increments as I would have hoped.

***********************************\/
started session 192.168.2.54 \/
***********************************\/


User Access Verification

Username: debug
Password:
debug> con t
config> int m
config-ifMan> exit
config> exit
debug>
***********************************/\
ended session 192.168.2.54 /\
***********************************/\

I too figured it was an issue with ruby win32 and sockets, so I tried
applying Jean's patch to my cvs version of ruby, but it still waits
until I close the socket before printing the output.
This *could* be an issue with sockets and blocking. Jean-Francois
Nadeau posted a rather large patch for Win32 and sockets, though I
don't know whether it will make it into 1.8.2.

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/3154

I'm not sure if that's the issue or not, though. As others have
suggested, please post some code snippets to help us help you.

Regards,

Dan
"OS X: because it was easier to make UNIX user-friendly than to fix
Windows"
 
A

Ara.T.Howard

I think I just figured out my own problem. I needed to do a $stdout.flush
after each command to update the console in an interactive manner. Telnet
now works as expected. Yeah!

i had a professor who used to advocate explaining bugs to one's dog. after
that we'd joke around in debugging sessions saying 'can you be my dog for a
minute...'. definitely one of the pluses of posting to the group - i've done
this an embarassing number of time myself, but at least the problem gets
solved ;-)

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 
J

Joel VanderWerf

Michael said:
I think I just figured out my own problem. I needed to do a
$stdout.flush after each command to update the console in an interactive
manner. Telnet now works as expected. Yeah!

Strange... on my system, $stdout.sync is true, and so output is
automatically flushed. Is $stdout.sync true on your system? If so I
wonder why the flush wasn't happening.
 
M

Michael Hale

On my system it is set to false. I am using 1.8.2 preview 4 if that
makes a difference.

Strange... on my system, $stdout.sync is true, and so output is
automatically flushed. Is $stdout.sync true on your system? If so I
wonder why the flush wasn't happening.
"OS X: because it was easier to make UNIX user-friendly than to fix
Windows"
 
C

Charles Mills

irb> $stdout.sync
=> true
irb> exit
$ echo 'puts $stdout.sync' | ruby
false

It depends if your in irb or not.

 
J

Joel VanderWerf

Charles said:
irb> $stdout.sync
=> true
irb> exit
$ echo 'puts $stdout.sync' | ruby
false

It depends if your in irb or not.

Ouch! Burned again by checking something in irb....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,002
Messages
2,570,261
Members
46,859
Latest member
VallieMcKe

Latest Threads

Top