Skip to content

Instantly share code, notes, and snippets.

@me-vlad
Last active March 14, 2024 07:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save me-vlad/2c2822dc679b4bb6f199cd56dfd5ddbe to your computer and use it in GitHub Desktop.
Save me-vlad/2c2822dc679b4bb6f199cd56dfd5ddbe to your computer and use it in GitHub Desktop.
Reboot HUAWEI based GPON
#!/bin/sh
#
# Reboot HUAWEI based GPON with web control
#
IFS=
echo "Enter GPON username:"
read USER
echo "Enter GPON password:"
read -rs PASS
if [ -z "${USER}" ] || [ -z "${PASS}" ]; then
echo "Error: username and password required" && exit 1
fi
PASS64=$(echo -n "${PASS}" | base64)
GPON_URL="http://192.168.100.1"
GPON_RAND_URL="$GPON_URL/asp/GetRandCount.asp"
GPON_LOGIN_URL="$GPON_URL/login.cgi"
GPON_RESET_URL="$GPON_URL/html/ssmp/reset/reset.asp"
GPON_REBOOT_URL="$GPON_URL/html/ssmp/reset/set.cgi?x=InternetGatewayDevice.X_HW_DEBUG.SMP.DM.ResetBoard&RequestFile=html/ssmp/reset/reset.asp"
# get initial token
TOKEN=$(curl -Ss "${GPON_RAND_URL}" | sed 's|^\xef\xbb\xbf||')
# log in and get cookie
if [ -n "${TOKEN}" ]; then
COOKIE=$(curl -c - -Ss \
-b "Cookie=body:Language:english:id=-1" \
-d "UserName=${USER}" \
-d "PassWord=${PASS64}" \
-d "x.X_HW_Token=${TOKEN}" \
"${GPON_LOGIN_URL}" | egrep "Cookie[[:space:]]+sid=" | awk '{print $NF}')
else
echo "Error: missing initial token" && exit 1
fi
# get hw token
if [ -n "${COOKIE}" ]; then
HWTOKEN=$(curl -Ss \
-b "Cookie=${COOKIE}" \
"${GPON_RESET_URL}" \
| grep "hwonttoken" | sed -E 's|^.+value="(\w+)">.*|\1|')
else
echo "Error: missing session cookie" && exit 1
fi
# reboot GPON
if [ -n "${HWTOKEN}" ]; then
curl -Ss -m 5 \
-b "Cookie=${COOKIE}" \
-H "Referer: ${GPON_RESET_URL}" \
-H "Accept-Language: en-US,en;q=0.9,en_US;q=0.8" \
-d "x.X_HW_Token=${HWTOKEN}" \
"${GPON_REBOOT_URL}" 2>/dev/null
else
echo "Error: missing hw token" && exit 1
fi
#!/usr/bin/env -S ANSIBLE_STDOUT_CALLBACK=yaml ansible-playbook
---
# Ansible play to reboot HUAWEI GPON with telnet access
- name: GPON | Reboot HQ GPON ONU
hosts: hq-gpon
become: false
gather_facts: false
vars:
hq_gpon_telnet_user: "{{ vault_hq_gpon_telnet_user }}"
hq_gpon_telnet_pass: "{{ vault_hq_gpon_telnet_pass }}"
vars_prompt:
- name: gpon_reboot
prompt: "Reboot GPON? (yes/no)"
private: false
default: "no"
tasks:
- name: GPON | Get GPON info
telnet:
user: "{{ hq_gpon_telnet_user }}"
password: "{{ hq_gpon_telnet_pass }}"
login_prompt: "Login:"
password_prompt: "Password:"
timeout: 30
prompts:
- "WAP>"
command:
- display optic
- display sysinfo
no_log: true
register: gpon_info
changed_when: false
failed_when: gpon_info.output is undefined or gpon_info.output | length == 0
- name: GPON | Display GPON info
debug:
msg:
- |
"
------------------------------------
Reference Values
------------------------------------
Voltage : 3100 to 3500 mV
Bias : 20 to 90 mA
Temperature : -10 to +85 ℃
RX Optical Power : -30 to -8 dBm
TX Optical Power : 0.5 to 5 dBm
------------------------------------
"
- "{{ gpon_info.output }}"
when: gpon_info.output is defined or gpon_info.output | length > 0
- name: GPON | Reboot GPON
telnet:
user: "{{ hq_gpon_telnet_user }}"
password: "{{ hq_gpon_telnet_pass }}"
login_prompt: "Login:"
password_prompt: "Password:"
timeout: 5
prompts:
- "WAP>"
command:
- reset
no_log: true
ignore_errors: true # noqa ignore-errors
changed_when: false
when: gpon_reboot is defined and gpon_reboot | bool
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment