Skip to content

Instantly share code, notes, and snippets.

@KyleJamesWalker
Created July 9, 2019 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KyleJamesWalker/08ea1ec9aec730ec738d491f99be232b to your computer and use it in GitHub Desktop.
Save KyleJamesWalker/08ea1ec9aec730ec738d491f99be232b to your computer and use it in GitHub Desktop.
Huawei Modem DHCP Details
You can get the string manually with the following:
screen /dev/ttyUSB0
ATZ
AT^NDISDUP=1,1,"h2g2"
AT^DHCP?

# This responds with something like:
#  ^DHCP:11111111,22222222,33333333,44444444,55555555,66666666,100000000,50000000
# 
# Note without cell tower responds with:
#  +CME ERROR: no connection to phone
#
# Exit with
#  ctrl+a,\,y

Based on: https://gist.github.com/jmacego/342c13f9275c9124c34208b9486d8eea

This script uses pyserial and get everything directly from the modem. I plan on making this a full tool to modify the routing next.

import serial
import socket
import struct
def process(ser, cmd):
"""Send and recieve a command"""
ser.timeout = 1.0
if ser.inWaiting() > 0:
ser.flushInput()
ser.write(str.encode(cmd + '\r'))
msg = ser.read(1024)
data = [x for x in msg.decode("utf-8").replace('\r', '\n').split('\n') if x and x != cmd]
if data[-1] != 'OK':
raise RuntimeError("Unexpected response", data)
return data[0]
ser = serial.Serial('/dev/ttyUSB0', 9600)
process(ser, 'ATZ')
process(ser, 'AT^NDISDUP=1,1,"h2g2"')
response = process(ser, 'AT^DHCP?')
ips = []
hex_addresses = response.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("sudo ip address add {}/{} dev wwan0".format(ips[0], netmask))
print("sudo 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