Advertisement
Guest User

Untitled

a guest
Sep 20th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.94 KB | None | 0 0
  1. ################################################################################
  2. #                          IMPORT LIBRARIES:
  3. #   sys - used to ensure that splunk is in system path
  4. #   splunklib.client - to connect with Splunk Enterprise Server
  5. #   src.app.models - access all data models
  6. ################################################################################
  7. import sys
  8. import resources.sdk.splunklib.client as client
  9. from src.app.models.credential import Credential as credential
  10.  
  11. ################################################################################
  12. #                           GLOBAL VARIABLES:
  13. ################################################################################
  14.  
  15. def runner():
  16.     splunkPath() # Ensures that the Splunk SDK is in the system path
  17.     cred = getCredentials() # Import the credentials necessary to connect to Server
  18.     splunkConnect(cred)
  19.  
  20. ################################################################################
  21. #   Adds Splunk SDK to system path
  22. ################################################################################
  23. def splunkPath():
  24.     if "./resources/sdk" not in sys.path:
  25.         sys.path.append("./resources/sdk")
  26.  
  27. ################################################################################
  28. #   Imports the credentials to connect to the Splunk Enterprise Server
  29. ################################################################################
  30. def getCredentials():
  31.     useDefault = input("Would you like to use the default credential settings? <True/False>")
  32.     useDefault = useDefault == "True"
  33.     cred = credential("https://localhost", 8000, "admin", "changeme")
  34.     return cred
  35.  
  36. ################################################################################
  37. #   Connects to Splunk server with credentials
  38. ################################################################################
  39. def splunkConnect(credential):
  40.     service = client.connect(
  41.     host=credential.HOST,
  42.     port=credential.PORT,
  43.     username=credential.USERNAME,
  44.     password=credential.PASSWORD
  45.     )
  46.  
  47.     # Just test if it's all working and print out the apps
  48.     for app in service.apps:
  49.         print app.name
  50.  
  51. runner()
  52. ----------------------------------------------------------------------------------------------------------------------------------
  53. class Credential(object):
  54.     HOST = "localhost"
  55.     PORT = 8000
  56.     USERNAME = "admin"
  57.     PASSWORD = "changeme"
  58.     SCHEME = "https"
  59.     VERSION = "5.0"
  60.     LOCALCOPY = False
  61.  
  62.     def __init__(self, *args):
  63.         if self.LOCALCOPY:
  64.             with open("..\..\..\.splunkrc", "wb") as cred:
  65.                 allLines = cred.readLines()
  66.                 self.HOST = allLines[1][5:]
  67.                 self.PORT = allLines[3][5:]
  68.                 self.USERNAME = allLines[5][8:]
  69.                 self.PASSWORD = allLines[7][8:]
  70.  
  71.         else:
  72.             if args[0]!="localhost" or args[1] != 8000 or args[2]!="admin" or args[3]!="changeme":
  73.                 self.HOST = args[0]
  74.                 self.PORT = args[1]
  75.                 self.USERNAME = args[2]
  76.                 self.PASSWORD = args[3]
  77.  
  78.             fileContent = "# Splunk host (default: localhost) \n \
  79.                            host={} \n \
  80.                            # Splunk admin port (default: 8089) \n \
  81.                            port={} \n \
  82.                            # Splunk username \n \
  83.                            username={} \n \
  84.                            # Splunk password \n \
  85.                            password={} \n \
  86.                            # Access scheme (default: https) \n \
  87.                            scheme=https \n \
  88.                            # Your version of Splunk (default: 5.0) \n \
  89.                            version=5.0".format(args[0], args[1], args[2], args[3])
  90.  
  91.             newFile = open("..\..\..\.splunkrc", "wb")
  92.             newFile.write(fileContent)
  93.             newFile.close()
  94.             self.LOCALCOPY = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement