Class: CiscoAsaKnifePlugin::BaseCiscoAsaCommand

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/BaseCiscoAsaCommand.rb

Direct Known Subclasses

CiscoAsaHostAdd, CiscoAsaHostRemove

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_common_optionsObject



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
# File 'lib/chef/knife/BaseCiscoAsaCommand.rb', line 18

def self.get_common_options
  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

Instance Method Details

#get_cisco_asa_configObject



58
59
60
61
# File 'lib/chef/knife/BaseCiscoAsaCommand.rb', line 58

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_host)}: ") { |q| q.echo = "*" } unless get_config(:cisco_asa_enable_password)
end

#get_config(key) ⇒ Object



51
52
53
54
55
56
# File 'lib/chef/knife/BaseCiscoAsaCommand.rb', line 51

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/chef/knife/BaseCiscoAsaCommand.rb', line 63

def run_config_commands(commands)
  asa = Cisco::Base.new(:host => get_config(:cisco_asa_host), :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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/chef/knife/BaseCiscoAsaCommand.rb', line 81

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