Salamek / huawei-lte-api

API For huawei LAN/WAN LTE Modems
GNU Lesser General Public License v3.0
376 stars 92 forks source link

Lost Internet Connection #210

Closed KevinRoebert closed 6 months ago

KevinRoebert commented 6 months ago

I'm not sure if it's because of this tool or something else. What I have noticed, however, is that when I use netkeeper or my self-written script (see below), the internet connection drops after five to six calls (crontab every minute). What is very strange about this is that I can ping IP addresses, but cannot open a website or SSH connections etc. This does not work even if I use the IP address directly (i.e. no DNS error).

My device: B818-263

My script:

from huawei_lte_api.Client import Client
from huawei_lte_api.Connection import Connection
from huawei_lte_api.enums.client import ResponseEnum

import requests

# constants
USER = ''
PASSWORD = '
HOST = '192.168.8.1'

WEBSITES = ['https://apple.com', 'https://google.com']

def check_internet_connection(websites, timeout=5, threshold=1):
    successful_connections = 0

    for site in websites:
        try:
            response = requests.get(site, timeout=timeout)

            if response.status_code == 200:
                successful_connections += 1
                print(f"Verbindung zu {site} erfolgreich!")
                if successful_connections >= threshold:
                    return True
        except Exception:
            print(f"Keine Verbindung zu {site} moeglich.")

    return False

if __name__ == "__main__":
    with Connection('http://{}:{}@{}/'.format(USER, PASSWORD, HOST)) as connection:
        client = Client(connection)

        if check_internet_connection(WEBSITES, 10, 1):
            print("Internetverbindung vorhanden.")
        else:
            print("No Internet connection.")
            print('Disabling dialup...')
            if client.dial_up.set_mobile_dataswitch(0) == ResponseEnum.OK.value:
                print('Disabling dialup: OK')
            else:
                print('Disabling dialup: Error')
            print('Enabling dialup...')
            if client.dial_up.set_mobile_dataswitch(1) == ResponseEnum.OK.value:
                print('Enabling dialup: OK')
                if not check_internet_connection(WEBSITES, 10, 1):
                    print("Restarted required. Dialup was not successfully.")
                    if client.device.reboot() == ResponseEnum.OK.value:
                        print('Reboot requested successfully')
                    else:
                        print('Error on requesting reboot')
            else:
                print('Enabling dialup: Error')
Salamek commented 6 months ago

Hmmi have no idea what is causing this for you, i never had any issues while running netkeeper... (if i understand you correctly, your connection goes bananas when you do ~5 "i'm online" checks?) Those online checks does not have anything to do with the modem... netkeeper is using ping, your script is using http check... maybe it is caused by something else and this is just coincidence? IDK sounds weird...

KevinRoebert commented 6 months ago

Okay. Thank you. I've found out that I get the following error in with netkeeper:


* netkeeper.service - Netkeeper Service
     Loaded: loaded (/lib/systemd/system/netkeeper.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2024-05-04 20:46:12 UTC; 18min ago
   Main PID: 26701 (netkeeper)
      Tasks: 1 (limit: 38293)
     Memory: 20.5M
        CPU: 365ms
     CGroup: /system.slice/netkeeper.service
             `-26701 /usr/bin/python3 /usr/bin/netkeeper run --config_prod --log_dir /var/log

May 04 20:53:28 netkeeper netkeeper[26701]: Loading config from /etc/netkeeper/config.yml
May 04 20:53:28 netkeeper netkeeper[26701]: W0504 20:53:28.733 26701 netkeeper.py:282] Connection error rate reached threshold
May 04 20:53:28 netkeeper netkeeper[26701]: W0504 20:53:28.933 26701 netkeeper.py:294] Modem thinks its connected, sleeping for 1 minute...
May 04 21:03:55 netkeeper netkeeper[26701]: Exception ignored in: <function MultiPing.__del__ at 0x7f89d4003760>
May 04 21:03:55 netkeeper netkeeper[26701]: Traceback (most recent call last):
May 04 21:03:55 netkeeper netkeeper[26701]:   File "/usr/lib/python3/dist-packages/netkeeper/ext/multiping.py", line 472, in __del__
May 04 21:03:55 netkeeper netkeeper[26701]:     self._sock.close()
May 04 21:03:55 netkeeper netkeeper[26701]: AttributeError: 'MultiPing' object has no attribute '_sock'
May 04 21:03:55 netkeeper netkeeper[26701]: W0504 21:03:55.957 26701 netkeeper.py:282] Connection error rate reached threshold
May 04 21:03:56 netkeeper netkeeper[26701]: W0504 21:03:56.157 26701 netkeeper.py:294] Modem thinks its connected, sleeping for 1 minute...```
Salamek commented 6 months ago

That most likely happens due to DNS resolution failure in multiping (that is happening before self._sock is defined) but del is called always... This should be detected as network error by netkeeper (with is most likely is) so that is not the issue.