Hello,
I have this code for checking modem connection
why ?
what did I do wrong , and why the code won't continue ?
maybe because the
Thanks,
I have this code for checking modem connection
import os
import sys
import time
import datetime
import requests
import threading
import socket
import netifaces as ni
import schedule
Error_Count = 0
def get_ip_address():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
return s.getsockname()[0]
def CheckConnection():
#threading.Timer(60.0, CheckConnection).start() # called every minute
global Error_Count
address_check = '8.8.8.8'
response = os.system('ping -c 2 ' + address_check)
if response == 0:
pingstatus = "Network Active"
print(pingstatus, "count: ", str(Error_Count))
else:
print(f'Count is {Error_Count}')
if Error_Count == 3:
#os.system('sudo shutdown -r now')
print('put here reboot command if nothing works , need to check remove route maybe$
Error_Count = 0
else:
errorTime = datetime.datetime.now()
pingstatus = (str(errorTime) + " : Netwrok Down!")
OutInterface = get_ip_address()
print('trying to go out using : ' + OutInterface)
time.sleep(2)
try:
boot_response = os.system('bash /bin/modem_reset >> /home/pi/logs/modem_restar$
except Exception as e2:
print(e2)
print(str(boot_response) + '\n\r Modem shoud be up , see IP')
time.sleep(5)
# ni.ifaddresses('wwan0')
try:
Wan_ip = ni.ifaddresses('wwan0')[ni.AF_INET][0]['addr']
except ValueError as ve1:
print(ve1)
Wan_ip='x.x.x.x'
else:
print('Wan ip - ', Wan_ip)
try:
Vpn_ip = ni.ifaddresses('tun0')[ni.AF_INET][0]['addr']
except ValueError as ve:
print(ve)
Vpn_ip='y.y.y.y'
else:
print('vpn ip - ', Vpn_ip)
Error_Count += 1
#return pingstatus
#threading.Timer(60.0, CheckConnection).start() # called every minute
schedule.every(1).minutes.do(CheckConnection)
while True:
schedule.run_pending()
time.sleep(1)whenever he get to the try-catch for printing wan_ip - I get this error File "/home/pi/Documents/CheckConnection_new1.py", line 69, in <module>
schedule.run_pending()
File "/usr/local/lib/python3.7/dist-packages/schedule/__init__.py", line 780, in run_pending
default_scheduler.run_pending()
File "/usr/local/lib/python3.7/dist-packages/schedule/__init__.py", line 100, in run_pending
self._run_job(job)
File "/usr/local/lib/python3.7/dist-packages/schedule/__init__.py", line 172, in _run_job
ret = job.run()
File "/usr/local/lib/python3.7/dist-packages/schedule/__init__.py", line 661, in run
ret = self.job_func()
File "/home/pi/Documents/CheckConnection_new1.py", line 47, in CheckConnection
Wan_ip = ni.ifaddresses('wwan0')[ni.AF_INET][0]['addr']
KeyError: 2and it's finish the code why ?
what did I do wrong , and why the code won't continue ?
maybe because the
schedule.run_pending()not in try-catch also ?
Thanks,
