Language selection:
Skip to main navigation Skip to main content Skip to page footer

Below is a description of the script, designed to save the settings of a group of master units on the ftp server.

Running the script is provided on the operating system of the user (administrator). To periodically run this script you need to use the task scheduler of your operating system (cron, anacron - for * nix, Task Scheduler, nnCron - for Win, etc.)

To run the script requires Python 3, the distributive which can be downloaded here: https://www.python.org/downloads/, or installed from the repository of your operating system.

To configure the script You must specify the parameters in the User settings tab:

ParameterFunction
usernameUser account name with administrator privileges on the device
passwordUser account password with administrator privileges on the device
ftpurlFTP server address
ftpuserFTP server user
ftppasswordFTP server password
ftpdirFTP directory for copies of setting files

The list of monitoring units' IP addresses is defined in the file hosts.txt with one IP address on each line. For Example:

192.168.0.193
192.168.0.194
telemetry.asia

Configuration files are stored on the ftp server in the specified directory as settings_mm.dd.yy-hh: mm: ss.vut

The text of the script is attached below:

vtbackup.py Group settings backup 

#!/usr/bin/python3
#
# vtbackup.py 

# Didactum backup settings script v.0.3
#
__author__ = 'Didactum'

import datetime
import ftplib
import hashlib
import os
import sys
import time
import urllib.request
from urllib.request import urlretrieve
import urllib.parse
from xml.dom.minidom import parse, parseString

# get Python version
# print(sys.version_info)

#-----------------------------------
#
# User settings
#
#-----------------------------------
# admin username on device
username = 'guest'
# password on device 
password = 'guest' # or setup hash: h=...
hash_object = hashlib.sha1(str.encode(password))
h = hash_object.hexdigest()
#print(h)
# ftp url:
ftpurl = 'ftp.server.com'
# ftp user name:
ftpuser = 'user'
# password for ftp user:
ftppassword = 'pass'
# directory on ftp server:
ftpdir = '/savesettings/'
#-----------------------------------


#
# make filename by timestamp
#
def MakeFilename(host, prefix='settings_', ext='.vut'):
   timeStamp =  datetime.datetime.fromtimestamp(time.time()).strftime("%b-%d-%y-%H:%M:%S")
   filename = prefix+host+'-'+timeStamp+ext
   return filename

#
# auth procedure
#
def QueryAuth(host, username, h):
   param = "querytype=auth&name=%s&h=%s" % (username, h)
   hosturl="http://"+host+"/"
 
   response = urllib.request.urlopen(hosturl+"engine.htm", param.encode("ascii"))   
   #print(response.read());

   xmldoc = parseString(response.read().decode("utf-8"))
   itemlist = xmldoc.getElementsByTagName('error')
   if len(itemlist) :
       print("Error"+itemlist[0].attributes['type'].value)
       return
   else :
     itemlist = xmldoc.getElementsByTagName('user')

     if len(itemlist) :
         k=itemlist[0].attributes['k'].value
         #print(k)
         return k

#
# get settings procedure
#
def QuerySettingsGet(host, k, filename):
   hosturl="http://"+host+"/"
   param = ("k=%s" % (k))
   urlretrieve(hosturl+"settings.htm?"+param, filename)

#
# get dump procedure
#
def QueryDumpGet(host, k):
   param = "&txt=true&id=201001"
   pparam = ("k=%s&%s" % (k,param))
   print(pparam)
   urlretrieve(hosturl+"dump.htm?"+pparam, "dump.xml")

#
# Upload to FTP server 
#
def FtpUpload(ftpurl, ftpuser,ftppassword, ftpdir, filename):
   # file to send
   file = open(filename,'rb')

   # send the file
   session = ftplib.FTP(ftpurl, ftpuser, ftppassword)
   session.cwd(ftpdir)
   session.storbinary('STOR '+filename, file)
   session.quit()

   # close file
   file.close()

#
# backup device settings 
#
def HostBackup(host):
   # make filename
   filename = MakeFilename(host)
   #print(filename)
   #return 
   
   try :
       # auth on device
       k = QueryAuth(host, username,h)
       #print(k)

       # get file settings from device
       QuerySettingsGet(host, k, filename)

       # upload to ftp
       FtpUpload(ftpurl, ftpuser,ftppassword, ftpdir, filename)

       # tidy 
       os.remove(filename)
       print("%s\t - backup succeeded" % (host))
   except:
       print("%s\t - backup failed" % (host))  

#
# backup settings for devices from list (text file)
#
def HostsListBackup(hosts):
   with open(hosts, 'r') as f:
       lines = f.readlines()
       for host in lines:
           HostBackup(host.rstrip('\n')) 
   
#
# main programm
#

HostsListBackup("hosts.txt");    

To run the script, you must run:

> python vtbackup.py

or

$ python3 vtbackup.py

In this case, the path to the Python interpreter must be specified in the PATH, or you must specify the full path in the start line.

The result of the script:

192.168.0.193     - backup succeeded
192.168.0.194     - backup failed
telemetry.asia    - backup succeeded

This website uses cookies

This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Cookie Policy.

Essential cookies enable basic functions and are necessary for the website to function properly.
Statistics cookies collect information anonymously. This information helps us to understand how our visitors use our website.
Marketing cookies are used by third parties or publishers to display personalized advertisements. They do this by tracking visitors across websites.