Pi to Pi connection checker
Posted: Sat Oct 04, 2014 8:08 pm
Hello,
I have two Pi at two different addresses monitoring all sorts of things.
I would like each to check in with the other to ensure connectivity and in the event of a power failure, network failure, etc... alert me with my existing alerting system.
my first thought was to have them email each other periodically but i would haven't really looked into code to check emails and verify its from the other pi.
I then looked into establishing a socket connection between the two and landed on the following setup.
Are there any other neat ways to do this?
It basically attempts a connection and alerts me if unsuccessful. I figure there is no point in sending/receiving stuff due to security concerns. There would be instances of server and client running on each Pi.
Thanks!
'Server' Side
'Client' Side
Disclaimer: This is condensed code. I have logic to handle repetitive alerts and also logic to alert when connection has resumed from failure among other things.
I have two Pi at two different addresses monitoring all sorts of things.
I would like each to check in with the other to ensure connectivity and in the event of a power failure, network failure, etc... alert me with my existing alerting system.
my first thought was to have them email each other periodically but i would haven't really looked into code to check emails and verify its from the other pi.
I then looked into establishing a socket connection between the two and landed on the following setup.
Are there any other neat ways to do this?
It basically attempts a connection and alerts me if unsuccessful. I figure there is no point in sending/receiving stuff due to security concerns. There would be instances of server and client running on each Pi.
Thanks!
'Server' Side
Code: Select all
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #create socket
s.bind(('', 12345)) #bind port
s.listen(1) #set to listen
while 1:
conn, addr = s.accept() #accept connection
Logit(str(addr)) # Custom log function
conn.close() #close connectionCode: Select all
import socket
while 1:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create socket
s.connect((mydns.dyndns.com, 12345)) # connect
s.close() # close socket
except socket.error: #catch socket error (failed connection)
sendalert('mydns.dyndns.com not reachable!') #personal alert function
time.sleep(3600) #try again later