Created
January 23, 2020 07:50
-
-
Save hktaskin/b47c9a78491548a3c8a1f0026e42381a to your computer and use it in GitHub Desktop.
Tenable.sc Asset Difference Checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tenable.sc import TenableSC | |
import datetime | |
import difflib | |
from shutil import copyfile | |
import os | |
import smtplib | |
# crontab | |
# At 08:30 on every day-of-week from Monday through Friday. | |
# 30 8 * * 1-5 python /home/user/tsc_assets/tsc_assets.py >> /home/user/tsc_assets/log.txt | |
print("Start") | |
sc = TenableSC('TSC_IP_ADDRESS', port=443) | |
sc.login('USERNAME', 'PASSWORD') | |
assetlist = sc.asset_lists.details(2,["viewableIPs"]) # Dynamic asset with Plugin ID is equal to 19506 | |
sc.logout() | |
currenttime = datetime.datetime.now() | |
print(" " + currenttime.strftime('%Y-%m-%d %H:%M:%S %Z %z')) | |
PATH = "/home/user/tsc_assets/asset_list/" # has to end with "/" | |
filename = "tsc_assets_" + currenttime.strftime('%Y%m%d%H%M%S') + ".txt" | |
with open(PATH + filename, 'w') as outfile: | |
outfile.write("TIME: " + currenttime.strftime('%Y-%m-%d %H:%M:%S %Z %z') + "\n") | |
outfile.write("IP_COUNT: " + assetlist["viewableIPs"][0]["ipCount"] + "\n") | |
outfile.write("ASSET_LIST:\n" + assetlist["viewableIPs"][0]["ipList"] + "\n") | |
iplist1 = open(PATH + "yesterday.txt").readlines() | |
iplist2 = open(PATH + filename).readlines() | |
difffilename = "tsc_assets_" + currenttime.strftime('%Y%m%d%H%M%S') + "_diff.txt" | |
with open(PATH + difffilename, 'w') as difffile: | |
i = 0 | |
for line in difflib.unified_diff(iplist1, iplist2, n=0): | |
if line.startswith('-') or line.startswith('+'): | |
difffile.write(line) | |
newdifflist = open(PATH + difffilename).readlines() | |
if len(newdifflist) == 4: # no difference | |
os.remove(PATH + difffilename) | |
os.remove(PATH + filename) | |
print(" No difference") | |
else: # we have difference | |
copyfile(PATH + filename, PATH + "yesterday.txt") | |
copyfile(PATH + difffilename, PATH + "latestdiff.txt") | |
print(" Difference detected") | |
print(" " + filename) | |
print(" " + difffilename) | |
# send email | |
sender = 'from@domain.com' | |
receivers = ['to@domain.com'] | |
message = "From: SOMEONE <from@domain.com>\n" | |
message += "To: SOMEONE <to@domain.com>\n" | |
message += "Subject: TSC Asset Updates - " + currenttime.strftime('%Y.%m.%d') | |
message += "\n\n" | |
message += ''.join(newdifflist) | |
message += "\n\n---\nSIGNATURE"; | |
try: | |
smtpObj = smtplib.SMTP('SMTP_SERVER', 25) | |
smtpObj.sendmail(sender, receivers, message) | |
print(" Successfully sent email") | |
except: | |
print(" Error: unable to send email") | |
print("Done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment