-
Notifications
You must be signed in to change notification settings - Fork 3
/
pp_network.py
285 lines (230 loc) · 8.95 KB
/
pp_network.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import subprocess
import smtplib
from email.mime.text import MIMEText
import email.utils
import configparser
import time
from time import sleep
from pp_utils import Monitor
class Mailer(object):
config=None
def __init__(self):
self.mon=Monitor()
""" email is enabled if email.cfg exists"""
def read_config(self,options_file=None):
if options_file != None:
Mailer.options_file=options_file
Mailer.config=configparser.ConfigParser(inline_comment_prefixes = (';',))
self.config=Mailer.config
self.config.read(Mailer.options_file)
self.server=self.config.get('email','server')
self.port=self.config.get('email','port')
self.username=self.config.get('email','username')
self.password=self.config.get('email','password')
self.email_to=self.config.get('email-editable','to')
self.is_to_list= self.email_to.splitlines()
self.is_to = ', '.join(self.is_to_list)
if self.config.has_option('email-editable','sender'):
self.email_sender = self.config.get('email-editable','sender')
else:
self.email_sender = self.username
email_allowed=self.config.get('email-editable','email_allowed')
if email_allowed == 'yes':
self.email_allowed = True
else:
self.email_allowed = False
email_with_ip=self.config.get('email-editable','email_with_ip')
if email_with_ip == 'yes':
self.email_with_ip = True
else:
self.email_with_ip=False
email_on_error=self.config.get('email-editable','email_on_error')
if email_on_error == 'yes':
self.email_on_error = True
else:
self.email_on_error=False
email_on_terminate=self.config.get('email-editable','email_on_terminate')
if email_on_terminate == 'yes':
self.email_on_terminate = True
else:
self.email_on_terminate=False
email_at_start=self.config.get('email-editable','email_at_start')
if email_at_start == 'yes':
self.email_at_start = True
else:
self.email_at_start=False
attach_log=self.config.get('email-editable','log_on_error')
if attach_log == 'yes':
self.log_on_error = True
else:
self.log_on_error=False
def save_config(self):
self.config.set('email-editable','email_allowed','yes' if self.email_allowed is True else'no')
self.config.set('email-editable','to',self.email_to)
self.config.set('email-editable','sender',self.email_sender)
self.config.set('email-editable','email_with_ip','yes' if self.email_with_ip is True else'no')
self.config.set('email-editable','email_at_start','yes'if self.email_at_start is True else 'no')
self.config.set('email-editable','email_on_error','yes' if self.email_on_error is True else 'no')
self.config.set('email-editable','email_on_terminate','yes' if self.email_on_terminate is True else 'no')
self.config.set('email-editable','log_on_error','yes' if self.log_on_error is True else 'no')
with open(Mailer.options_file, 'w') as config_file:
self.config.write(config_file)
def connect(self):
tries = 0
while True:
try:
self.smtpserver = smtplib.SMTP(self.server, self.port, timeout=1)
break
except Exception as e:
tries = tries + 1
if (tries > 5):
return False, e
time.sleep(1)
return True, ''
def send(self,subject,message):
#say hello
try:
self.smtpserver.ehlo()
except Exception as ex:
return False,'First ehlo ' + str(ex)
try:
self.smtpserver.starttls()
except Exception as ex:
return False,'Start TLS ' + str(ex)
try:
self.smtpserver.ehlo()
except Exception as ex:
return False,'Second ehlo ' + str(ex)
#login
try:
self.smtpserver.login(self.username, self.password)
except Exception as ex:
return False,'Login ' + str(ex)
msg = MIMEText(message)
msg['Subject'] = subject
msg['From'] = self.email_sender
msg['To'] = self.is_to
msg['Date']= email.utils.formatdate(localtime=True)
#print (self.username,self.email_sender,self.is_to_list,msg)
try:
self.smtpserver.sendmail(self.email_sender, self.is_to_list, msg.as_string())
except Exception as ex:
return False,'sendmail ' + str(ex)
return True,''
def disconnect(self):
try:
self.smtpserver.quit()
except Exception as ex:
return False,str(ex)
return True,''
class Network(object):
ip=''
interface=''
def __init__(self):
self.mon=Monitor()
self.force_ip=''
def get_ip(self):
return Network.interface, Network.ip
def set_ip(self, interface,ip):
Network.interface = interface
Network.ip = ip
def read_config(self,options_file_path):
config=configparser.ConfigParser(inline_comment_prefixes = (';',))
config.read(options_file_path)
self.preferred_interface=config.get('network','preferred_interface')
self.unit=config.get('network','unit')
self.force_ip=config.get('network','force_ip')
self.manager_port=int(config.get('manager','port'))
self.manager_username=config.get('manager','username')
self.manager_password=config.get('manager','password')
self.editor_username=config.get('editor','username')
self.editor_password=config.get('editor','password')
self.editor_port=int(config.get('editor','port'))
def wait_for_network(self,tries):
i=0
while True:
if self.is_connected() is True:
return True
else:
i+=1
self.mon.log(self, 'Trying to connect to network ' + str(i))
if i>tries:
return False
sleep(1)
def is_connected(self):
interfaces,ips = self.get_ips()
if interfaces == 0:
return False
else:
return True
def get_ips(self):
arg='ip route list'
p=subprocess.Popen(arg,shell=True,universal_newlines=True,stdout=subprocess.PIPE)
data = p.communicate()
# Split data [0] obtained from stdout into lines
# print (' data is',data[0],'\n')
ip_lines = data[0].splitlines()
if len(ip_lines) == 0:
# print 'not connected'
return 0, []
interfaces=0
result=[]
for line in ip_lines:
fields= line.split()
# print (fields)
if fields[0] != 'default' and 'src' in fields:
interface= fields[fields.index('dev')+1]
ip = fields[fields.index('src')+1]
interfaces +=1
result=result + [[interface,ip]]
return interfaces, result
def get_preferred_ip(self):
# returned ip_type
# none available - return ''
# 1 available - return it
# 2 or more available - return one that matches or if none match first found
if self.force_ip.strip() != '':
return self.preferred_interface,self.force_ip
number, interfaces=self.get_ips()
# print interfaces,ips
if number == 0:
return '',''
elif number == 1:
#if one interface return only
i_type = interfaces[0][0]
ip = interfaces[0][1]
return i_type,ip
else:
i_type=''
for interface in interfaces:
if interface[0] == self.preferred_interface:
i_type=interface[0]
ip=interface[1]
return i_type,ip
# nothing matches preferred so use first one
i_type=interfaces[0][0]
ip=interfaces[0][1]
return i_type,ip
if __name__ == "__main__":
network=Network()
i=0
while True:
if network.is_connected() is True:
break
else:
i+=1
if i>20:
print('failed to connect after 20 seconds')
exit()
sleep(1)
print('network available')
preferred_interface=''
i_type,ip=network.get_preferred_ip()
print(i_type,ip)
mailer=Mailer()
mailer.read_config('/home/pi/pipresents/pp_config/pp_email.cfg')
success=mailer.connect()
print('Connected '+ str(success))
mailer.send(
'test','hello')
mailer.disconnect()