Net::Telnet Question

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.

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

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