POSIX_Scripts/modem-manager

65 lines
2.2 KiB
Bash
Executable File

#!/bin/sh
# We need root to configure interface properties.
if [ "$(whoami)" != root ]
then printf "We need root!\n"
exit
else
# Has the interface even been initialized? If not, do so.
while [ ! -e /dev/cdc-wdm0 ]
do printf "3\n" >> /sys/devices/pci0000:00/0000:00:14.0/usb2/2-3/bConfigurationValue
done
# Get the name of our interface.
wif=$(ifconfig -l | tr ' ' '\n' | grep ww)
# Establish a link, but only if one is not already available.
if [ -e /tmp/mbim-network-state-cdc-wdm0 ]
then printf "Connection already established.\nBringing down...\n"
printf "Closing netlink...\n"
mbim-network /dev/cdc-wdm0 stop >> /dev/null
printf "Bringing down interface...\n\n"
ifconfig "${wif}" down >> /dev/null
exit
else printf "Turning on radio...\n"
mbimcli --device=/dev/cdc-wdm0 --device-open-proxy --set-radio-state=on >> /dev/null
printf "Bringing up interface...\n"
ifconfig "${wif}" up >> /dev/null
printf "Opening netlink...\n\n"
mbim-network /dev/cdc-wdm0 start >> /dev/null
fi
# Store the result of the query so that we don't have to call it constantly
printf "Querying for network info...\n"
mbimcli -d /dev/cdc-wdm0 -p --query-ip-configuration > /tmp/qic.tmp
# Get our IPv4.
ip=$(sed -ne "s/IP \[0\]\: '\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\/[0-9][0-9]\)'/\1/p" /tmp/qic.tmp | sed 's/ //g' | cut -d '/' -f 1)
# Get our CIDR netmask and convert to decimal.
nm=255.255.255.$(((0xFFFFFFFF >> $(sed -ne "s/IP \[0\]\: '\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\/[0-9][0-9]\)'/\1/p" /tmp/qic.tmp | sed 's/ //g' | cut -d '/' -f 2)) ^ 0xFF))
# Get our gateway.
gw=$(sed -ne "s/Gateway: '\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)'/\1/p" /tmp/qic.tmp|sed 's/ //g')
# Get our MTU.
mtu=$(sed 7!d /tmp/qic.tmp | sed -ne "s/MTU: '\([0-9]*\)'/\1/p" | sed -ne 's/ //gp')
printf "IP is %s\nNM is %s\nGW is %s\nMTU is %s\n\n" "${ip}" "${nm}" "${gw}" "${mtu}"
# Actually set up the connection.
printf "Assigning interface details...\n"
ifconfig "${wif}" "${ip}" netmask "${nm}"
printf "Routing through cellular gateway...\n"
route add default gw "${gw}"
# Test connection.
printf "Testing interface.\n\n"
ping -c 4 sdf.org
printf "\n"
rm /tmp/qic.tmp
fi
exit