Skip to content

Instantly share code, notes, and snippets.

@jmacego
Last active June 19, 2018 01:12
Show Gist options
  • Save jmacego/342c13f9275c9124c34208b9486d8eea to your computer and use it in GitHub Desktop.
Save jmacego/342c13f9275c9124c34208b9486d8eea to your computer and use it in GitHub Desktop.
Huawei e397u-53 Connectivity
#!/usr/bin/env python
from __future__ import print_function
from sys import argv
import socket
import struct
"""
Decode the DHCP string given by AT^DHCP? on Huawei USB Modem and
print out appropriate commands to add it statically.
Tested under Python 2.7, 3.5, and 3.6
No warranty of any kind.
"""
if len(argv) >= 2:
response = argv[1]
else:
response = b'^DHCP:0100000a,fcffffff,0200000a,0200000a,8080808,4040808,"\
"100000000,50000000'
print("Please pass the DHCP string to this script, using default example")
print("Run as: python huawei-dhcp-decode.py "\
"^DHCP:0100000a,fcffffff,0200000a,0200000a,8080808,4040808,"\
"100000000,50000000")
ips = []
hex_addresses = response.decode().split(':', 1)[1].split(",")
for i in range(6):
ips.append(socket.inet_ntoa(struct.pack("<L", int(hex_addresses[i],
base=16))))
netmask = sum([bin(int(x)).count('1') for x in ips[1].split('.')])
print("ip address add {}/{} dev wwan0".format(ips[0], netmask))
print("ip route add default via {}".format(ips[2]))
print("#The server recommends these DNS servers: {} and {}".format(ips[4],
ips[5]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment