Updated: Python script to download virus definitions

I previously wrote a script that would download the virus definitions from Symantec’s website. The script performed wonderfully, but there were a few things that started to bug me. So time to make a few changes!

First off, I set the script to run on a cron job at 9am PST. You would think that Symantec would release something by that time in the morning, but often nothing would be posted. This lead to the script not downloading any defs and me having to go to the site to download them, thus negating my script.

So I updated the script to check to newer defs and to download the latest. The script was then set to run hourly. I also included a progress meter, not so much because it was needed, but simply to do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python
 
import re
import glob
import os
import sys
from urllib import urlretrieve
from datetime import datetime
from sgmllib import SGMLParser
 
class URLLister(SGMLParser):
    def reset(self):
        SGMLParser.reset(self)
        self.url = ""
 
    def start_a(self, attrs):
        href = attrs[0][1]
        if href:
            self.url = href
 
def delete_old():
    search_path = '/home/cpowell/Desktop/'
    old_files = glob.glob(search_path + '*.jdb')
    for found in old_files:
        os.remove(found)
 
def newFile(fn):
    # Check for newer file
    oldFile = glob.glob('/home/cpowell/Desktop/' + '*.jdb')
    if not oldFile:
        return True
    if (oldFile[0].split('/').pop() != fn):
        return True
    else:
        return False
 
def dlProgress(count, blockSize, totalSize):
    percent = int(count*blockSize*100/totalSize)
    sys.stdout.write("rtttttt" + "...%d%%" % percent)
    sys.stdout.flush()
 
def downloadFile(url, fn):
    print "Downloading new virus definitions, please wait...",
    delete_old()
    fileName = '/home/cpowell/Desktop/' + fn
    urlretrieve(url + fn, fileName, reporthook=dlProgress)
 
# Open everything
print "Checking for new definitions..."
now = datetime.now()
currentDate = now.strftime('%d-%b-%Y')
url = "http://definitions.symantec.com/defs/jdb/"
site = urlopen(url)
parser = URLLister()
s = site.readlines()
links = []
 
for line in s:
    if re.search(currentDate, line):
        links.append(line)
 
if links:
    parser.feed(links.pop())
    if newFile(parser.url):
        downloadFile(url, parser.url)
    else:
        print "nNo new virus definitions found."
else:
    print "nNo new virus definitions found."
 
site.close()
parser.close()

One thought on “Updated: Python script to download virus definitions

  1. Pingback: Updated Again: Python script to download virus definitions

Leave a Reply

Your email address will not be published. Required fields are marked *