I'm working on a script that checks if a certain device is connected to the network. If it is connected the device should get a message via email. It works flawlessly on windows, however, when I run it on my raspberry pi it just doesn't work. Can someone tell my why it doesn't work and if it is possible to run this on Linux?
Here's the code:
import smtplib, subprocess, time
host = "192.168.2.10"
x = True
server = smtplib.SMTP( "smtp.gmail.com", 587 )
server.starttls()
server.login( '[email protected]', 'password' )
while True:
try:
response = subprocess.getoutput(['ping', host])
except:
print("An error occured")
if('Destination host unreachable' in response or 'Request timed out' in response and x == True):
print("Device isn't connected")
x = False
elif('Destination host unreachable' in response or 'Request timed out' in response and x == False):
print("Device is still not connected")
elif('Destination host unreachable' not in response and x == False):
print("Device just connected")
x = True
try:
server.sendmail( 'Raspberry Pi', '[email protected]', 'Hello! ')
except:
print("An error occured. Message wasn't send")
x = False
elif('Destination host unreachable' not in response and x == True):
print("Device is still online")
else:
print('Something went wrong with the if statements.')
time.sleep(1)
Edit: If I run it on my raspberry and print the response variable it shows just the 'ping' menu. When you write 'ping' inside a command line you'll get all the options you have with that command. I get the same result while running this script. It somehow doesn't get the second parameter of the subprocess.getoutput. Here's the screenshot from the command line