Class: OpenvasCli::VasConnection
- Inherits:
-
Object
- Object
- OpenvasCli::VasConnection
- Includes:
- XmlAddin
- Defined in:
- lib/openvas-cli/vas_connection.rb
Instance Method Summary collapse
-
#close ⇒ Object
Closes the active connection and sets it up for re-connection.
- #config ⇒ Object
-
#initialize(config = {}) ⇒ VasConnection
constructor
Initializes the client, connectes to the OpenVas Managment service specified by host & port, and unless auto_login is set to
false, loggs in using username and password. -
#login ⇒ Object
Logs into the OpenVAS Management service using the specified username and passoword.
- #send_receive(request) ⇒ Object
- #state ⇒ Object
Methods included from XmlAddin
Constructor Details
#initialize(config = {}) ⇒ VasConnection
Initializes the client, connectes to the OpenVas Managment service specified by host & port, and unless auto_login is set to false, loggs in using username and password.
14 15 16 17 18 19 20 21 22 |
# File 'lib/openvas-cli/vas_connection.rb', line 14 def initialize(config={}) @logger = OpenvasCli.logger @config = OpenvasCli.configuration connect if @config.auto_login == true login end end |
Instance Method Details
#close ⇒ Object
Closes the active connection and sets it up for re-connection.
25 26 27 28 |
# File 'lib/openvas-cli/vas_connection.rb', line 25 def close @socket.close if @socket @socket = nil end |
#config ⇒ Object
30 31 32 |
# File 'lib/openvas-cli/vas_connection.rb', line 30 def config @config ||= {} end |
#login ⇒ Object
Logs into the OpenVAS Management service using the specified username and passoword. By default, this method is called by new unless auto_login is set to false.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/openvas-cli/vas_connection.rb', line 37 def login ("Logging in: :user => #{@config.username}", :info) areq = Nokogiri::XML::Builder.new { |xml| xml.authenticate { xml.credentials { xml.username { xml.text(@config.username) } xml.password { xml.text(@config.password) } } } } send_receive(areq.doc) end |
#send_receive(request) ⇒ Object
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/openvas-cli/vas_connection.rb', line 55 def send_receive(request) if request.kind_of? String tosend = request else tosend = request.to_xml end tries = 0 begin unless @socket && @socket.state =~ /sslok/i ("Socket closed, Reconnecting", :info) connect login end ("Sending: #{tosend}", :debug) @socket.puts(tosend) rbuf='' size=0 begin timeout(@config.time_out) { a = @socket.sysread(@config.buffer_size) size = a.length rbuf << a } end while size >= @config.buffer_size rescue Timeout::Error msg = "Command Timed Out [#{tries}] (#{$!})\nCommand: #{tosend}" msg, :error tries += 1 @socket = nil retry if tries < @config.max_tries raise VasExceptions::CommunicationException.new(msg) rescue EOFError msg = "EOFError [#{tries}] (#{$!})\nReceived: #{rbuf}\nCommand: #{tosend}" msg, :error tries += 1 @socket = nil retry if tries < @config.max_tries raise VasExceptions::CommunicationException.new(msg) end response = Nokogiri::XML(rbuf) "RECEIVED: #{response.to_xml}", :debug unless VasConnection.extract_value_from("//@status", response) =~ /20\d/ msg = "Command Failed: #{VasConnection.extract_value_from("//@status_text", response)}\n" + "Command Status: #{VasConnection.extract_value_from("//@status", response)}\n" + "Command: #{tosend}\n" + "Response: #{response.to_xml}" msg, :error raise VasExceptions::CommandException.new(msg) end response end |
#state ⇒ Object
51 52 53 |
# File 'lib/openvas-cli/vas_connection.rb', line 51 def state @socket ? @socket.state : "closed" end |