summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSaku Ytti <saku@ytti.fi>2017-07-07 14:05:48 +0300
committerSaku Ytti <saku@ytti.fi>2017-07-07 14:05:48 +0300
commit72a6619986086df4d8db876ca0424f6bb6671b1b (patch)
treec248aae3c2f57bf228eb2884091d3d84a66c84da
parent776386ea339b53b90284acf123e0dce5758b901c (diff)
add pre_loginfeature-pre_login
-rw-r--r--lib/oxidized/input/cli.rb17
-rw-r--r--lib/oxidized/input/ssh.rb11
-rw-r--r--lib/oxidized/input/telnet.rb3
3 files changed, 27 insertions, 4 deletions
diff --git a/lib/oxidized/input/cli.rb b/lib/oxidized/input/cli.rb
index 660e173..fb39b81 100644
--- a/lib/oxidized/input/cli.rb
+++ b/lib/oxidized/input/cli.rb
@@ -4,12 +4,15 @@ module Oxidized
attr_reader :node
def initialize
+ @pre_login = []
@post_login = []
@pre_logout = []
@username, @password, @exec = nil
end
def get
+ pre_login
+ login
connect_cli
d = node.model.get
disconnect
@@ -19,6 +22,14 @@ module Oxidized
raise
end
+ def pre_login
+ Oxidized.logger.debug "lib/oxidized/input/cli.rb: Running pre_login commands at #{node.name}"
+ @pre_login.each do |command, block|
+ Oxidized.logger.debug "lib/oxidized/input/cli.rb: Running pre_login command: #{command.inspect}, block: #{block.inspect} at #{node.name}"
+ block ? block.call : (cmd command)
+ end
+ end
+
def connect_cli
Oxidized.logger.debug "lib/oxidized/input/cli.rb: Running post_login commands at #{node.name}"
@post_login.each do |command, block|
@@ -32,6 +43,12 @@ module Oxidized
@pre_logout.each { |command, block| block ? block.call : (cmd command, nil) }
end
+ def pre_login _pre_login=nil, &block
+ unless @exec
+ @pre_login << [_pre_login, block]
+ end
+ end
+
def post_login _post_login=nil, &block
unless @exec
@post_login << [_post_login, block]
diff --git a/lib/oxidized/input/ssh.rb b/lib/oxidized/input/ssh.rb
index 27e81e0..cca580b 100644
--- a/lib/oxidized/input/ssh.rb
+++ b/lib/oxidized/input/ssh.rb
@@ -24,7 +24,7 @@ module Oxidized
secure = Oxidized.config.input.ssh.secure
@log = File.open(Oxidized::Config::Log + "/#{@node.ip}-ssh", 'w') if Oxidized.config.input.debug?
port = vars(:ssh_port) || 22
-
+
ssh_opts = {
:port => port.to_i,
:password => @node.auth[:password], :timeout => Oxidized.config.timeout,
@@ -47,15 +47,18 @@ module Oxidized
Oxidized.logger.debug "lib/oxidized/input/ssh.rb: Connecting to #{@node.name}"
@ssh = Net::SSH.start(@node.ip, @node.auth[:username], ssh_opts)
+ connected?
+ end
+
+ def login
unless @exec
shell_open @ssh
begin
- login
+ shell_login
rescue Timeout::Error
raise PromptUndetect, [ @output, 'not matching configured prompt', @node.prompt ].join(' ')
end
end
- connected?
end
def connected?
@@ -116,7 +119,7 @@ module Oxidized
# some models have SSH auth or terminal auth based on version of code
# if SSH is configured for terminal auth, we'll still try to detect prompt
- def login
+ def shell_login
if @username
match = expect username, @node.prompt
if match == username
diff --git a/lib/oxidized/input/telnet.rb b/lib/oxidized/input/telnet.rb
index a5561b9..2b0a440 100644
--- a/lib/oxidized/input/telnet.rb
+++ b/lib/oxidized/input/telnet.rb
@@ -19,6 +19,9 @@ module Oxidized
opt['Output_log'] = Oxidized::Config::Log + "/#{@node.ip}-telnet" if Oxidized.config.input.debug?
@telnet = Net::Telnet.new opt
+ end
+
+ def login
if @node.auth[:username] and @node.auth[:username].length > 0
expect username
@telnet.puts @node.auth[:username]