Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

telnetting cisco routers/devices

3 views
Skip to first unread message

"Peña, Botp"

unread,
Jul 3, 2005, 11:45:34 PM7/3/05
to
Hi ruby telnet gurus,

I'm having difficulty telnetting properly cisco devices. I googled the net
and found http://nettelnetcisco.sourceforge.net/docs.html. But this is perl.
Any kind soul doing something like this in ruby and would love to share?

Thanks in advance -botp


jm

unread,
Jul 4, 2005, 3:43:15 AM7/4/05
to
Just a dumb question to begin with - what have you tried? Net::Telnet?
Have you tried ruby's expect? (Had forgotten about this until I was
flipping through pickaxe 2 today for something else).

J.

"Peña, Botp"

unread,
Jul 4, 2005, 5:04:49 AM7/4/05
to
jm [mailto:je...@ghostgun.com] wrote:
#
#Just a dumb question to begin with - what have you tried? Net::Telnet?
#Have you tried ruby's expect? (Had forgotten about this until I was
#flipping through pickaxe 2 today for something else).
#
#J.

only Net::Telnet.

Net::Telnet works great in managing linux boxes.

but on cisco, the behavior is weird.. i mean of cisco not telnet. The perl
link below seems to address all my issues but, it is in perl. I'm sorry i'm
a bit bias for ruby..

thanks and kind regards -botp

btw, where can i find expect?? sorry if this question is dumber than you
think but i cannot find expect..


#
#On 04/07/2005, at 1:45 PM, Peña, Botp wrote:
#
#> Hi ruby telnet gurus,
#>
#> I'm having difficulty telnetting properly cisco devices. I
#googled the
#> net
#> and found http://nettelnetcisco.sourceforge.net/docs.html.
#But this is
#> perl.
#> Any kind soul doing something like this in ruby and would love to
#> share?
#>
#> Thanks in advance -botp
#>
#>
#>
#
#


Ghislain Mary

unread,
Jul 4, 2005, 12:58:17 PM7/4/05
to
Hi,

Here is what I did to access some information on a Cisco Access Point
using telnet:


require 'net/telnet'


=begin rdoc
This class consists of using a telnet connection to get some information
from a Cisco Access Point.

It has the ability to get some information about the STAs associations,
some global statistics about the wireless interface of the AP, some
statistics per QoS priority, and some statistics per STA.
=end
class CiscoAP

# The parameters of the AP about QoS: CWMin, CWMax, TXOP...
attr_reader :qos_params

# Create a new CiscoAP instance.
def initialize(host, login, password)
@host, @login, @password = host, login, password
connect
@qos_params = qos_information
end

# Close the connection to the telnet server.
def close
@connection.close
end

# Clear all the statistics.
def clear_all_stats
clear_global_stats
clear_interface_stats
end

# Clear the global statistics about the Dot11Radio 0 interface.
def clear_global_stats
telnet_exec('clear dot11 statistics Dot11Radio 0')

true
end

# Clear the interface statistics about the Dot11Radio 0 interface.
def clear_interface_stats
telnet_exec('clear interface Dot11Radio 0')

true
end

# Get the global statistics about the Dot11Radio 0 interface.
def global_stats
telnet_exec('show interfaces Dot11Radio 0 statistics')
end

=begin rdoc
Get a Hash containing statistics for a particular STA knowing its
MAC address.

Output example:
{
:rts_retries => 0,
:packets_in => 76274,
:ip_address => "10.0.1.3",
:packets_out => 157063,
:state => "Assoc",
:bytes_in => 24183047,
:mac_address => "0040.96a6.802c",
:supported_rates => [6.0, 9.0, 18.0, 24.0, 36.0],
:bytes_out => 165501061,
:data_retries => 7604,
:signal_strength => -39,
:current_rate => 36.0
}
=end
def information_about_sta(mac_address)
result = telnet_exec(
"show dot11 associations #{to_cisco_mac_address mac_address }")

if result.size > 0
output = Hash.new
(md = result.match(/Address\s*:\s*(.{4}\..{4}\..{4})/)) &&
(output[:mac_address] = md[1])
(md = result.match(/IP Address\s*:\s*(\d+\.\d+\.\d+\.\d+)/)) &&
(output[:ip_address] = md[1])
(md = result.match(/State\s*:\s*(\w+)/)) && (output[:state] =
md[1])
(md = result.match(/Current Rate\s*:\s*(.*)?\s/)) &&
(output[:current_rate] = md[1].to_f)
(md = result.match(/Supported Rates\s*:\s*(.*)$/)) &&
(output[:supported_rates] = md[1].split.collect { |elem|
elem.to_f })
(md = result.match(/Signal Strength\s*:\s*(-\d+)\s/)) &&
(output[:signal_strength] = md[1].to_i)
(md = result.match(/Packets Input\s*:\s*(\d+)\s/)) &&
(output[:packets_in] = md[1].to_i)
(md = result.match(/Packets Output\s*:\s*(\d+)\s/)) &&
(output[:packets_out] = md[1].to_i)
(md = result.match(/Bytes Input\s*:\s*(\d+)\s/)) &&
(output[:bytes_in] = md[1].to_i)
(md = result.match(/Bytes Output\s*:\s*(\d+)\s/)) &&
(output[:bytes_out] = md[1].to_i)
(md = result.match(/Data Retries\s*:\s*(\d+)\s/)) &&
(output[:data_retries] = md[1].to_i)
(md = result.match(/RTS Retries\s*:\s*(\d+)\s/)) &&
(output[:rts_retries] = md[1].to_i)

