How to display python output in splunk? - python

I am writing a code in python which will run sql select query and return the result. How do I display the output from python script in Splunk?
Currently I just have a python script running sql query and have tried importing import splunklib.client as clientwhich fails as [pylint] E0401:Unable to import 'splunklib.client'
Tried this:
import mysql.connector
import splunklib.client as client
#splunk credentials
HOST = "localhost"
PORT = 8089
USERNAME = "admin"
PASSWORD = "yourpassword"
# Connect to splunk and log in
service = client.connect(
host=HOST,
port=PORT,
username=USERNAME,
password=PASSWORD)
But it gives an error while importing library
I expect the output of python script which will be something like :
STAGED = 1
FAILED = 2
VALIDATED =1
to be displayed in SPLUNK using python script itself.

You have to pass this command in commands.conf file. In stanza give command name and key value pair pass filename = python filename
[commandname]
filename = python.py
You can more knowledge on docs.splunk about commands.conf file

Related

How to connect to a mongo DB via SSH using Python?

Using python 3.10.10 on Windows 10 I am trying to connect to a mongo database via ssh ideally. On the command line I just do
ssh myuser#111.222.333.444
mongo
and I can query the mongo DB. With the following python code
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure
HOST = "111.222.333.444"
USER = "myuser"
class Mongo:
def __init__(self):
self.host = HOST
self.user = USER
self.uri = f"mongodb://{self.user}#{self.host}"
def connection(self):
try:
client = MongoClient(self.uri)
client.server_info()
print('Connection Established')
except ConnectionFailure as err:
raise(err)
return client
mongo = Mongo()
mongo.connection()
however I get an error
pymongo.errors.ConfigurationError: A password is required.
But as I am able to just login via ssh using my public key I do not require a password. How can this be solved in python?
I also tried to run a command on the command line using ssh alone like
ssh myuser#111.222.333.444 "mongo;use mydb; show collections"
but this does not work like that either.
You do two different things. In the first command you connect via ssh (using port 22) to the remote server. On the remote server you start the mongo shell. In the second command, you try to connect directly to the mongod server (default port 27017).
In your case myuser is the user on remote server operating system, not the user on the MongoDB.
You can (almost) always connect to a MongoDB without username/password, however when you provide a username then you also need a password. Try
self.uri = f"mongodb://{self.host}"
It is not fully clear what you try to achieve. You can configure MongoDB to logon with x509 certificate instead of username/password, see Use x.509 Certificates to Authenticate Clients. These connections are also encrypted via TLS/SSL.
Or are you looking to configure a SSH-Tunnel? See https://serverfault.com/questions/597765/how-to-connect-to-mongodb-server-via-ssh-tunnel
Here is the solution that I found in the end, as simple as possible, and it can be run from within python, and without any special module to install, from a windows powershell:
import json
import subprocess
cmd_mongo = json.dumps('db.units.find({"UnitId": "971201065"})')
cmd_host = json.dumps(f"mongo mydb --eval {cmd_mongo}")
cmd_local = f"ssh {USER}#{HOST} \"{cmd_host}\""
output = subprocess.check_output(cmd_local, shell=True)
print(output)

How do I make a pyodbc script where I can update the database password if necessary

