# Getting the result of a command with Net::Telnet

**URL:** <https://www.ruby-forum.com/t/getting-the-result-of-a-command-with-net-telnet/81362>\
**Category:** Ruby\
**Created:** [January 8, 2007, 11:41am UTC](https://www.ruby-forum.com/t/getting-the-result-of-a-command-with-net-telnet/81362 "2007-01-08T11:41:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [January 8, 2007, 11:41am UTC](https://www.ruby-forum.com/t/getting-the-result-of-a-command-with-net-telnet/81362/1 "2007-01-08T11:41:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [January 8, 2007, 9:36pm UTC](https://www.ruby-forum.com/t/getting-the-result-of-a-command-with-net-telnet/81362/2 "2007-01-08T21:36:14Z")

</div>

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 ?
