Advertisement
Guest User

Untitled

a guest
Jul 12th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. require 'tiny_tds'
  2. require 'csv'
  3.  
  4. ## Connect to database
  5. client = TinyTds::Client.new username: '<db-user>', password: '<db-pass>', host: '<host or IP>', port: 1433, database: '<db-name>'
  6. if client.active?
  7. puts "Connection established"
  8. end
  9.  
  10. ## Open and readCSV file
  11.  
  12. logs = CSV.read('test.csv')
  13. record = 1
  14. index = 0
  15.  
  16. ## Store basic information and loop through each record
  17.  
  18. logs[record].each do |value|
  19. host = logs[record][0]
  20. domain = logs[record][1]
  21. type = logs[record][2]
  22. server_type = logs[record][3]
  23.  
  24. ## Insert Host information to database and test by verifying the generated index number for the entry
  25. results = client.execute("INSERT dbo.Host_Name (Host_Name)
  26. OUTPUT INSERTED.Host_Name_Index VALUES ('#{host}' )")
  27. results.each(:as => :array) do |row|
  28. puts row
  29. index = row.pop ## removes the array and grabs the device index of the current host record and stores it for use in Asset table entry
  30. end
  31.  
  32. results = client.execute("INSERT dbo.Domain (FQ_Domain_Name)
  33. OUTPUT INSERTED.Domain_Index VALUES ('#{domain}' )")
  34. results.each do |row|
  35. puts row
  36. end
  37.  
  38. ## Check device type and device index value to insert into Assets table
  39. if type == 'server'
  40. results = client.execute("INSERT dbo.Assets (Host_Name_Index, Type_Index)
  41. OUTPUT INSERTED.Device_Index VALUES (#{index.to_i},1)")
  42. results.each do |row|
  43. puts row
  44. end
  45. elsif type == 'desktop'
  46. results = client.execute("INSERT dbo.Assets (Host_Name_Index, Type_Index)
  47. OUTPUT INSERTED.Device_Index VALUES (#{index.to_i},2)")
  48. results.each do |row|
  49. puts row
  50. end
  51. else type == 'laptop'
  52. results = client.execute("INSERT dbo.Assets (Host_Name_Index, Type_Index)
  53. OUTPUT INSERTED.Device_Index VALUES (#{index.to_i},3)")
  54. results.each do |row|
  55. puts row
  56. end
  57. end
  58.  
  59. ## increase record count to move to next record entry
  60. record += 1
  61. end
  62.  
  63. print 'Done processing csv file'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement