From c718e46be7eecbd098152e29922c42e9f5e148e1 Mon Sep 17 00:00:00 2001
From: Marno van der Molen <marnovandermolen@acknowledge.nl>
Date: Thu, 18 May 2017 10:15:38 +0200
Subject: Merge u/panzerfly's changes for Cisco ASA multiple context support

---
 lib/oxidized/model/asa.rb | 78 +++++++++++++++++++++++++++++++++++------------
 1 file changed, 59 insertions(+), 19 deletions(-)

(limited to 'lib/oxidized/model')

diff --git a/lib/oxidized/model/asa.rb b/lib/oxidized/model/asa.rb
index 038dd6b..8a2ead8 100644
--- a/lib/oxidized/model/asa.rb
+++ b/lib/oxidized/model/asa.rb
@@ -20,6 +20,17 @@ class ASA < Oxidized::Model
     cfg
   end
 
+  cmd :secret do |cfg|
+    cfg.gsub! /enable password (\S+) (.*)/, 'enable password <secret hidden> \2'
+    cfg.gsub! /username (\S+) password (\S+) (.*)/, 'username \1 password <secret hidden> \3'
+    cfg
+  end
+
+  # check for multiple contexts
+  cmd 'show mode' do |cfg|
+    @is_multiple_context = cfg.include? 'multiple'
+  end
+
   cmd 'show version' do |cfg|
     # avoid commits due to uptime / ixo-router01 up 2 mins 28 secs / ixo-router01 up 1 days 2 hours
     cfg = cfg.each_line.select { |line| not line.match /(\s+up\s+\d+\s+)|(.*days.*)/ }
@@ -31,25 +42,11 @@ class ASA < Oxidized::Model
     comment cfg
   end
 
-  cmd 'more system:running-config' do |cfg|
-    cfg = cfg.each_line.to_a[3..-1].join
-    cfg.gsub! /^: [^\n]*\n/, ''
-    # backup any xml referenced in the configuration.
-    anyconnect_profiles = cfg.scan(Regexp.new('(\sdisk0:/.+\.xml)')).flatten
-    anyconnect_profiles.each do |profile|
-  	  cfg << (comment profile + "\n" )
-   	  cmd ("more" + profile) do |xml|
-	      cfg << (comment xml)
-	    end
-    end
-    # if DAP is enabled, also backup dap.xml
-    if cfg.rindex(/dynamic-access-policy-record\s(?!DfltAccessPolicy)/)
-   	  cfg << (comment "disk0:/dap.xml\n")
-      cmd "more disk0:/dap.xml" do |xml|
-        cfg << (comment xml)
-      end
-    end
-    cfg
+  post do
+    if @is_multiple_context
+      multiple_context
+    else
+      single_context
   end
 
   cfg :ssh do
@@ -62,5 +59,48 @@ class ASA < Oxidized::Model
     post_login 'terminal pager 0'
     pre_logout 'exit'
   end
+  
+  def single_context
+      # Single context mode
+      cmd 'more system:running-config' do |cfg|
+        cfg = cfg.each_line.to_a[3..-1].join
+        cfg.gsub! /^: [^\n]*\n/, ''
+        # backup any xml referenced in the configuration.
+        anyconnect_profiles = cfg.scan(Regexp.new('(\sdisk0:/.+\.xml)')).flatten
+        anyconnect_profiles.each do |profile|
+            cfg << (comment profile + "\n" )
+            cmd ("more" + profile) do |xml|
+              cfg << (comment xml)
+            end
+        end
+        # if DAP is enabled, also backup dap.xml
+        if cfg.rindex(/dynamic-access-policy-record\s(?!DfltAccessPolicy)/)
+            cfg << (comment "disk0:/dap.xml\n")
+            cmd "more disk0:/dap.xml" do |xml|
+              cfg << (comment xml)
+            end
+        end
+        cfg
+      end
+  end
+
+  def multiple_context
+      # Multiple context mode
+      cmd 'changeto system' do |cfg|
+        cmd 'show running-config' do |systemcfg|
+          allcfg = "\n\n" + systemcfg + "\n\n"
+          contexts = systemcfg.scan(/^context (\S+)$/)
+          files = systemcfg.scan(/config-url (\S+)$/)
+          contexts.each_with_index do |cont, i|
+            allcfg = allcfg + "\n\n----------========== [ CONTEXT " + cont.join(" ") + " FILE " + files[i].join(" ") + " ] ==========----------\n\n"
+            cmd "more " + files[i].join(" ") do |cfgcontext|
+              allcfg = allcfg + "\n\n" + cfgcontext
+            end
+          end
+          cfg = allcfg
+        end
+        cfg
+      end
+  end
 
 end
-- 
cgit v1.2.3