Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ################################################################################
- # IMPORT LIBRARIES:
- # sys - used to ensure that splunk is in system path
- # splunklib.client - to connect with Splunk Enterprise Server
- # src.app.models - access all data models
- ################################################################################
- import sys
- import resources.sdk.splunklib.client as client
- from src.app.models.credential import Credential as credential
- ################################################################################
- # GLOBAL VARIABLES:
- ################################################################################
- def runner():
- splunkPath() # Ensures that the Splunk SDK is in the system path
- cred = getCredentials() # Import the credentials necessary to connect to Server
- splunkConnect(cred)
- ################################################################################
- # Adds Splunk SDK to system path
- ################################################################################
- def splunkPath():
- if "./resources/sdk" not in sys.path:
- sys.path.append("./resources/sdk")
- ################################################################################
- # Imports the credentials to connect to the Splunk Enterprise Server
- ################################################################################
- def getCredentials():
- useDefault = input("Would you like to use the default credential settings? <True/False>")
- useDefault = useDefault == "True"
- cred = credential("https://localhost", 8000, "admin", "changeme")
- return cred
- ################################################################################
- # Connects to Splunk server with credentials
- ################################################################################
- def splunkConnect(credential):
- service = client.connect(
- host=credential.HOST,
- port=credential.PORT,
- username=credential.USERNAME,
- password=credential.PASSWORD
- )
- # Just test if it's all working and print out the apps
- for app in service.apps:
- print app.name
- runner()
- ----------------------------------------------------------------------------------------------------------------------------------
- class Credential(object):
- HOST = "localhost"
- PORT = 8000
- USERNAME = "admin"
- PASSWORD = "changeme"
- SCHEME = "https"
- VERSION = "5.0"
- LOCALCOPY = False
- def __init__(self, *args):
- if self.LOCALCOPY:
- with open("..\..\..\.splunkrc", "wb") as cred:
- allLines = cred.readLines()
- self.HOST = allLines[1][5:]
- self.PORT = allLines[3][5:]
- self.USERNAME = allLines[5][8:]
- self.PASSWORD = allLines[7][8:]
- else:
- if args[0]!="localhost" or args[1] != 8000 or args[2]!="admin" or args[3]!="changeme":
- self.HOST = args[0]
- self.PORT = args[1]
- self.USERNAME = args[2]
- self.PASSWORD = args[3]
- fileContent = "# Splunk host (default: localhost) \n \
- host={} \n \
- # Splunk admin port (default: 8089) \n \
- port={} \n \
- # Splunk username \n \
- username={} \n \
- # Splunk password \n \
- password={} \n \
- # Access scheme (default: https) \n \
- scheme=https \n \
- # Your version of Splunk (default: 5.0) \n \
- version=5.0".format(args[0], args[1], args[2], args[3])
- newFile = open("..\..\..\.splunkrc", "wb")
- newFile.write(fileContent)
- newFile.close()
- self.LOCALCOPY = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement