LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   LinuxQuestions.org Member Success Stories (http://www.linuxquestions.org/questions/linuxquestions-org-member-success-stories-23/)
-   -   Automated smartphone network connection with easytether (http://www.linuxquestions.org/questions/linuxquestions-org-member-success-stories-23/automated-smartphone-network-connection-with-easytether-843343/)

Dark_Helmet 11-09-2010 08:17 PM

Automated smartphone network connection with easytether
 
I have a first generation Motorola Droid. I bought the easytether app from the Android market. Tested it, liked it, and decided to try using easytether for my general internet connection. It's good for casual use (web, email, and even Hulu), but it's not so great for downloading huge files; for example, your connection is lost on an incoming call.

I know there are other alternatives out there, but other than pdanet, they require rooting your phone. I don't want to root my phone just yet, and I'm not going to change my mind anytime soon. :)

I managed to configure my system (Ubuntu-server 10.04) to (1) establish the network connection automatically when the phone is connected; (2) share the connection with other computers on my LAN; and (3) wrote a script to reconnect whenever a disconnect occurs.

Even though I could only test the scripts against my Droid, this approach ought to work for any Android phone with easytether installed.

To start, let me give the scripts I wrote. Perhaps someone smarter than me could merge them, but this works:

===== Scripts =====

/usr/local/bin/easyconnect
Code:

#!/bin/bash

##########################################################################
#
# easyconnect
#
# A script intended to help automate the creation of a network connection
# (and maintain that connection) to a smartphone with the easytether app
# installed on it.
#
# Originally written by Dark_Helmet at LinuxQuestions.org
#
##########################################################################

# --- Commands with absolute paths
BASENAME="/usr/bin/basename"
DATE="/bin/date"
DHCLIENT="/sbin/dhclient"
EASYDHCP="/usr/local/bin/easydhcp"
EASYTETHER="/usr/bin/easytether"
GREP="/bin/grep"
IFCONFIG="/sbin/ifconfig"
SLEEP="/bin/sleep"

# --- Customizable script variables
CONNECT_ERROR="connection refused by the smartphone (is USB mode enabled in EasyTether?)"
DATE_FORMAT="+[%D %T]"
ENUMERATE_ERROR="no compatible smartphones detected (is USB debugging enabled in Settings -> Applications -> Development?)"
INTERFACE="easytether0"
LOG_FILE="/var/log/easyconnect/easyconnect.log"

##########################################################################
#
# Start of the script's main body
#
##########################################################################

# Mark the time we started the script in the log and other useful info
echo "$( ${DATE} "${DATE_FORMAT}" ) $( ${BASENAME} ${0} ) start" >> ${LOG_FILE}
echo "Running as: ${USER} - PID: $$" >> ${LOG_FILE}

# Initialize some variables
error_count=0
easytetherid="start"

# The phone will occasionally disconnect for unexplained reasons
# This loop will repeatedly re-connect following those disconnects as
# long as there is a "enumerable" device connected to the machine
while [ "${easytetherid}" != "${ENUMERATE_ERROR}" ] ; do

  # Check that easytether can enumerate/find a usable device
  easytetherid=$( ${EASYTETHER} enumerate )
  if [ "${easytetherid}" = "${ENUMERATE_ERROR}" ] ; then
    exit 0
  fi

  # Check that there is no network interface already up and running
  easytetherif=$( ${IFCONFIG} | ${GREP} ${INTERFACE} )
  if [ -n "${easytetherif}" ] ; then
    exit 0
  fi

  # Launch the helper script to bring up the easytether0 net interface
  ${EASYDHCP} &

  # Minimal log messages to display:
  #  1.  when the connect attempts began: [<date & time>]
  #  2.  the number of failed connection attempts
  # Please note, these log messages will not be flushed to the log file
  # until a successful connection is made (because the newline is
  # stripped from the echo commands to give the "1..2..3.." format).
  if [ ${error_count} -eq 0 ] ; then
    echo -n "$( ${DATE} "${DATE_FORMAT}" ) - Connect" >> ${LOG_FILE}
  elif [ ${error_count} -eq 1 ] ; then
    echo -n " [Error: 1" >> ${LOG_FILE}
  else
    echo -n "..${error_count}" >> ${LOG_FILE}
  fi

  # Establish the connection
  easytetherconn=$( ${EASYTETHER} connect 2>&1 )
  if [ "${easytetherconn}" = "${CONNECT_ERROR}" ] ; then
    let error_count=error_count+1
    ${SLEEP} 4
  else
    if [ ${error_count} -ne 0 ] ; then
      echo "]" >> ${LOG_FILE}
    else
      echo "" >> ${LOG_FILE}
    fi
    error_count=0
  fi

done

# Mark when the script ends. A "good" run presumably ends when the user
# unplugs the phone--leaving no other device to connect with
echo "$( ${DATE} "${DATE_FORMAT}" ) $( ${BASENAME} ${0} ) stop" >> ${LOG_FILE}
echo "" >> ${LOG_FILE}

exit 0

/usr/local/bin/easydhcp
You may want to replace all of this with a simple "sudo dhclient easytether0" depending on your setup.
Code:

#!/bin/bash

##########################################################################
#
# easydhcp
#
# A helper script for easyconnect. The name is a historical carryover.
# Originally, the script issued a "sudo dhclient easytether0" command--
# hence the name. It no longer does so, but I kept the name.
#
# Originally written by Dark_Helmet at LinuxQuestions.org
#
##########################################################################

# --- Commands with absolute paths
EASYTETHER="/usr/bin/easytether"
ECHO="/bin/echo"
GREP="/bin/grep"
IFCONFIG="/sbin/ifconfig"
PS="/bin/ps"
ROUTE="/sbin/route"
SLEEP="/bin/sleep"

# --- Customizable script variables
INTERFACE="easytether0"
EASY_LOCAL_IP="192.168.117.2"
EASY_NETMASK="255.255.255.0"
EASY_GATEWAY="192.168.117.1"

##########################################################################
#
# Start of the script's main body
#
##########################################################################

# Give time to the launching script to establish the connection
${SLEEP} 2

# Check that an "easytether connect" process is running
active_connection=$( ${PS} aux | \
                    ${GREP} "${EASYTETHER} connect" | \
                    ${GREP} -v "${GREP}" )

# Exit if no such process is active
if [ -z "${active_connection}" ] ; then
  exit 1
fi

# Setup the details of the network interface
# Most of these were taken from direct observation when manually
# executing the recommended "sudo dhclient easytether0" command
# fromt the easytether developers.
${IFCONFIG} ${INTERFACE} ${EASY_LOCAL_IP} netmask ${EASY_NETMASK} up
${ROUTE} add default gw ${EASY_GATEWAY} ${INTERFACE}


exit 0

Easyconnect is the main script. It calls easydhcp as a helper. The easyconnect script spins in a loop, attempting to make a connection as long as a device is available to connect. Note: if you use these scripts, create the /var/log/easyconnect directory or change the log file location. Also note: some of these commands require root access; run the easyconnect script as root or give NOPASSWD privileges in sudoers for the appropriate commands to whatever user you run the script as.

===== Udev =====

Ok, so, the scripts by themselves are the bulk of the work. You can kick off easyconnect manually and be perfectly happy. If you want complete automation, you need udev. A quick-and-dirty guide to adding a udev rule can be found on this page.

Just follow their general process. I ended up with the following as my udev rule:

/etc/udev/rules.d/81-droid-easytether.rules
Code:

KERNEL=="sd?", ATTRS{idVendor}=="22b8", ATTRS{idProduct}=="41db", ATTRS{serial}=="FFFFFFFFFFFFFFFF", SYMLINK+="motodroidv1", RUN+="/usr/local/bin/easyconnect"
Keep in mind, that is one line in the file, and the underlined values are what you need to discover using udevadm (as described in the link).

Note: (1) the serial number above is bogus because I'm paranoid about publishing such unique identifying info on the web; (2) You do not need the SYMLINK, but it could be useful if you mount your phone as storage regularly; (3) change the RUN attribute to point to easyconnect (or your equivalent)

Restart udev. At this point, your setup should automatically establish an internet connection whenever you plug your phone into a USB port for that machine.

===== Connection Sharing =====

If you want to set up connection sharing to allow all the computers on your home network share the phone's internet connection, then take a look at this page.

Anyway, using an almost-fresh Ubuntu server install, I did not have to mess with steps 3-6 on the linked instructions.

Now, to do this, two things need to be configured:
1. The computer that's sharing needs to have a static IP address on your network.
2. You need to know a bit about DNS.

I won't go into either of those guys very much. The static IP address is a fairly simple process and there should be tons of instruction on how to do it with a quick Google search.

Your knowledge of DNS will determine what your easydhcp script needs to do. Earlier, I pointed out that your easydhcp script might simply be changed to "sudo dhclient easytether0." If you do not run a DNS server, then that's probably what you should do. However, I DO run a DNS server on the machine that shares the internet connection. Running dhclient on that machine would interfere with the way I have my network set up. So that is why my easydhcp script is written the way it is.

mjolnir 11-10-2010 05:37 AM

Nice work. I also have a Droid and I may try your script later.

GrapefruiTgirl 11-10-2010 05:49 AM

@ Dark_Helmet,

this is good stuff! I have no doubt that someone(s) will find this very handy. This tethering issue has been coming up more + more over the last year or two, but finding (good) information and knowing where to point an asker for more, are difficult at best.

Perhaps after some time for feedback, code improvements, or whatever, your posting could be made into an article for the LQ Wiki or Linux Tutorials.

Thanks for sharing it!

Dark_Helmet 11-10-2010 08:11 AM

Thank you both!

I should point out two things about the easytether program:

1. There is a free version available in the Android market. The free version is fully functional except for https:// websites. You have to pay for the full app ($10) to access secure websites. So, anyone can give this solution a test-run if they like--without spending a dime.

2. The easytether program comes in two parts: one part that installs on the phone itself and a second part that runs on the host machine. The developers of easytether have a .deb package available (both 32 and 64-bit). This solution does not replace either part. You must install both parts. This solution simply automates the entry of the appropriate commands as soon as the device is connected.

EDIT:
You can download the packages here: Mobile Stream Easytether drivers

More info: these are not source packages--they are binaries. Also, if you inspect the easytether program, it is SUID root. If you leave it as SUID root, you don't need to worry about permissions quite so much. Personally, I cleared the SUID bit and sudo'd whenever I needed to run it.

SantaClawZ 11-22-2010 12:00 AM

I am just beginning to use Linux, and I have a quick question. I have the 2 scripts made, but they aren't in the correct directory. I can't make new folders/files or move files to /usr/local/bin/ because the options are dimmed. Can someone please explain how to put the scripts there? (In newbie terms please)

Dark_Helmet 11-22-2010 01:37 AM

Two questions:
1. What distribution are you using?
2. Is this your personal computer, or does someone else own/manage it (e.g. a corporate/educational IT department)?

EDIT: Nevermind about the distribution. I noticed your post has an Ubuntu logo. So I'll assume that's what you're using. Second question still stands...

SantaClawZ 11-22-2010 03:12 PM

Its my personal laptop running ubuntu 10.10
Also I'm using a Motorola Droid 2, but that shouldn't matter for the scripts.

Dark_Helmet 11-22-2010 03:56 PM

Ok, I'll walk you through it. (Please don't take offense if this is overly simplistic--I'm writing it from the perspective of a random Internet user coming across this post with 0 Linux knowledge).

We're going to use the command line rather than a GUI (because that's how I'm familiar with moving files into these types of places).

1. Start with the top menu:
Applications -> Accessories -> Terminal

You should have a new window with a command prompt. When I give you commands to enter, I'll prefix them with a '$' just to help indicate the end of the command prompt. Don't type the '$' on your end.

2. Now, before we get into the commands, find the script files with the normal window-based file manager. When you find them, there will be a path to the directory in the Location field of the file manager window. For instance, it might be: /home/santaclawz/Documents

3. In the terminal window, change directory to where the scripts are (note: you will need to update the directory based on what you saw in the Location field from step #2--and again, don't type the '$'):
Code:

$ cd /home/santaclawz/Documents
4. Verify that you can see the script files:
Code:

$ ls -l
Note: double-check the names of the script files. If you wrote them with a GUI program, that program may have added a ".txt" extension that you didn't realize.

5. Copy the files using the sudo command. I assume you used the same filenames I did (easyconnect and easydhcp). If you did not use those names, you'll need to modify the command to match what you used:
Code:

$ sudo cp easyconnect easydhcp /usr/local/bin
Note: If you're running as a non-root user (which you should be), you'll be asked for a password. Enter your normal password. The sudo command gives you a one-command root status if you give your correct password.

6. Now we make sure that the scripts are executable, by giving everyone permission to read and execute them:
Code:

$ sudo chmod 755 /usr/local/bin/easyconnect /usr/local/bin/easydhcp
Note: you will not need to enter your password this time--the system will remember you gave the right password earlier, but it will expire after a few minutes.

7. Ok, the scripts are installed. If you need to create the directory for the log file, type this:
Code:

$ sudo mkdir /var/log/easyconnect
That should be it! You should be able to run the easyconnect script from the terminal with:
Code:

$ easyconnect
If you run into problems, let me know.

And again, you'll need to install the easytether drivers I linked to earlier. And you may want easydhcp to run "sudo dhclient easytether0" instead of what I have.

If you (or anybody else) gets it working, please let me know. The more people that get some use out of this, the more likely I'll spend more time refining it. I'm already kicking around the idea of a python script or a C program. But unless there's lots of interest, they'll remain when-I-have-time projects.

SantaClawZ 11-22-2010 04:20 PM

It seems to be working now, thanks :)

I'm working on connection sharing now, will do the udev tomorrow.

I'm trying to share the connection to my ps3 and I've almost got it.

Using EasyTether I've got these settings so far, but I've been unsuccessful
Code:

IP Address: 192.168.117.3
Subnet Mask: 255.255.255.0
Default Router: 192.168.117.2
Primary DNS: 8.8.8.8
Secondary DNS: 8.8.4.4

I'm pretty sure I have the PC end set up correctly.


All times are GMT -5. The time now is 03:01 AM.