Commit 58541e93 authored by Alexandre Dulaunoy's avatar Alexandre Dulaunoy
Browse files

Mgmt ref downloader added

parent d9b82c44
Loading
Loading
Loading
Loading

sbin/db_mgmt_ref.py

0 → 100755
+63 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Import NIST CVE Reference Key/Maps
#
# Software is free software released under the "Modified BSD license"
#
# Copyright (c) 2015 	Alexandre Dulaunoy - a@foo.be

# Imports
import os
import sys
runPath = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(runPath, ".."))

from urllib.request import urlopen
import zipfile
import shutil

from lib.ProgressBar import progressbar
from lib.Config import Configuration

RefUrl = Configuration.getRefURL()

# connect to db
db = Configuration.getMongoConnection()
info = db.info
# check modification date
try:
    u = urlopen(RefUrl)
except:
    sys.exit("Cannot open url %s. Bad URL or not connected to the internet?"%(vFeedurl))
i = info.find_one({'db': 'ref'})
if i is not None:
    if u.headers['last-modified'] == i['last-modified']:
        print("Not modified")
        sys.exit(0)
# create temp file and download and unpack database
if not os.path.exists('./tmp'):
    os.mkdir('./tmp')

with open('./tmp/allrefmaps.zip', 'wb') as fp:
    shutil.copyfileobj(u, fp)

x = zipfile.ZipFile('./tmp/allrefmaps.zip')
for e in x.namelist():
    print (e)

# extract each file (skip index.html)
# split the prefix name
#set s:CVEID of "alt link"
#key/value r:BEA  - url
#key/value d:BEA  - description of prefix


#with open('./tmp/vfeed.db.tgz', 'wb') as fp:
#    shutil.copyfileobj(u, fp)
#t = tarfile.open(name='./tmp/vfeed.db.tgz', mode='r')
#t.extract('vfeed.db', path='./tmp/')
#t.close

#update database info after successful program-run
info.update({'db': 'ref'}, {"$set": {'last-modified': u.headers['last-modified']}}, upsert=True)