Module: Chef::Knife::CiscoAsaBase

Included in:
CiscoAsaHostAdd, CiscoAsaHostRemove
Defined in:
lib/chef/knife/cisco_asa_base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/chef/knife/cisco_asa_base.rb', line 14

def self.included(includer)
  includer.class_eval do

    deps do
      require 'readline'
      require 'chef/json_compat'
    end

    unless defined? $default
      $default = Hash.new
    end

    option :cisco_asa_enable_password,
      :short => "-E PASSWORD",
      :long => "--cisco-asa-enable-password PASSWORD",
      :description => "Enable password for Cisco ASA"

    option :cisco_asa_hostname,
      :short => "-h HOSTNAME",
      :long => "--cisco-asa-hostname HOSTNAME",
      :description => "The hostname for Cisco ASA"

    option :cisco_asa_password,
      :short => "-p PASSWORD",
      :long => "--cisco-asa-password PASSWORD",
      :description => "The password for Cisco ASA"

    option :cisco_asa_username,
      :short => "-u USERNAME",
      :long => "--cisco-asa-username USERNAME",
      :description => "The username for Cisco ASA"
    $default[:cisco_asa_username] = ENV['USER']

    option :noop,
      :long => "--noop",
      :description => "Perform no modifying operations",
      :boolean => false
  end
end

Instance Method Details

#get_cisco_asa_configObject



61
62
63
64
# File 'lib/chef/knife/cisco_asa_base.rb', line 61

def get_cisco_asa_config
  config[:cisco_asa_password] = ask("Cisco Password for #{get_config(:cisco_asa_username)}: ") { |q| q.echo = "*" } unless get_config(:cisco_asa_password)
  config[:cisco_asa_enable_password] = ask("Enable Password for #{get_config(:cisco_asa_hostname)}: ") { |q| q.echo = "*" } unless get_config(:cisco_asa_enable_password)
end

#get_config(key) ⇒ Object



54
55
56
57
58
59
# File 'lib/chef/knife/cisco_asa_base.rb', line 54

def get_config(key)
  key = key.to_sym
  rval = config[key] || Chef::Config[:knife][key] || $default[key]
  Chef::Log.debug("value for config item #{key}: #{rval}")
  rval
end

#run_config_commands(commands) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chef/knife/cisco_asa_base.rb', line 66

def run_config_commands(commands)
  asa = Cisco::Base.new(:host => get_config(:cisco_asa_hostname), :user => get_config(:cisco_asa_username), :password => get_config(:cisco_asa_password), :transport => :ssh)
  asa.enable(get_config(:cisco_asa_enable_password))
  asa.cmd("conf t")
  commands.each do |command|
    asa.cmd(command)
  end
  asa.cmd("end")
  asa.cmd("write mem")
  unless get_config(:noop)
    output = asa.run
    output.each do |line|
      Chef::Log.debug(line)
    end
  end
  output
end

#tcp_test_port(hostname, port) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chef/knife/cisco_asa_base.rb', line 84

def tcp_test_port(hostname,port)
  tcp_socket = TCPSocket.new(hostname, port)
  readable = IO.select([tcp_socket], nil, nil, 5)
  if readable
    Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}") if port == 22
    true
  else
    false
  end
  rescue Errno::ETIMEDOUT
    false
  rescue Errno::EPERM
    false
  rescue Errno::ECONNREFUSED
    sleep 2
    false
  rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH
    sleep 2
    false
  ensure
    tcp_socket && tcp_socket.close
end