Remote Syslog rslogger can be used to write important lines of informational logging from a python script to a remote syslog server. We found it usefull as we run multiple scripts on different hosts. With this we track the given info on a central / remote server. Example use case: automation scripts for device configuration.
Install the python socket module using the following command:
pip install socket
Get a local copy of this repo:
git clone https://github.com/tslenter/rslogger
cd rslogger
#On Windows:
copy rslogger <Directory of the project>
#On Linux
cp rslogger <Directory of the project>
The following is a demo example that extracts data from a Cisco DNA controller and sends the data string to a syslog socket:
import requests
import os
from requests.auth import HTTPBasicAuth
import urllib3
import argparse
from rslogger import syslog
from rslogger import fcl
from rslogger import lvl
#Disable HTTPS validation
urllib3.disable_warnings()
#Set variables to None
hostname = None
username = None
password = None
#Create HTTP header
headers = {
'content-type': "application/json",
'x-auth-token': ""
}
#Global information
print('Running from directory: ', os.getcwd())
#Add arguments
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--hostname', help='Enter a hostname or ip of the Cisco DNA Controller', required=True)
parser.add_argument('-u','--username', help='Add a username', required=True)
parser.add_argument('-p', '--password', help='Add a password', required=True)
args = parser.parse_args()
#Extract variables from namespace to global
globals().update(vars(args))
#Generate token for DNA Controller
def dnac_login(host, passwrd, user):
# Generate token
BASE_URL = 'https://' + host
AUTH_URL = '/dna/system/api/v1/auth/token'
USERNAME = user
PASSWORD = passwrd
response = requests.post(BASE_URL + AUTH_URL, auth=HTTPBasicAuth(USERNAME, PASSWORD), verify=False)
token = response.json()['Token']
return token
#Extract data from DNA controller
def network_device_list(token, host):
url = "https://" + host + "/api/v1/network-device"
headers["x-auth-token"] = token
response = requests.get(url, headers=headers, verify=False)
data = response.json()
for item in data['response']:
#Feel free to list more information: item["hostname"],item["platformId"],item["softwareType"],item["softwareVersion"],item["upTime"], item["serialNumber"], item["managementIpAddress"]
message = str("hostname: ")+item["hostname"]
syslog(message, level=lvl['notice'], facility=fcl['log_audit'], host='172.16.201.2', port=514)
#Login to DNA Controller
if hostname or username or password != None:
print("Started session on: " + hostname)
print("Started session with user: " + username)
login = dnac_login(hostname, password, username)
network_device_list(login, hostname)
else:
print("Did you use the parameters to run this command?")
kern, user, mail, daemon, auth, syslog, lpr, news, uucp, cron, authpriv, ftp, ntp, log_audit, log_alert, clock_daemon, local0, local1, local2, local3, local4, local5, local6, local7
emerg, alert, crit, err, warning, notice, info, debug
from rslogger import syslog
from rslogger import fcl
from rslogger import lvl
#Run test message to localhost (a syslog server is needed)
syslog()
#Expected output:
#Jul 5 17:02:21 localhost daemon: notice: Test is RS test message to localhost
#With variables:
message=str('Hello world')
syslog(message, level=lvl['alert'], facility=fcl['daemon'], host='172.16.201.2', port=514)
#Expected output (syslog server):
#Jul 5 17:02:21 comp0001.remotesyslog.com rslogger: daemon: alert: rslogger_output: Hello world
rslogger is free to use. If you like this content and you like to make a donation go to the main webpage of Remote Syslog for more information:
https://www.remotesyslog.com/
To improve the code and functions we like to have you help. Send your idea or code to: info@remotesyslog.com or create a pull request. We will review it and add it to this project.
"rslogger" is a free application that can be used to send syslog messages from python to Remote Syslog.
Copyright (C) 2021 Tom Slenter
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
For more information contact the author:
Name author: Tom Slenter
E-mail: info@remotesyslog.com
More information can be found:
Website:
https://www.remotesyslog.com/
Read the docs:
https://remote-syslog.readthedocs.io/en/latest/
Master script:
https://www.github.com/tslenter/RS
Owner
Contributors
Categories
Products
Catalyst CenterProgramming Languages
PythonLicense
Code Exchange Community
Get help, share code, and collaborate with other developers in the Code Exchange community.View Community