# Net::Telnet Question

**URL:** <https://www.ruby-forum.com/t/net-telnet-question/73677>\
**Category:** Ruby\
**Created:** [October 26, 2006, 4:39am UTC](https://www.ruby-forum.com/t/net-telnet-question/73677 "2006-10-26T04:39:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mercan01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mercan01](https://www.ruby-forum.com/u/mercan01)\
**Post date:** [October 26, 2006, 4:39am UTC](https://www.ruby-forum.com/t/net-telnet-question/73677/1 "2006-10-26T04:39:17Z")

</div>

So I’ve spent the last couple or so writing a rather trivial little ruby  
script to automate enabling and disabling an access list, mostly to  
become  
more familiar with Net::Telnet. I’ve run into an interesting issue I  
can’t  
quite figure out.

Here’s the code:

require ‘net/telnet’  
login\_password = “password”  
enable\_password = “password”  
host = “10.1.1.1”

cisco\_device = Net::Telnet::new(“Host” =\> host,  
“Timeout” =\> 10,  
“Prompt” =\> /[\w+]\*[\>#]\z/,  
“Output\_log” =\> ‘output.log’)

cisco\_device.puts(login\_password) { |c| print c } #login  
cisco\_device.cmd(“terminal length 0”) { |c| print c } #stops the more  
prompts

cisco\_device.cmd(“enable”) { |c| print c } #enter enable mode  
cisco\_device.puts(enable\_password)

#configure ip access-group  
cisco\_device.cmd(“configure terminal”) { |c| print c }  
cisco\_device.cmd(“interface Ethernet 0”) { |c| print c }  
cisco\_device.cmd(“ip access-group 10 in”) { |c| print c }  
#cisco\_device.cmd(“exit”) { |c| print c } #-----Confusing Line-----

cisco\_device.close

As the code is there, here’s the ouput in both the output log and the  
screen.

User Access Verification

Password:  
Router\>terminal length 0  
Router\>ena  
Password:  
Router#configure terminal  
Enter configuration commands, one per line. End with CNTL/Z.  
Router(config)#interface Ethernet 0  
Router(config-if)#

This also has the side effect of not producing the effect I desire (in  
this  
case the access list is not applied)

If I uncomment out the last line, the series of commands works as  
intended  
(the access list is applied), and the following output is displayed:

User Access Verification

Password:  
Router\>terminal length 0  
Router\>enable  
Password:  
Router#configure terminal  
Enter configuration commands, one per line. End with CNTL/Z.  
Router(config)#interface Ethernet 0  
Router(config-if)#ip access-group 10 in  
Router(config-if)#

I’ve tried this code on several different makes of routers (different  
IOS  
versions) an on OS X and Windows XP, so I’m sure it must be something  
I’m  
doing wrong. That last cmd() also works with other IOS commands, so it’s  
not  
something special with exit.

I’ve been digging through the docs and google, and I’ve tried the  
different  
modes for telnets and I even spent some time experimenting with  
different  
values for prompt, but I don’t think that’s the issue.

Obviously I can get the script to work, but I’m more curious as to why  
it’s  
requiring that last command to be run. I’d appreciate any insight anyone  
can  
offer.

Thanks,

William R.

---

<div class="post-metadata">

**Author:** ![mercan01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mercan01](https://www.ruby-forum.com/u/mercan01)\
**Post date:** [October 26, 2006, 5:04pm UTC](https://www.ruby-forum.com/t/net-telnet-question/73677/2 "2006-10-26T17:04:09Z")

</div>

On Oct 25, 2006, at 22:37, William R. wrote:

> So I’ve spent the last couple or so writing a rather trivial little  
> ruby  
> script to automate enabling and disabling an access list, mostly to  
> become  
> more familiar with Net::Telnet. I’ve run into an interesting issue  
> I can’t  
> quite figure out.

…

> #cisco\_device.cmd(“exit”) { |c| print c } #-----Confusing Line-----
> 
> cisco\_device.close

…

> If I uncomment out the last line, the series of commands works as  
> intended  
> (the access list is applied), and the following output is displayed:
> 
> …

> modes for telnets and I even spent some time experimenting with  
> different  
> values for prompt, but I don’t think that’s the issue.
> 
> Obviously I can get the script to work, but I’m more curious as to  
> why it’s  
> requiring that last command to be run. I’d appreciate any insight  
> anyone can  
> offer.

I haven’t used Net::Telnet but my guess would be that it’s a timing /  
flushing problem. Have you tried putting delays (sleep(1)) between  
commands?

Ben

---

<div class="post-metadata">

**Author:** ![mercan01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3e96dc/32.png) [@mercan01](https://www.ruby-forum.com/u/mercan01)\
**Post date:** [October 26, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/net-telnet-question/73677/3 "2006-10-26T17:10:09Z")

</div>

Actually, I had not. But I just tried and that seemed to have done the  
trick! Thanks for your help.
