AVUpdater.py

The servers in our DMZ do not have any access to the outside world. Getting Symantec dat files onto the DMZ AV server was kind of a pain. I would manually download the latest file (when I remembered) and then copy it to the correct directory on the server. I was getting kind of tired of manually doing this and add to that it was only happening about 2 times a month. Today I decided that I would write a script in python to take care of getting the correct file and moving it to the right location for me.  I have not set it up as a scheduled task yet but I may in the future. Here is what I came up with:

Disclaimer: I know that this may not be the best code written. I am still learning so please make comments that could improve on what is here. Thanks.

#!/usr/bin/env python

import datetime
from ftplib import FTP
import shutil, os

### Config stuff ###
# FTP site to connect to
ftpsite = 'ftp.symantec.com'

# Directory where the files are stored on the FTP server
directory = 'AVDEFS/symantec_antivirus_corp/xdb/'

# Set a variable for todays date like Feb 03 to match to
today = datetime.datetime.now().strftime("%b %d")

# Array to hold the directory listing of the remote server
data = []

# Set the directory on the DMZ AV server to copy to
destDir = r'\\SomeRemoteAvServer\RemoteDir'

def handleDownload(block):
    file.write(block)
    print ".",

# Find the file that matches todays date
def getFilename(data):
    for line in data:
        perms, x, y, z, size, month, day, time, filename = line.split()
        if len(day) == 1:
            day = '0' + day
        timestamp = month + ' ' + day
        if timestamp == today:
            return filename

def compareFileSize(filename,destFile):
    """
    Compares src file to destFile to see if they are the same size.
    """
    if os.path.getsize(filename) == os.path.getsize(destFile):
        return 1
    else:
        return 0

# Open a connection to the FTP server
ftp = FTP(ftpsite)

# Log in as anonymous
print 'Logging in.'
print ftp.login()

# Change to the directory where the files are stored
ftp.cwd(directory)

# Get a directory listing
ftp.dir(data.append)

# Get the file that matches todays date from the directory listing
filename = getFilename(data)

print 'Opening local file ' + filename
file = open(filename, 'wb')

# Download the file a chunk at a time
# Each chunk is sent to handleDownload
# We append the chunk to the file and then print a '.' for progress
# RETR is an FTP command
print 'Getting ' + filename
ftp.retrbinary('RETR ' + filename, handleDownload)

# Clean up time
# Close the local file
print
print 'Closing file ' + filename
file.close()

# Disconnect from the FTP server
print 'Closing FTP connection'
print ftp.close()

destFile = os.path.join(destDir,filename)

# Now copy the file to the AV server in the DMZ if it does not already exist
# then remove it from the local system.
if os.path.isfile(destFile):
    print 'The file %s is already on the server.' % filename
    os.remove(filename)
else:
    print 'Copying the file to %s' % destDir
    shutil.copy2(filename, destDir)
    # Compare the two files to make sure they copied correctly
    if compareFileSize(filename, destFile):
        print 'The file looks to have copied fine. Deleting the local copy now.'
        os.remove(filename)
    else:
        print 'Something seems to have gone wrong. The file did not copy correctly to %s' % destDir
Advertisements
  1. #1 by zodman on February 3, 2009 - 6:28 pm

    Symantec ???? its like Norton AV ??? ughhhhhhhhhhhhh!!!!! im use nod32 its better and not consume a lot of Memory

  2. #2 by etank on February 3, 2009 - 6:33 pm

    Yeah but Norton AV is what we have standardised on at work. You gotta use what you gotta use I guess.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: