Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.