I've created a Python script with pyodbc to access my server database. However, the password of the DB will eventually change and I would need an option to update it in the script.
So far I have
server = 'XXXXXXXX'
database = 'YYYYYYY'
username = 'ZZZZZZZZ'
password = 'ppp'
cnxn = pyodbc.connect('DRIVER={SQL Server}; SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+password)
Should password 'ppp' change to say 'qqq', how can I add an option to update it and have the app replace the previous password?
Thanks
Consider using an external resource to retrieve database credentials including a config .ini or .yaml file, environmental variables, or odbc.ini. Then, have this resource update as needed and avoid hard-coding any values in script which otherwise pose security risks especially in productionizing your app:
YAML Approach
YAML
[DATABASE]
server: 'XXXXXXXX'
database: 'YYYYYYY'
username: 'ZZZZZZZZ'
password: 'ppp'
Python
import yaml
import pyodbc
with open("data.yaml", 'r') as stream:
db_creds = yaml.load(stream)
cnxn = pyodbc.connect('DRIVER={{SQL Server}}; SERVER={server};DATABASE={database};' + \
'UID={username};PWD={password}'.format(**db_creds['DATABASE']))
Environment Variable Approach
Unix (printenv) or Windows (SET or gci env) (command line output)
...
DATABASE_SERVER='XXXXXXXX'
DATABASE_NAME='YYYYYYY'
DATABASE_USER='ZZZZZZZZ'
DATABASE_PWD='ppp'
...
Python
import os
import pyodbc
dbcreds = [os.environ['DATABASE_SERVER'], os.environ['DATABASE_NAME'],
os.environ['DATABASE_USER'], os.environ['DATABASE_PWD'])
cnxn = pyodbc.connect('DRIVER={{SQL Server}}; SERVER={0};DATABASE={1};' + \
'UID={2};PWD={3}'.format(*db_creds))
ODBC.ini Approach
odbc.ini
[myDSN]
Driver = SQL Server
Database = 'YYYYYYY'
Servername = 'XXXXXXXX'
UserName = 'ZZZZZZZZ'
Password = 'ppp'
Python
import pyodbc
cnxn = pyodbc.connect('DSN=myDSN')

Get mongod rs.status() results from a python script

For monitoring purposees I try to get the output of the following shell commands but from a python script
:mongo --port 27040
-> enters mongodb shell
:rs.status()
see image
The result of the command is json that I want to access outside the mongo shell to write it to a file, I can run other command in python using pymongo like:
import json, os
# load mongo library
current_dir = os.path.dirname(os.path.realpath(__file__))
os.sys.path.append(os.path.join(current_dir, 'pymongo-3.7.1-cp27-cp27m-manylinux1_x86_64.whl'))
from bson import json_util
from pymongo import MongoClient
from pymongo.errors import OperationFailure, ConnectionFailure
#connection settings
port = 27040
hostname = "localhost"
#default database used by mongodb
database = "test"
try:
# connect to the database
client = MongoClient(hostname,int(port))
db = client[database] # select the database
serverstats = db.command("serverStatus")
serialized_serverstats = json.dumps(serverstats, default=json_util.default)
print serialized_serverstats
except Exception as e:
print("Unhandled Error is %s" % e)
This runs something equal to running db.serverStatus() in the mongo shell.
But how do I run rs.status() form inside a python script?
You should do it like this:
db = client ['admin']
db_stats = db.command({'replSetGetStatus' :1})
If you want to check what's the underlying command of any shell command:
> rs.status
function () {
return db._adminCommand("replSetGetStatus");
}
>

Python script to upload a file to a remote server

I am working on a project that requires us to upload a vile via SFTP to a remote server, and we are having troubles doing this. We tried following this youtube guide, but we are having some issues.
We are getting a "no such file" error when we run the script, and we know for sure that the file exists and that we have given the python script the right name and location for the file.
This is the script as we have it right now:
import pysftp as sftp
def sftpTry():
try:
s = sftp.Connection(host='babbage.cs.missouri.edu', username ='<username>', password = '<password>')
remotepath = '~it3001s14grp1/videos/newVideo/new.avi'
#localpath = '/etc/motion/capture/hello.txt'
localpath = '/etc/motion/capture/06--2014-05-15---16-16-25.avi'
s.put(localpath, remotepath)
s.close()
except Exception, e:
print str(e)
sftpTry();
You should begin your remote path with a forward slash "/". Also, check the directory you are specifying in the remotepath. You should try to do a pwd in the directory when you login into the server (say using ssh). The remote-path should be specified exactly like that.
Although you do have the filename name in the remote path, it would throw an error if you specify just the folder's name.
Another tip would be to use getpass instead of hard-coding the password:
passwd = getpass.getpass()
s = sftp.Connection(host='<host>', username = '<username>', password = passwd)

Connecting to splunk server using python script then use this a in robotframework test

I am new to python and have run in to a problem with the following.
This is a code snippet from the Splunk api, thats used to connect to a splunk server then print the installed apps.
import splunklib.client as client
HOST = "server.splunk"
PORT = 8089
USERNAME = "UserABC"
PASSWORD = "Passw000rd"
# Create a Service instance and log in
service = client.connect(
host=HOST,
port=PORT,
username=USERNAME,
password=PASSWORD)
# Print installed apps to the console to verify login
for app in service.apps:
print app.name
I've tried it on my system in cmd and it works fine. However I intend to use this function in a Robot Framework test so the function needs to be defined in order to have a keyword I can use. I'm guessing something like the following:
import splunklib.client as client
def setServer(HOST, PORT, USERNAME, PASSWORD):
HOST = "server.splunk"
PORT = 8089
USERNAME = "UserABC"
PASSWORD = "Passw000rd"
service = client.connect(host=HOST,port=PORT,username=USERNAME,password=PASSWORD)
for app in service.apps:
print app.name
print ("\n")
My problem is when I run this nothing is printed to CMD at all. Any ideas
Thanks
A print in Python library is not displayed on the console of Robot Framework, that is the expected behaviour. If you want to check that the piece of code was run and the print was done, check the log.html produced by Robot. It should contain your print. Then if you really want to display something on Robot Console, then you have to use Log To Console keyword from your Robot Test. But as your print is in the python lib, you will have to import BuiltIn lib within your Python code. With all that, you should be fine.

Categories

Resources