Module: ASAConsole::Util
- Defined in:
- lib/asa_console/util.rb
Overview
Miscellaneous utility functions.
Constant Summary collapse
- CISCO_TIME_REGEX =
%r{ (?<hour> \d\d): (?<min> \d\d): (?<sec> \d\d) (?:\.(?<subsec> \d\d\d)\s)? (?<tz> .*?)\s (?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s)? (?<month> \w\w\w)\s (?<day> \d\d?)\s (?<year> \d\d\d\d) }x
- VERSION_EXPR_REGEX =
%r{^ (?<opr> [><=!]=?)?\s* # Comparison operator (?<major> \d+) (?: # Major release number \.(?<minor> \d+|x) (?: # Minor release number or x \((?<maint> \d+|x)\) # Maintenance release number or x )? )? $}x
Class Method Summary collapse
-
.apply_control_chars(raw) ⇒ String
Convert a string with terminal control characters to plain text as it would appear in a terminal window.
-
.parse_cisco_time(str) {|time, tz| ... } ⇒ Time
Parse the time format commonly used in various command output.
-
.version_match?(version, exprs) ⇒ Boolean
Match an ASA software version string in
x.x(x)format against one or more conditional expressions.
Class Method Details
.apply_control_chars(raw) ⇒ String
Convert a string with terminal control characters to plain text as it would appear in a terminal window.
An ASA will use backspaces and carriage returns to hide text that has
already been output to the console. For example, the ASA outputs extra
characters to hide the <--- More ---> prompt when the user presses a
key.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/asa_console/util.rb', line 41 def self.apply_control_chars(raw) output = '' raw.split("\n").each do |line| io = StringIO.new line.scan(/([^\r\x08]+|[\r\x08])/) do |m| case m[0] when "\r" io.rewind when "\x08" io.pos = io.pos - 1 else io.write(m[0]) end end output << io.string << "\n" end output.chop! unless raw.end_with?("\n") output.delete("\x00") end |
.parse_cisco_time(str) {|time, tz| ... } ⇒ Time
It is not possible to reliably evaluate the timezone string without running additional commands, so this function (optimistically) returns a UTC timestamp. See test_clock for one method of adjusting a remote timestamp to local time using "show clock" commands.
Parse the time format commonly used in various command output. This can be useful for things like extracting the configuration modification time from "show version" output or for parsing the last failover time.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/asa_console/util.rb', line 77 def self.parse_cisco_time(str) m = CISCO_TIME_REGEX.match(str) return nil unless m tz = m[:tz] year = m[:year].to_i month = m[:month] day = m[:day].to_i hour = m[:hour].to_i min = m[:min].to_i sec = m[:sec].to_i subsec = "0.#{m[:subsec]}".to_f time = Time.utc(year, month, day, hour, min, sec) + subsec time = yield(time, tz) if block_given? time end |
.version_match?(version, exprs) ⇒ Boolean
Match an ASA software version string in x.x(x) format against one or
more conditional expressions.
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/asa_console/util.rb', line 100 def self.version_match?(version, exprs) ver = [] version_match_parse(version) { |_opr, pattern| ver = pattern } exprs.each do |e| version_match_parse(e) do |opr, pattern| return false unless version_match_compare(opr, ver, pattern) end end true end |