output
end
end

=begin rdoc
Get QoS information about the AP: the values for CWmin, CWmax,
AIFSn and TXOP.

Output example:
{
:BACKGROUND => { :cwmin => 4,
:cwmax => 10,
:aifsn => 7,
:txop => 0 },
:VIDEO => { :cwmin => 3,
:cwmax => 4,
:aifsn => 2,
:txop => 3008 },
:BESTEFFORT => { :cwmin => 4,
:cwmax => 10,
:aifsn => 3,
:txop => 0 },
:VOICE => { :cwmin => 2,
:cwmax => 3,
:aifsn => 2,
:txop => 1504 }
}
=end
def qos_information
result = telnet_exec('show controllers | begin Configured Access
Class Parameters')
result = result.split("\n")[1..4].join("\n")

if result.size > 0
output = Hash.new
result.each_line do |line|
priority = line.match(/\s*(\w*)\s*:/)[1].upcase.to_sym
output[priority] = {
:cwmin => line.match(/cw-min\s+(\d+)/)[1].to_i,
:cwmax => line.match(/cw-max\s+(\d+)/)[1].to_i,
:aifsn => line.match(/fixed-slot\s+(\d+)/)[1].to_i,
:txop => line.match(/txop\s+(\d+)/)[1].to_i
}
end
end

output
end

private

# Connect to the telnet server.
def connect
@connection = Net::Telnet.new(
"Host" => @host,
"Timeout" => false,
"Prompt" => /.*\#/
) #{ |str| print str }
@connection.login({
"Name" => @login,
"Password" => @password,
"LoginPrompt" => /Username:/
}) #{ |str| print str }

# Unset the terminal length.
@connection.cmd('terminal length 0')
end

# Execute a set of telnet commands.
def telnet_exec(commands)
result = ''
commands = [commands] if commands.class != Array

# Execute each command.
commands.each do |command|
result += @connection.cmd(command)
end

# Clean the result a bit.
result.gsub!(/^.*#/, '')
commands.each do |command|
result.gsub!(/#{command}/, '')
end
result.gsub!(/(\n)+/, "\n")
result.gsub!(/\|\n/, '')
result.strip!

result
end

# Converts an ordinary MAC address to a Cisco MAC address.
# XX:XX:XX:XX:XX:XX => XXXX.XXXX.XXXX
def to_cisco_mac_address(mac_address)
mac_address =~ /(\w\w):(\w\w):(\w\w):(\w\w):(\w\w):(\w\w)/
"#$1#$2.#$3#$4.#$5#$6"
end

end


Hope that helps.

Regards,

Ghislain

"Peña, Botp"

unread,
Jul 4, 2005, 11:16:49 PM7/4/05
to
Ghislain Mary [mailto:nos...@lunacymaze.org] said:

#Here is what I did to access some information on a Cisco Access Point
#using telnet:

[skipped useful/c-oo-l code/sample]

Hi Ghislain.

Thanks for the very helpful code. I will put it in my archive. I hope you
don't mind.

After scrutinizing your code, I got an important hint. I think i screwed up
my prompt option. The prompt works great in linux, but i failed to modify it
on other devices... My script seems to work now.. hopefully on all..

sample script and run:

--------------

>cat t.rb

time_start = Time::new

require 'net/telnet'
host = "10.1.1.1"
tn = Net::Telnet::new( \
"Host" => host, # default: "localhost"
"Port" => 23, # default: 23
"Binmode" => false, # default: false
"Output_log" => "output_log", # default: nil (no output)
"Dump_log" => "dump_log", # default: nil (no output)
"Prompt" => /[$%#>]/, # default: /[$%#>] \z/n
"Telnetmode" => true, # default: true
"Timeout" => 5, # default: 10
"Waittime" => 0 # default: 0
)
tn.waitfor(/assword:/) {|c| print c}
tn.puts "testpassword"
tn.cmd("show version") { |c| print c }
tn.cmd("quit") {|c| print c}
tn.close

puts
puts "#{Time.now - time_start} seconds"

>t


User Access Verification

Password:
dev01>show version
Cisco Internetwork Operating System Software
IOS (tm) 1600 Software (C1600-Y-M), Version 12.0(9), RELEASE SOFTWARE (fc1)
Copyright (c) 1986-2000 by cisco Systems, Inc.
Compiled Tue 25-Jan-00 00:11 by bettyl
Image text-base: 0x02005000, data-base: 0x0246E5D8

ROM: System Bootstrap, Version 12.0(3)T, RELEASE SOFTWARE (fc1)
ROM: 1600 Software (C1600-RBOOT-R), Version 12.0(3)T, RELEASE SOFTWARE
(fc1)

dev01 uptime is 1 day, 11 hours, 14 minutes
System restarted by power-on
System image file is "flash:c1600-y-mz.120-9"

cisco 1601 (68360) processor (revision C) with 7680K/512K bytes of memory.
Processor board ID 23082938, with hardware revision 00000003
Bridging software.
X.25 software, Version 3.0.0.
1 Ethernet/IEEE 802.3 interface(s)
1 Serial(sync/async) network interface(s)
System/IO memory with parity disabled
8192K bytes of DRAM onboard
System running from RAM
7K bytes of non-volatile configuration memory.

dev01>
0.531 seconds
----------------

works great!

Thanks again Ghislain.

kind regards -botp


0 new messages