Forum: Ruby Getting the result of a command with Net::Telnet

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ronald F. (Guest)
on 2007-01-08 12:41
(Received via mailing list)
Please have a look at the following simple Ruby statement (tn is
a Net::Telnet object):

tn.login(....)
tn.cmd({'String' => 'uname -n', 'Match' => /root@sb.*# */}) {
  puts "string received:"
  puts str
}

When I run this, I see the result:

string received:
u
string received:
name -n
sb1-1
root@sb1-1:~#

That is, I get the response in two pieces: First the first character
only ("u"), then the rest.

Is there a way to get everything in one piece?

Or might there even be a clever way, which would let me see just
the result of the uname -n command ("sb1-1"), but neither the echoing
of "uname -n", nor the subsequent prompt ("root@sb1-1:~#")? Of
course I can easily extract the required information by myself,
but this seems such a common thing to do (send a command via telnet
and analyze what comes back) that I wonder whether there existed
an easier way to do it.

Ronald
Rodrigo B. (Guest)
on 2007-01-08 22:36
Ronald F. wrote:
> Please have a look at the following simple Ruby statement (tn is
> a Net::Telnet object):
>
> tn.login(....)
> tn.cmd({'String' => 'uname -n', 'Match' => /root@sb.*# */}) {
>   puts "string received:"
>   puts str
> }


tn.login(....)
str = tn.cmd('uname -n').chop
p str

Does it works for you ?
This topic is locked and can not be replied to.