Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #3055 > unrolled thread
| Started by | "Eric T." <erictetz@gmail.com> |
|---|---|
| First post | 2011-04-17 02:10 -0500 |
| Last post | 2011-04-19 12:12 -0500 |
| Articles | 12 — 6 participants |
Back to article view | Back to comp.lang.ruby
Telnet "More?" "Eric T." <erictetz@gmail.com> - 2011-04-17 02:10 -0500
Re: Telnet "More?" Christopher Dicely <cmdicely@gmail.com> - 2011-04-17 09:56 -0500
Re: Telnet "More?" "Eric T." <erictetz@gmail.com> - 2011-04-17 20:00 -0500
Re: Telnet "More?" 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-17 17:47 -0500
Re: Telnet "More?" "Eric T." <erictetz@gmail.com> - 2011-04-17 19:48 -0500
Re: Telnet "More?" Christopher Dicely <cmdicely@gmail.com> - 2011-04-18 00:37 -0500
Re: Telnet "More?" 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-19 12:21 -0500
Re: Telnet "More?" "mouser" <invalid@invalid.com> - 2011-04-18 04:18 +0000
Re: Telnet "More?" "mouser" <invalid@invalid.com> - 2011-04-18 04:22 +0000
Re: Telnet "More?" Brian Candler <b.candler@pobox.com> - 2011-04-19 04:18 -0500
Re: Telnet "More?" "mouser" <invalid@invalid.com> - 2011-04-19 16:36 +0000
Re: Telnet "More?" Markus Fischer <markus@fischer.name> - 2011-04-19 12:12 -0500
| From | "Eric T." <erictetz@gmail.com> |
|---|---|
| Date | 2011-04-17 02:10 -0500 |
| Subject | Telnet "More?" |
| Message-ID | <0bc1be8ec871e5fbad7753c695247ddc@ruby-forum.com> |
I'm trying to use the telnet library. I don't know Ruby AT ALL
(evaluating it side by side with Python to see which is going to be best
for my admin chores; this is my very first script), and this has got me
stumped:
require 'net/telnet'
t = Net::Telnet::new(
'Host' => 'somehost.com',
'Prompt' => /:.*>/,
)
out = lambda do |c| print c end
t.login('someusername', 'somepassword', &out)
t.cmd('dir', &out)
t.cmd('dir', &out)
The second dir command hangs with the server responding "More?" Here's
the tail end of the output log, after the first dir command:
...
04/13/2011 04:21 PM <DIR> Searches
04/13/2011 04:21 PM <DIR> Videos
04/16/2011 12:54 AM 6,558 _viminfo
1 File(s) 6,558 bytes
13 Dir(s) 279,022,981,120 bytes free
C:\Users\foo>dir
More?
And it just hangs there.
Full logs here (names changed to protect the innocent):
http://tetzfiles.com/temp/output_log
http://tetzfiles.com/temp/dump_log
This is a bitch to Google ("?" is ignored), but I found someone else
asking the same question on Stackoverflow, but he got no response
(http://stackoverflow.com/questions/3450942/ruby-telnet-lib-weird-response).
You guys could help both of us out. :)
------------------------------------------------------------------
In a slightly unrelated question, I don't understand why I have to do
this:
out = lambda do |c| print c end
t.login('someusername', 'somepassword', &out)
t.cmd('dir', &out)
t.cmd('dir', &out)
Rather than simply this:
t.login('someusername', 'somepassword', &print)
t.cmd('dir', &print)
t.cmd('dir', &print)
Seems kinda pointless to make a function which does nothing but pass
it's arguments unaltered to another function.
--
Posted via http://www.ruby-forum.com/.
[toc] | [next] | [standalone]
| From | Christopher Dicely <cmdicely@gmail.com> |
|---|---|
| Date | 2011-04-17 09:56 -0500 |
| Message-ID | <BANLkTim638b6r89U_xYninSyYUab5kLfew@mail.gmail.com> |
| In reply to | #3055 |
On Sun, Apr 17, 2011 at 12:10 AM, Eric T. <erictetz@gmail.com> wrote:
> I'm trying to use the telnet library. I don't know Ruby AT ALL
> (evaluating it side by side with Python to see which is going to be best
> for my admin chores; this is my very first script), and this has got me
> stumped:
>
> require 'net/telnet'
> t = Net::Telnet::new(
> 'Host' => 'somehost.com',
> 'Prompt' => /:.*>/,
> )
> out = lambda do |c| print c end
> t.login('someusername', 'somepassword', &out)
> t.cmd('dir', &out)
> t.cmd('dir', &out)
>
> The second dir command hangs with the server responding "More?" Here's
> the tail end of the output log, after the first dir command:
>
> ...
> 04/13/2011 04:21 PM <DIR> Searches
> 04/13/2011 04:21 PM <DIR> Videos
> 04/16/2011 12:54 AM 6,558 _viminfo
> 1 File(s) 6,558 bytes
> 13 Dir(s) 279,022,981,120 bytes free
>
> C:\Users\foo>dir
> More?
>
> And it just hangs there.
It looks like the server is sending something other than the default
prompt and expecting a response; you probably need to handle that
case.
If you use telnet directly (from the console, not a script) and do the
same sequence of commands, what happens?
>
> In a slightly unrelated question, I don't understand why I have to do
> this:
>
> out = lambda do |c| print c end
> t.login('someusername', 'somepassword', &out)
> t.cmd('dir', &out)
> t.cmd('dir', &out)
>
> Rather than simply this:
>
> t.login('someusername', 'somepassword', &print)
> t.cmd('dir', &print)
> t.cmd('dir', &print)
>
> Seems kinda pointless to make a function which does nothing but pass
> it's arguments unaltered to another function.
Ruby is unlike Python in that, while it has functions that are objects
(e.g., Proc objects), methods (including Kernel#print) are not
functions (or objects). So you are creating a function that performs a
method call against the object that is the current value of "self"
passing that method the same arguments that the function was called
with, not creating a function that calls a function with the same
arguments the first function is called with.
You could capture the method as a callable, function-like object with
self.method(:print) instead of an explicit lambda declaration, which
makes the difference more clear, but is somewhat less concise in this
case.
[toc] | [prev] | [next] | [standalone]
| From | "Eric T." <erictetz@gmail.com> |
|---|---|
| Date | 2011-04-17 20:00 -0500 |
| Message-ID | <56feb3a19cdca1e267e47ba4161d62d8@ruby-forum.com> |
| In reply to | #3063 |
Christopher Dicely wrote in post #993345:
> It looks like the server is sending something other than the default
> prompt and expecting a response; you probably need to handle that
> case.
Yes, but I'm wondering *why* this happens with the Ruby client. It's
hard as hell to search the web for the "More?" response, because search
engines ignore the "?" leaving you search for "More" (pun not indented,
but apropos).
> If you use telnet directly (from the console, not a script) and do the
> same sequence of commands, what happens?
It works fine. It also works fine using the Python client.
But so far, in my extremely superficial comparison of the languages, I'm
liking Ruby better. I like that regex is a first class citizen (/f?o/i
is a hell of a lot better than re.compile('f?o', re.IGNORECASE)), and I
like the implementation of Ruby's telnet library better (passing blocks
to receive output is a nice touch). Unfortunately, a straight-forward
use of the library is provoking a weird response from the server.
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-04-17 17:47 -0500 |
| Message-ID | <4c00eb93cb86eea75501e223526dc08b@ruby-forum.com> |
| In reply to | #3055 |
Eric T. wrote in post #993311:
> In a slightly unrelated question, I don't understand why I have to do
> this:
>
> out = lambda do |c| print c end
> t.login('someusername', 'somepassword', &out)
> t.cmd('dir', &out)
> t.cmd('dir', &out)
>
> Rather than simply this:
>
> t.login('someusername', 'somepassword', &print)
> t.cmd('dir', &print)
> t.cmd('dir', &print)
>
> Seems kinda pointless to make a function...
Which function are you referring to??
> which does nothing but pass
> it's arguments unaltered to another function.
Yes, that would be pointless, but where does such a function appear in
your code?
In your code, the & does two things:
1) It calls to_proc() on the specified object. print() is a method of
the Kernel module, and Kernel doesn't define to_proc().
2) It tells ruby to use the Proc object as a block.
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | "Eric T." <erictetz@gmail.com> |
|---|---|
| Date | 2011-04-17 19:48 -0500 |
| Message-ID | <9ad7422f9e7881a37eb06b255a50f31e@ruby-forum.com> |
| In reply to | #3074 |
7stud -- wrote in post #993403: > Yes, that would be pointless, but where does such a function appear in > your code? That would be (I thought) the 'out' lambda, but as Christopher explained, print is actually a *method* (not obvious if you don't know Ruby), so it makes more sense that a closure is required. In Lua, for instance, print is a global function, so making a closure 'function out(...) print(...) end' would be utterly superfluous; anything you could pass 'out' to you could just pass 'print' to. It still strikes me odd that Kernel#print is not static (does it modify Kernel state?), or if it *is* static that you can't just pass it as-is to anything that requires a callable. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Christopher Dicely <cmdicely@gmail.com> |
|---|---|
| Date | 2011-04-18 00:37 -0500 |
| Message-ID | <BANLkTi=idLgKyOxzo7AUWORbDmjK0CyjuA@mail.gmail.com> |
| In reply to | #3076 |
On Sun, Apr 17, 2011 at 5:48 PM, Eric T. <erictetz@gmail.com> wrote: > > It still strikes me odd that Kernel#print is not static (does it modify > Kernel state?), or if it *is* static that you can't just pass it as-is > to anything that requires a callable. Methods (like blocks) aren't objects, though (like blocks) they can be reified as objects, consequently methods, as such, can't be passed to other methods. (One side effect -- or possibly motivating reason -- for this is that Ruby methods can be called without parens, including no-arg methods. If methods were objects accessible by the method name, reference to the method as an object would be -- assuming the most obvious syntax -- ambiguous with a method call without parens.) And Ruby doesn't have static methods the way Java does. While Ruby does have class/module methods, they are just instance methods on an object (because in Ruby, classes and modules are objects.)
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-04-19 12:21 -0500 |
| Message-ID | <7d2f1e2e2a48773ca3137c3e39cbc5d7@ruby-forum.com> |
| In reply to | #3076 |
Eric T. wrote in post #993412: > 7stud -- wrote in post #993403: >> Yes, that would be pointless, but where does such a function appear in >> your code? > > That would be (I thought) the 'out' lambda, Ahh, I see. > but as Christopher > explained, print is actually a *method* (not obvious if you don't know > Ruby), print() is a method in python3 as well. > so it makes more sense that a closure is required. > > In Lua, for instance, print is a global function, print() acts like a global function in ruby. Kernel is "mixed into" the Object class, from which all objects inherit, which means any object can call the methods defined in Kernel. And because the methods defined in Kernel are private, you cannot specify a receiver when calling the methods, and therefore calling a Kernel method looks like calling a global method in any other language. > or if it *is* static that you can't just pass it as-is > to anything that requires a callable. In ruby, the name of a method is not a reference to the method. Rather, the name of the method serves as a method call with no arguments. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | "mouser" <invalid@invalid.com> |
|---|---|
| Date | 2011-04-18 04:18 +0000 |
| Message-ID | <qYOqp.768$_P6.0@newsfe05.iad> |
| In reply to | #3055 |
On 16-Apr-2011, "Eric T." <erictetz@gmail.com> wrote:
> This is a bitch to Google ("?" is ignored), but I found someone else
> asking the same question on Stackoverflow, but he got no response
> (http://stackoverflow.com/questions/3450942/ruby-telnet-lib-weird-response).
> You guys could help both of us out. :)
I was able to reproduce your problem in Ruby 1.9.2 but not 1.8.7, against an
MS telnet server instance. It appears to be some kind of character encoding
problem that prevents certain telnet options from being set to true
(@telnet_options["BINARY"] and @telnet_options["SGA"]). I haven't tried any
other telnet servers, so it may not manifest itself against every server,
and granted MS Telnet services are known to be pretty bad. I may look into
it further if I have the time, but you might want to continue your
experiments in 1.8.7 for now. For anyone else interested in looking at
this, you might look at some of the string.gsub calls in the preprocess
instance method of Net::Telnet, I suspect this might be part of the problem.
[toc] | [prev] | [next] | [standalone]
| From | "mouser" <invalid@invalid.com> |
|---|---|
| Date | 2011-04-18 04:22 +0000 |
| Message-ID | <V%Oqp.26631$sS4.14591@newsfe11.iad> |
| In reply to | #3081 |
On 17-Apr-2011, "mouser" <invalid@invalid.com> wrote: > @telnet_options["BINARY"] and @telnet_options["SGA"] I should have written @telnet_option.... not @telnet_options
[toc] | [prev] | [next] | [standalone]
| From | Brian Candler <b.candler@pobox.com> |
|---|---|
| Date | 2011-04-19 04:18 -0500 |
| Message-ID | <c90962a3c57ccedb81a073a7013f756c@ruby-forum.com> |
| In reply to | #3055 |
Eric T. wrote in post #993311:
> t.cmd('dir', &out)
> t.cmd('dir', &out)
t.cmd is really shorthand for t.puts followed by t.waitfor
So you could try something like this:
def cmd_or_more(str, &blk)
t.puts str
loop do
res = t.waitfor(/:.*>|More\?/, &blk)
break if res =~ /:.*>/
t.puts ""
end
end
t.cmd_or_more('dir', &out)
t.cmd_or_more('dir', &out)
Of course, if the question is really "why does Microsoft hang with a
'More?' prompt when dir is issued a second time", then this isn't really
the right solution.
It would be better if you could issue some command to the CLI to say "no
pager" - like "term len 0" on a Cisco router.
Regards,
Brian.
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | "mouser" <invalid@invalid.com> |
|---|---|
| Date | 2011-04-19 16:36 +0000 |
| Message-ID | <ZRirp.31333$zn.21162@newsfe19.iad> |
| In reply to | #3151 |
On 19-Apr-2011, Brian Candler <b.candler@pobox.com> wrote: > Of course, if the question is really "why does Microsoft hang with a > 'More?' prompt when dir is issued a second time", then this isn't really > the right solution. But I found that his code *does* work in 1.8.7 but not in 1.9.2, so it's not just Microsoft's fault. It looked to me to be a difference in character encodings between the two versions of Ruby causing some issues with regex matching in the preprocess instance method.
[toc] | [prev] | [next] | [standalone]
| From | Markus Fischer <markus@fischer.name> |
|---|---|
| Date | 2011-04-19 12:12 -0500 |
| Message-ID | <4DADC275.9090708@fischer.name> |
| In reply to | #3177 |
On 19.04.2011 18:40, mouser wrote: > On 19-Apr-2011, Brian Candler <b.candler@pobox.com> wrote: > >> Of course, if the question is really "why does Microsoft hang with a >> 'More?' prompt when dir is issued a second time", then this isn't really >> the right solution. > > But I found that his code *does* work in 1.8.7 but not in 1.9.2, so it's not > just Microsoft's fault. It looked to me to be a difference in character > encodings between the two versions of Ruby causing some issues with regex > matching in the preprocess instance method. Is it maybe a similar issue I faced with Sinatra recently [1]? In 1.9 strings coming over network are in ASCII-8BIT usually and you've to convert them to char charset to work with them. HTH, - Markus [1] http://www.ruby-forum.com/topic/476119#989522
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web