Skip to content

Instantly share code, notes, and snippets.

@aslafy-z
Last active November 1, 2023 22:59
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aslafy-z/a78767e963ce90d7aeeff4ffba4bf9dc to your computer and use it in GitHub Desktop.
Save aslafy-z/a78767e963ce90d7aeeff4ffba4bf9dc to your computer and use it in GitHub Desktop.
Forward SMS from Huawei LTE router to another number

Forward SMS from Huawei LTE router to another number

Dependencies

Usage

export HUAWEI_ROUTER_IP_ADDRESS="" # Router IP address
export HUAWEI_ROUTER_ACCOUNT="" # Router Username
export HUAWEI_ROUTER_PASSWORD="" # Router Password
export TARGET_PHONE_NUMBERS="" # Phone numbers to forward to (international format, coma separated list)
export DELAY_WAIT_SECONDS="" # Delay between two checks (when there is no new SMS)
export DELAY_LOOP_SECONDS="" # Delay between two SMS read

python3 sms-forwarder.py
import sys
import time
from os import environ
from huawei_lte_api.Client import Client
from huawei_lte_api.AuthorizedConnection import AuthorizedConnection
from huawei_lte_api.Connection import Connection
from huawei_lte_api.api.User import User
from huawei_lte_api.enums.sms import BoxTypeEnum, TextModeEnum
from huawei_lte_api.enums.user import LoginStateEnum
import huawei_lte_api.exceptions
try:
HUAWEI_ROUTER_IP_ADDRESS = environ['HUAWEI_ROUTER_IP_ADDRESS']
HUAWEI_ROUTER_ACCOUNT = environ['HUAWEI_ROUTER_ACCOUNT']
HUAWEI_ROUTER_PASSWORD = environ['HUAWEI_ROUTER_PASSWORD']
TARGET_PHONE_NUMBERS = environ['TARGET_PHONE_NUMBERS'].split(',')
DELAY_LOOP_SECONDS = int(environ.get('DELAY_LOOP_SECONDS', "1"))
DELAY_WAIT_SECONDS = int(environ.get('DELAY_WAIT_SECONDS', "15"))
except:
print('Please provide the required environment variables.')
sys.exit(1)
connection = None
client = None
# Use infinite loop to check SMS
while True:
try:
# Establish a connection with authorized
connection = AuthorizedConnection('http://{}:{}@{}/'.format(HUAWEI_ROUTER_ACCOUNT, HUAWEI_ROUTER_PASSWORD, HUAWEI_ROUTER_IP_ADDRESS))
client = Client(connection)
# get first SMS (unread priority)
sms_list = client.sms.get_sms_list(1, BoxTypeEnum.LOCAL_INBOX, 1, 0, 1, 1)
# Skip this loop if no messages
if sms_list['Messages'] == None:
client.user.logout()
time.sleep(DELAY_WAIT_SECONDS)
continue
# Skip this loop if the SMS was read
if int(sms_list['Messages']['Message']['Smstat']) == 1:
client.user.logout()
time.sleep(DELAY_WAIT_SECONDS)
continue
sms = sms_list['Messages']['Message']
# Found a new SMS
print('{Date} Found a new SMS ID:{Message_Index} From:{Phone_Number}'.format(Date=sms['Date'], Message_Index=sms['Index'], Phone_Number=sms['Phone']))
print('Details: {}'.format(sms))
# Skip if SMS is an acknowledgment
if sms['SmsType'] != '7':
# Forward to target numbers
sent_sms = client.sms.send_sms(
phone_numbers=TARGET_PHONE_NUMBERS,
message=sms.get('Content', ''),
sca=sms.get('Sca', ''),
)
# Set SMS as read
client.sms.set_read(sms['Index'])
# Logout
client.user.logout()
time.sleep(DELAY_LOOP_SECONDS)
except huawei_lte_api.exceptions.ResponseErrorLoginRequiredException as e:
pass
except KeyboardInterrupt:
# Force logout on exit
client.user.logout()
print('Exitting...')
sys.exit(0)
except Exception as e:
print('Router connection failed! Please check the settings. \nError message:\n{error_msg}'.format(error_msg=e))
# Force logout on exit
client.user.logout()
print('Exitting...')
sys.exit(1)
@pawnedprawn
Copy link

Hello, im a total noob, how do i use this? Thank you

@aslafy-z
Copy link
Author

@pawnedprawn I updated the markdown file. Let me know if you need more details.

@pawnedprawn
Copy link

Thank you very much for replying. Where do I begin? This will be helpful for me since the modem is far from my home.

@aslafy-z
Copy link
Author

First, ensure your model is compatible by checking https://github.com/Salamek/huawei-lte-api#tested-on.
Then, ensure you have a local network access to your router.
Follow guidance at https://www.192-168-1-1-ip.co/huawei/routers/789/ to check you can connect to the administration panel.
Next, install the python dependencies on the box that will execute this script and set environment variables.
Finally, launch the script.

@isuckatthishelp
Copy link

Hi @aslafy-z!
My routers compatible and I have access to the admin panel but I am also a total noob and not sure how to install the “python dependencies”. I’d like to learn but could you point me in the right direction? Can this be installed directly on the router? Normally I’d just trial and error until I figure it out but don’t want to brick it! Thanks for any help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment