-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_sftp_login.rb
executable file
·81 lines (69 loc) · 1.81 KB
/
check_sftp_login.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#! /opt/puppetlabs/puppet/bin/ruby
# Author: Robert Waffen <robert.waffen@pixelpark.com>
# Date: 2020-08-07
require 'net/sftp'
require 'optparse'
# gem dependencies
# - net-ssh
# - net-sftp
# - bcrypt_pbkdf
# - ed25519
@options = {}
OptionParser.new do |opts|
opts.on('-H', '--hostname=HOSTNAME') { |hostname| @options[:hostname] = hostname }
opts.on('-i', '--identity=IDENTITY') { |identity| @options[:identity] = identity }
opts.on('-p', '--password=PASSWORD') { |password| @options[:password] = password }
opts.on('-u', '--username=USERNAME') { |username| @options[:username] = username }
end.parse!
@hostname = @options[:hostname]
@username = @options[:username]
@identity = @options[:identity] unless @options[:identity].nil? || @options[:identity].empty?
@password = @options[:password] unless @options[:password].nil? || @options[:password].empty?
@connected = false
if @hostname.nil?
puts 'Hostname missing'
exit 1
end
if @username.nil?
puts 'Username missing'
exit 1
end
if @password.nil? && @identity.nil?
puts 'Password or Identity has to be set!'
exit 1
end
begin
unless @password.nil?
Net::SFTP.start(
@hostname,
@username,
config: false,
non_interactive: true,
password: @password
) do |sftp|
sftp.connect do
@connected = true
end
end
end
unless @identity.nil?
Net::SFTP.start(
@hostname,
@username,
config: false,
non_interactive: true,
keys: [@identity],
keys_only: true
) do |sftp|
sftp.connect do
@connected = true
end
end
end
rescue Net::SSH::AuthenticationFailed => e
result = e.message
status = [2, 'CRITICAL', "not possible - #{result}"]
end
status = [0, 'OK', 'possible'] if @connected
puts "#{status[1]} - SFTP Login: #{status[2]}"
exit status[0]