#!/bin/bash # ppp or ./optus-dongle # usage: sudo ./ppp [start|status|stop|log] interface=ppp0 logfile=/tmp/optus-dongle.log serialdevice=/dev/ttyUSB0 baudrate=460800 phonenumber=*99# error() { echo "ERROR executing $1" exit 1 } execute() { echo "executing: $1" bash -c "$1" || error "$1" return 0 } ############################################################ if [ $(whoami) != root ] then echo "run script as root" exit 1 fi if [ -z "$(sudo which pppd)" ] echo "need to install point-to-point protocol daemon program (pppd)" exit 2 fi case "$1" in start) # this sets up and runs pppd echo -e "\n\n\n$(/bin/date)">>$logfile echo "########### OPTUS dongle session started ############">>$logfile read -p "attach optus dongle and then press ENTER < " # make sure that the kernel has switched the CDROM mode to USBserial # by checking that it has created the serial USB device /dev/ttyUSB0 echo -n "waiting for $serialdevice to be created ... " serial=notcreated while [ $serial == notcreated ] do [ $(/bin/ls -l 2>/dev/null $serialdevice|wc -l) -gt 0 ] && serial=created done echo "OK" /bin/ls -l 2>/dev/null $serialdevice|tee -a $logfile # put the nameservers that we discovered Optus uses into /etc/resolv.conf echo "establishing primary and secondary nameservers" cat > /tmp/resolv.conf <<"EOF" nameserver 198.142.0.51 nameserver 61.88.88.88 EOF sudo mv /tmp/resolv.conf /etc/resolv.conf cat /etc/resolv.conf|tee -a $logfile # before we connect, make sure this ppp0 interface is protected if ! -x /usr/local/bin/iptables.init ] then echo "need to start firewall for ppp0 interface" exit 3 fi read -p "press ENTER to start firewall < " sudo /usr/local/bin/iptables.init ppp0|tee -a $logfile|less read -p "press ENTER to start pppd session < " echo -e "\a" read -p "enter PIN (leave empty if no PIN) < " PIN if [ ! -z "$PIN" ] then echo -e "AT+CPIN=${PIN}\r">$serialdevice sleep 1 fi echo "using serial device $serialdevice"|tee -a $logfile echo "using baud rate $baudrate"|tee -a $logfile echo "using phone number $phonenumber"|tee -a $logfile # run the normal pppd, and note the spaces between '' and ATZ (etc) sudo /usr/sbin/pppd $serialdevice $baudrate noauth debug connect "/usr/sbin/chat -v '' ATZ OK ATDT${phonenumber} CONNECT '\d\c'" # wait until the /sbin/ifconfig command shows that ppp0 has been created echo -n "waiting for interface $interface to be created ... " ppp=pending while [ $ppp == pending ] do [ $(/sbin/ifconfig 2>/dev/null $interface|grep -v error|wc -l) -gt 0 ] && ppp=ready sleep 1 done echo "OK" # wait until the /sbin/ifconfig command shows a valid inet address is assigned echo -n "waiting for IP address ... " ipaddress=pending while [ $ipaddress == pending ] do [ $(/sbin/ifconfig $interface|grep 'inet addr'|wc -l) -gt 0 ] && ipaddress=ready sleep 1 done echo "OK" /sbin/ifconfig $interface|grep 'inet addr'|tr -s ' ' sleep 3 /sbin/ifconfig $interface|tee -a $logfile # if there are any other interfaces, make sure this is the default one sudo /sbin/route add default ppp0 sudo /sbin/route -n echo "now may access internet: browser, w3m, apt-get, etc." echo "run 'ppp log' to see /var/log/ppp.log" echo "run 'ppp stop' to finish this ppp session" ;; status) # show the state of the ppp0 interface /sbin/ifconfig $interface ;; stop) # get traffic on the serial ppp0 interface /sbin/ifconfig $interface|grep 'RX bytes'|tr -s ' '|tee -a $logfile echo "########### ppp session finished ############"|tee -a $logfile sudo kill $(pidof pppd) echo "when solid blue light goes out, you may disconnect dongle" echo "run 'ppp log' to examine $logfile" ;; log) # show the details of the pppd negotiation first if [ ! -e /var/log/ppp.log ] then echo "you have not yet set up ppp logging" echo "refer to http://axion.physics.ubc.ca/ppp-linux.html" else sudo tail -100 /var/log/ppp.log|less fi # show the results of running this ppp session next less $logfile ;; *) echo "usage: sudo ./ppp [start|status|stop|log]" exit 0 ;; esac exit 0