| 53 | |
| 54 | }}} |
| 55 | |
| 56 | == Python 3 Script to Keep VPN Link Up == |
| 57 | |
| 58 | {{{ |
| 59 | |
| 60 | |
| 61 | }}} |
| 62 | |
| 63 | == Python 3 Library (lib3) for Above == |
| 64 | |
| 65 | {{{ |
| 66 | #Python Library written by paul kudla (c) 2011 |
| 67 | |
| 68 | #Load the librarys for the system |
| 69 | |
| 70 | import os,sys,time,socket |
| 71 | import string |
| 72 | from ftplib import FTP |
| 73 | from decimal import * |
| 74 | from datetime import date |
| 75 | import datetime |
| 76 | import smtplib |
| 77 | from email.mime.multipart import MIMEMultipart |
| 78 | from email.mime.base import MIMEBase |
| 79 | from email.mime.text import MIMEText |
| 80 | from email.utils import COMMASPACE, formatdate |
| 81 | from email import encoders |
| 82 | import subprocess |
| 83 | |
| 84 | getcontext().prec = 20 |
| 85 | |
| 86 | |
| 87 | class commands: |
| 88 | def __init__(self,command) : |
| 89 | self.command = command |
| 90 | #print (self.command) |
| 91 | self.output = 'Error' |
| 92 | self.status = '255' |
| 93 | |
| 94 | #sample |
| 95 | #rc, gopath = subprocess.getstatusoutput('ls -a') |
| 96 | |
| 97 | self.status, self.output = subprocess.getstatusoutput(self.command) |
| 98 | |
| 99 | try: |
| 100 | self.cr = self.output.split('\n') |
| 101 | except : |
| 102 | self.cr = [] |
| 103 | try: |
| 104 | self.count = len(self.cr) |
| 105 | except : |
| 106 | self.count = 0 |
| 107 | |
| 108 | self.status = int(self.status) |
| 109 | |
| 110 | #return count=number of lines, cr = lines split, getoutput = actual output returned, status = return code |
| 111 | |
| 112 | return |
| 113 | |
| 114 | #Email with attachment |
| 115 | class sendmail: |
| 116 | def __init__(self, send_from, send_to, send_subject, send_text, send_files): |
| 117 | #send_from, send_to, send_subject, send_text, send_files): |
| 118 | #print ('lib.py sending email') |
| 119 | assert type(send_to)==list |
| 120 | assert type(send_files)==list |
| 121 | |
| 122 | msg = MIMEMultipart() |
| 123 | msg['From'] = send_from |
| 124 | msg['To'] = COMMASPACE.join(send_to) |
| 125 | msg['Date'] = formatdate(localtime=True) |
| 126 | msg['Subject'] = send_subject |
| 127 | |
| 128 | msg.attach( MIMEText(send_text) ) |
| 129 | |
| 130 | for f in send_files: |
| 131 | part = MIMEBase('application', "octet-stream") |
| 132 | part.set_payload( open(f,"rb").read() ) |
| 133 | Encoders.encode_base64(part) |
| 134 | part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f)) |
| 135 | msg.attach(part) |
| 136 | |
| 137 | try : #Send Local? |
| 138 | smtp = smtplib.SMTP('') |
| 139 | #smtp.login('backup@scom.ca','522577') |
| 140 | #print ('Sending Email to : %s' %send_to) |
| 141 | smtp.sendmail(send_from, send_to, msg.as_string()) |
| 142 | smtp.close() |
| 143 | |
| 144 | except : |
| 145 | smtp = smtplib.SMTP('mail.scom.ca') |
| 146 | smtp.login('backup@scom.ca','522577') |
| 147 | #print ('Sending Email to : %s' %send_to) |
| 148 | smtp.sendmail(send_from, send_to, msg.as_string()) |
| 149 | smtp.close() |
| 150 | |
| 151 | class getdatetime: |
| 152 | def __init__(self): |
| 153 | self.datetime = datetime.date.today() |
| 154 | self.datetime_now = datetime.datetime.now() |
| 155 | self.date = str( time.strftime("%Y-%m-%d %H:%M:%S") ) |
| 156 | self.date_long = str( time.strftime("%Y-%m-%d %H:%M:%S") ) |
| 157 | self.date_short = str( time.strftime("%Y-%m-%d") ) |
| 158 | self.time = str( time.strftime("%H:%M:%S") ) |
| 159 | self.date_time_sec = self.datetime_now.strftime ("%Y-%m-%d %H:%M:%S.%f") |
| 160 | |
| 161 | |
| 162 | #Return edi senddate string (short) 2011-10-31 into 111031 |
| 163 | |
| 164 | class create_ascii : |
| 165 | def __init__(self,string_data) : |
| 166 | self.string_data = str(string_data) |
| 167 | import string |
| 168 | self.printable = set(string.printable) |
| 169 | self.list = list(filter(lambda x: x in self.printable, self.string_data)) |
| 170 | #print (self.list) |
| 171 | |
| 172 | self.ascii = '' |
| 173 | for n in range (0,len(self.list)) : |
| 174 | self.ascii = self.ascii + self.list[n] |
| 175 | self.ascii = str(self.ascii) |
| 176 | |
| 177 | return |
| 178 | |
| 179 | |
| 180 | class edi_send_date_short: |
| 181 | def __init__(self, senddate): |
| 182 | self.date = senddate |
| 183 | self.result = self.date[2] + self.date[3] + self.date[5] + self.date[6] + self.date[8] + self.date[9] |
| 184 | |
| 185 | def __str__(self): |
| 186 | return '%s' % self.result |
| 187 | |
| 188 | ##Return edi senddate string (long) 2011-10-31 into 20111031 |
| 189 | class edi_send_date_long: |
| 190 | def __init__(self, senddate): |
| 191 | self.date = senddate |
| 192 | self.result1 = self.date[0] + self.date[1] + self.date[2] + self.date[3] + self.date[5] + self.date[6] + self.date[8] + self.date[9] |
| 193 | self.result2 = self.date[2] + self.date[3] + self.date[5] + self.date[6] + self.date[8] + self.date[9] |
| 194 | |
| 195 | def __str__(self): |
| 196 | return '%s' % (self.result1,self.result2) |
| 197 | |
| 198 | class gpsdeg: |
| 199 | def __init__(self, dms): |
| 200 | self.dms = dms |
| 201 | self.is_positive = self.dms >= 0 |
| 202 | self.dms = abs(self.dms) |
| 203 | self.minutes,self.seconds = divmod(self.dms*3600,60) |
| 204 | self.degrees,self.minutes = divmod(self.minutes,60) |
| 205 | self.degrees = self.degrees if self.is_positive else -self.degrees |
| 206 | |
| 207 | def __str__(self): |
| 208 | return '%s' % (self.degrees,self.minutes,self.seconds) |
| 209 | |
| 210 | |
| 211 | class degdir: |
| 212 | def __init__(self, degrees): |
| 213 | self.direction_data = ['N','348.75','11.25','NNE', '11.25','33.75','NE','33.75','56.25','ENE', '56.25','78.75','E','78.75','101.25','ESE','101.25','123.75','SE','123.75','146.25','SSE','146.25','168.75','S','168.75','191.25','SSW','191.25','213.75','SW','213.75','236.25','WSW','236.25','258.75','W','258.75','281.25','WNW','281.25','303.75','NW','303.75','326.25','NNW','326.25','348.75'] |
| 214 | |
| 215 | |
| 216 | def __str__(self): |
| 217 | return '%s' % (self.direction) |
| 218 | |
| 219 | |
| 220 | class gettime: |
| 221 | def __init__(self): |
| 222 | self.uu = time.localtime() |
| 223 | |
| 224 | self.todaystime = str(self.uu[3]) #get the hr |
| 225 | |
| 226 | if int(self.uu[3]) < 10: #add a zero |
| 227 | self.todaystime = '0' + self.todaystime |
| 228 | if int(self.uu[4]) < 10: #add a zero in front |
| 229 | self.todaystime = self.todaystime +":0"+str(self.uu[4]) |
| 230 | else: |
| 231 | self.todaystime = self.todaystime +":"+str(self.uu[4]) |
| 232 | |
| 233 | def __str__(self): |
| 234 | return self.todaystime |
| 235 | |
| 236 | class array2dbstring: |
| 237 | def __init__(self,array): |
| 238 | self.data = array |
| 239 | for self.nn in range(0,len(self.data)): |
| 240 | print ('Data %s \t\t %s' % (str(self.data[self.nn]),str( type(self.data[self.nn])) ) ) #change all data into strings |
| 241 | self.a = type(self.data[self.nn]) |
| 242 | self.a = str(self.a) |
| 243 | if 'Decimal' in self.a : |
| 244 | self.n = str(self.data[self.nn]) |
| 245 | #self.n = self.n.lstrip("'") |
| 246 | #self.n = self.n.rstrip("'") |
| 247 | #self.data[self.nn] = float(self.data[self.nn]) |
| 248 | self.data[self.nn] = str('0.00') |
| 249 | print (self.n) |
| 250 | |
| 251 | if 'NoneType' in self.a : |
| 252 | self.data[self.nn] = '' |
| 253 | if 'datetime.datetime' in self.a : |
| 254 | #self.data[self.nn] = str(self.data[self.nn]) |
| 255 | #self.data[self.nn].replace |
| 256 | self.data[self.nn] = '2012-01-25 00:00:00' |
| 257 | self.data = str(self.data) |
| 258 | self.data = self.data.lstrip('[') |
| 259 | self.data = self.data.rstrip(']') |
| 260 | self.data = self.data.replace("'NULL'","NULL") |
| 261 | #self.data = self.data.replace(" '',", ",") |
| 262 | #self.data = self.data.replace(" '0.00'","'100'") |
| 263 | |
| 264 | def __str__(self): |
| 265 | return self.data |
| 266 | |
| 267 | class get_hostname: |
| 268 | def __init__(self): |
| 269 | self.hostname = socket.gethostname() |
| 270 | |
| 271 | |
| 272 | |