[info] N13U-B1 Auto-switch wan to 3g on plugging USB modem

Post new topic   Reply to topic    DD-WRT Forum Index -> Ralink SoC based Hardware
Author Message
AZERTY123
DD-WRT User


Joined: 07 Mar 2012
Posts: 57

PostPosted: Mon Apr 30, 2012 18:57    Post subject: [info] N13U-B1 Auto-switch wan to 3g on plugging USB modem Reply with quote
I know - there are Dual WAN Standby/failover/load Balancing etc.
I wanted a simple way of automatically connecting to 3g when modem is plugged in and switch back to previous mode when it detached.
I don't know what linux is.. So don't be harsh on my poor coding. Any constructive comments appreciated.


Basics: wrote:
1.You need to set up the 3g modem in the web interface.
2.Select "3G/UMTS/4G/LTE" in Setup>Basic Setup>WAN Connection Type.
3.Ensure that your modem connects automatically.


Now comes the scripting part:

Administration>Commands>Command Shell wrote:

Code:
#!/bin/sh
#Make script infinite loop
while :
do
MODEM_CONNECTED=$(usb_modeswitch usb_modeswitch -v 12d1 -p 1505  -W |grep "found matching")
MODEM_CONNECTED=$( echo $MODEM_CONNECTED |cut -c1-5)
HSP_STATE=$(nvram get wan_proto)


echo $MODEM_CONNECTED"#"$HSP_STATE

if [ "$MODEM_CONNECTED" = "found" ]; then
echo "modem found"
   if [ "$HSP_STATE" != "3g" ]; then
   nvram set wan_proto="3g"
   echo "modem present but wan not 3g, we will make it 3g"
   nvram set OLD_WAN=$HSP_STATE
   nvram commit
   reboot
   # we need to remember old WAN type, store in nvrm and reboot for quick 3g connection.
   else
   echo "modem present and wan 3g - nothing to do"
   fi
else
# "modem not found-we will make wan dhcp if not already dhcp"
# not working well - state change not reflected - so we hard code instead of  $OLD_WAN
  if [ "$HSP_STATE" != "dhcp" ]; then
   echo "commit dhcp"
   nvram set wan_proto="dhcp"
   nvram commit
   reboot
   else
   echo "No modem and wan already dhcp-nothing to do"
  fi
fi
sleep 4
done


This will start the above script after every boot:
Save as Firewall script wrote:

Code:
chmod 755 /tmp/custom.sh
sh /tmp/custom.sh


Notes wrote:


    1.Replace "12d1" with your modem's VID and "1505" with your modem's unswitched PID.
    2.nvram commit and reboot can be avoided for non-buggy non-stubborn modems which don't need manual modeswitching to work. (I have not included modeswitch script here).
    3.Replace "dhcp" with any other mode of your choice.
    Other options are
    Disabled="disabled"
    Static IP="static"
    Automatic Configuration - DHCP="dhcp"
    PPPoE="pppoe"
    etc
    4.If you are selecting any option other than dhcp then you need to ensure that all the options are set up eg. the static ip / subnet /gateway

------------------
If successful the router reboots and connects the modem to internet and when detached reboots and connects to the previous connection.
------------------




To do:
cat /proc/bus/usb/devices to get the device ID may consume less resources?
Is it harmful to nvram commit every time we plug in the modem? Any alternatives to store it permanently?
Tried retrieving the previous mode (rather than hard coding) and storing it in a variable, but doesn't seem to be reliable.


Last edited by AZERTY123 on Tue May 01, 2012 4:52; edited 1 time in total
Sponsor
AZERTY123
DD-WRT User


Joined: 07 Mar 2012
Posts: 57

PostPosted: Tue May 01, 2012 4:48    Post subject: Reply with quote
Newer version, less CPU (Remove modeswitch part if not needed wrote:

Code:

#!/bin/sh
#Make script infinite loop
while :
do
MODEM_STRING=$(echo `cat /proc/bus/usb/devices | grep 12d1`)
MODEM_CONNECTED=$(echo $MODEM_STRING | cut -c11-14)
MODEM_SWITCHED=$(echo $MODEM_STRING | cut -c23-26)

HSP_STATE=$(nvram get wan_proto)

echo $MODEM_CONNECTED"#"$HSP_STATE"##"$MODEM_SWITCHED

if [ "$MODEM_CONNECTED" = "12d1" ]; then
echo "modem found"
   if [ "$HSP_STATE" != "3g" ]; then
   nvram set wan_proto="3g"
   echo "modem present but wan not 3g, we will make it 3g"
   nvram commit
   reboot
   # we need to remember old WAN type, store in nvrm and reboot for quick 3g connection.
   else
   echo "modem present and wan 3g - nothing to do"
   fi
else
# "modem not found-we will make wan dhcp if not already dhcp"
# not working well - state change not reflected - so we hard code instead of  $OLD_WAN
  if [ "$HSP_STATE" != "dhcp" ]; then
   echo "commit dhcp"
   nvram set wan_proto="dhcp"
   nvram commit
   reboot
   else
   echo "No modem and wan already dhcp-nothing to do"
  fi
fi
# mode switching for stubborn modems - replace the vids and pids
   if [ "$MODEM_SWITCHED" != "140b" ]; then
   usb_modeswitch -v 12d1 -p 1505 -V 12d1 -P 140b -d -Q
   sleep 10
   usb_modeswitch -v 12d1 -p 1505 -V 12d1 -P 140b -M "55534243123456780000000000000011062000000100000000000000000000" -n 1 -W -i 0 -Q
   else
   echo modem already switched
   fi
sleep 4
done

Display posts from previous:    Page 1 of 1
Post new topic   Reply to topic    DD-WRT Forum Index -> Ralink SoC based Hardware All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum