Class: Inspec::Resources::MssqlSession
- Inherits:
-
Object
- Object
- Inspec::Resources::MssqlSession
- Defined in:
- lib/resources/mssql_session.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#pass ⇒ Object
readonly
Returns the value of attribute pass.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ MssqlSession
constructor
A new instance of MssqlSession.
- #query(q) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ MssqlSession
Returns a new instance of MssqlSession.
25 26 27 28 29 30 |
# File 'lib/resources/mssql_session.rb', line 25 def initialize(opts = {}) @user = opts[:user] @pass = opts[:pass] @host = opts[:host] || 'localhost' @instance = opts[:instance] end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
23 24 25 |
# File 'lib/resources/mssql_session.rb', line 23 def host @host end |
#pass ⇒ Object (readonly)
Returns the value of attribute pass.
23 24 25 |
# File 'lib/resources/mssql_session.rb', line 23 def pass @pass end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
23 24 25 |
# File 'lib/resources/mssql_session.rb', line 23 def user @user end |
Instance Method Details
#query(q) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/resources/mssql_session.rb', line 32 def query(q) escaped_query = q.gsub(/\\/, '\\\\').gsub(/"/, '\\"').gsub(/\$/, '\\$').gsub(/\@/, '`@') cmd_string = "sqlcmd -Q \"#{escaped_query}\"" cmd_string += " -U #{@user} -P #{@pass}" unless @user.nil? or @pass.nil? if @instance.nil? cmd_string += " -S #{@host}" else cmd_string += " -S #{@host}\\#{@instance}" end puts cmd_string cmd = inspec.command(cmd_string) out = cmd.stdout + "\n" + cmd.stderr if out =~ /Sqlcmd: Error/ skip_resource("Can't connect to the MS SQL Server.") end cmd end |
#to_s ⇒ Object
50 51 52 |
# File 'lib/resources/mssql_session.rb', line 50 def to_s 'MSSQL session' end |