bazbax (foobarbazbax) wrote,

Automatically connect EVDO card on insert on MacBook Pro

I bought a card for my MacBook Pro laptop that will connect to the high speed EVDO network. I've been annoyed that it takes about eight clicks to connect after insert the card. This should happen automatically whenever I insert the card. Here is a Perl script to automate that.

Before you can run this script you need to install some prereqs. Run these two commands in terminal:

sudo cpan -i File::Tail
sudo cpan -i Mac::AppleScript

Here is the script that you should write to /usr/local/bin/modem_watchdog
#!/usr/bin/perl

use strict;
use warnings;

use File::Tail;
use Mac::AppleScript qw(RunAppleScript);


my $file = File::Tail->new( name => '/var/log/system.log', interval => 1, maxinterval => 1, adjustafter => 2 );
while ( my $line = $file->read ) {
        if ( $line =~ /SMSIWirelessUSB: USB Modem driver loaded./ ) { 
                print "Verizon Wireless card detected.  Setting location!\n";
                print `scselect 'NationalAccess - BroadbandAccess'`;
                print "Connecting Via PPP\n";
                RunAppleScript(qq(
tell application "Internet Connect"
        connect
        quit
end tell
                )) or die "Didn't work!";
        }
        if ( $line =~ /SMSIWirelessUSB: USB Data driver unloaded./ ) { 
                print "You removed the card.  Setting location back to Automatic\n";
                print `scselect 'Automatic'`;
        }
}



To run the script automatically at startup, put this into /Library/LaunchDaemons/com.foobarbazbax.modem_watchdog.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd >
<plist version="1.0">
        <dict>
                <key>Label</key>
                <string>com.foobarbazbax.modem_watchdog</string>

                <key>ProgramArguments</key>
                <array>
                        <string>/usr/local/bin/modem_watchdog</string>
                </array>

                <key>RunAtLoad</key>
                <true/>
        </dict>
</plist>


The script polls the system log in /var/log/system.log and looks for the string that says the card has been inserted. Thanks to Reid for that idea. Reid agrees there is probably a better to fire off a script whenever the PC Card is inserted. If you know of one please comment below.

When the card is inserted it changes the location to the name of the card, "NationalAccess - BroadbandAccess", just like if you went into system preferences -> network and changed it there. Then it uses some AppleScript to start the PPP connection.

When you remove the card it sets the Location to "Automatic"

Let me know if you find this useful.
  • Post a new comment

    Error

    default userpic
  • 0 comments