AppleScript to generate a SecurID token and automatically connect to a Cisco VPN

Apple obviously hates copy and paste. They didn't add it to the iPhone for years, and even when they did add it, they only briefly allowed users to paste into the VPN password field, removing it with one of the first incremental updates to OS 3.0. Luckily there's an app for that – CopyPasteFix, only for jailbroken iPhones.

And then came Snow Leopard, OS X 10.6. They included built-in IPSEC VPN, freeing users from the shackles of the horrible Cisco IPSEC client. But yet again, Apple decided that users don't need to copy and paste text into the password field.

For anyone with a OTP soft token (like a RSA SecurID), this is a pain in the ass. Half the benefits of generating a passcode on the same device that you're using it on, are not having to type it.

Enter AppleScript, everyone's favorite natural language that thinks it's acceptable to use apostrophes when referencing variables. I've never actually written an AppleScript longer than tell application "iTunes" to playpause so this was new ground for me. Joe mentioned he has a similar script for junky non-RSA soft token solution he uses, but he left the country immediately after taunting me with the prospect of getting the code.

If you're doing any moderately serious AppleScript development, I highly recommend Script Debugger from Late Night Software. It was invaluable to seeing what objects and properties were really available, and troubleshooting errors.

What it does

  • Prompts for your SecurID PIN (you can save this in the script, I highly recommend you NOT do that)
  • Launches the SecurID application (it if it's running it will quit it first), generates a token, stores the token in a variable, and then quits the SecurID application
  • Kicks off the VPN connection process, types in the generated passcode when the window is available, and logs in.

If there's any type of a banner with terms to accept, it won't click OK there.

Download the script, view the source code and read more at the project page.

Seagal-arific

A couple weeks ago I announced my intent to codename all my future projects after Steven Seagal movies.

My first Seagal-themed project is off to a roaring start, and I built a custom DMG packaging script purely so I could force the beta testers to look at Seagal's (self described?) "striking and somewhat boyishly handsome" mug while they rush to install the latest build of my project.

Beta testers, this is what you have to look forward to for the next release!

Manipulate a plist from the command line

Since OS X 10.5 a command called PlistBuddy has been available.

It's useful for things like extracting the version number out of your Info.plist during a Run Script Build Phase.

PlistBuddy wasn't in my existing path, I found it at /usr/libexec/PlistBuddy

Example Usage

/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" Info.plist

Outputs: 0.14, or whatever the value of the CFBundleVersion key in Info.plist is.

Fix PHP 5.3 hang on Windows

I just upgraded to PHP 5.3 on my Windows development box, and ran into an issue making any database connection with the MySQL and MySQLi drivers. Pages not making any database connections worked fine, but any page with a database connection would sit and hang for about a minute, and then throw an error.

PHP Warning: mysqli::mysqli(): [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306) in xxx.php on line 2
PHP Warning: mysqli::mysqli(): (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
in xxx.php on line 2

PHP bug #45150 suggests that this is due to issues resolving localhost on Windows. This is partially correct, more specifically it's an issue with localhost resolving to ::1 when IPv6 is enabled.

While it feels like Windows has shoehorned in IPv6 support, the blame seems to lie with MySQL, which won't support IPv6 until version 6.0.

I'm not sure why the timeout isn't instant, the port is closed, so it could be partially a problem with PHP or Windows.

> nmap -sT -p 80,3306,3389 -6 ::1
 
Starting Nmap 5.00 ( http://nmap.org ) at 2009-11-20 15:48 Mountain Standard Time
Interesting ports on cfg64 (::1):
PORT     STATE  SERVICE
80/tcp   closed http
3306/tcp closed mysql
3389/tcp open   ms-term-serv
 
Nmap done: 1 IP address (1 host up) scanned in 3.14 seconds

The Fix

Open up %windir%\system32\drivers\etc\hosts with a text editor and comment out the line that looks like:

::1             localhost

Prefix it with a #, like so:

#::1             localhost

Save it and your PHP/MySQL connections will immediately begin working. You could also use 127.0.0.1 in your connection string instead of localhost, but I didn't want to change code in innumerable files.


© 2007-2010, Corey Gilmore | Posts RSS Feed | Comments RSS Feed | Contact

 

The views expressed on these pages are mine alone and not those of any past or present employer. All information presented on this site was obtained lawfully and not through disclosure under the terms of an NDA.