Python script to download virus definitions

Part of my daily responsibilities are to update our virus definitions daily on our networks. These networks are private LANs and do not have access to the Internet. Repeatedly going to the same site and downloading then became a chore, so I wrote this script, added it to a cron job and viola! Daily definitions waiting for me on my desktop every morning when I get to work.

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
#!/usr/bin/python
 
import time
import re
import glob
import os
from urllib import urlopen
from datetime import date
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():
    # Delete old file
    print "Removing any old files..."
    search_path = '/home/cpowell/Desktop/'
    old_files = glob.glob(search_path + '*.jdb')
    for found in old_files:
        print "t...%s deleted." % found
        os.remove(found)
 
# Open everything
print "Checking for new definitions..."
currentDate = date.today().strftime('%d-%b-%Y')
url = "http://definitions.symantec.com/defs/jdb/"
file_found = False
site = urlopen(url)
parser = URLLister()
s = site.readlines()
for line in s:
    if re.search(currentDate, line):
        parser.feed(line)
        file_found = True
site.close()
parser.close()
if file_found:
    delete_old()
    #Download the file
    print "Downloading the new virus definitions, please wait..."
    dl = urlopen(url + parser.url)
    newfile = open('/home/cpowell/Desktop/' + currentDate + "_" + parser.url, 'w')
    newfile.write(dl.read())
    newfile.close()
    dl.close()
else:
    print "No new virus definitions found."
 
print "Done."

Leave a Reply

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