# Huawei usb 3g modem reset – linux

> Reliably reset a Huawei USB 3G modem on Linux: a persistent udev symlink, xHCI USB bus rebind and an AT reset script with the right timings.

- URL: https://guillaumeplayground.net/huawei-usb-3g-modem-reset-linux/
- Author: Guillaume (https://guillaumeplayground.net/about/)
- Published: 2016-06-28
- Updated: 2017-01-10
- Section: Computing & hacking
- Topics: debian, ubuntu
- Source: Guillaume playground

A small memo to manage usb reset for Huawei usb 3g modem:

Get a persistant udev device:  
Edit: /etc/udev/rules.d/99-usb-modem.rules

```
SUBSYSTEM=="tty", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="1003", SYMLINK+="usbmodem"
```

Reset the usb bus (quite rude but it is the only way i found to refresh the stack succesfully each time) and issue a ‘AT’ reset.

```
#!/bin/sh

# Reset USB bus
for i in $(ls /sys/bus/pci/drivers/xhci_hcd/|grep :); do
 echo "$i" >/sys/bus/pci/drivers/xhci_hcd/unbind
 sleep 10
 echo "$i" >/sys/bus/pci/drivers/xhci_hcd/bind
done
sleep 10

# Reset modem
chat -t 1 -e "" '\pAT' OK AT+CFUN=1,1 '\pAT' OK >> /dev/usbmodem < /dev/usbmodem
sleep 20
```

Sleep timers are important.

I’m using huawei 3g modem for backup connections to my datacenters.  
If the external network is detected down, the usb modem pops up, creates a ppp connection, and initiates a ssh forward service on a remote server (which owns a static IP on another network).

```
/usr/bin/ssh -o ServerAliveInterval=10 -o ServerAliveCountMax=2 \
        -o StrictHostKeyChecking=no -o ConnectTimeout=10 \
        -i <MY_SSH_KEY> \
        -C -g -R 0.0.0.0:<MY_SSH_SERVICE_PORT>:localhost:22 \
        <LOGIN>@<HOST> sleep 3600;
```

Like this, i can access my network via a simple:

```
ssh -p <MY_SSH_SERVICE_PORT> <LOGIN_ON_UNREACHABLE>@<HOST>
```
