I am on a campus network that assigns IP addresses via DHCP. I occasionally need to access files on my computer from a classroom or the library, or where ever. I turned on FTP, ssh, and apache, because with our firewall and dynamic IP and OS X itself, there isn't much of a risk.
Anyway, I couldn't always connect to my computer unless I checked the IP address before heading out and hoping it didn't change while I was gone. So I wrote a few scripts to automatically upload my IP address to the campus webserver and a redirect script to take me to my computer.
Read the rest of the hint for the how-to...
First, in a text editor, I modified a copy of my ~/.netrc file so that I could define a macro for ftp that would upload a given file when ftp was connected:
machine 111.111.111.111
login myUserID
password somePassword
macdef init
cd public_html
put ip.txt
bye
I named this copy of the file .netrc2 and it goes in my home directory. It specifies a machine and an automatic login for that machine. Then it defines a macro to upload ip.txt to public_html and then disconnects. Then I wrote a shell script and called it ftpmyip.sh and placed this file in ~/bin:
echo -n `ipconfig getifaddr en0` > ip.txt
cp /Users/myUserID/.netrc /Users/myUserID/.netrc.bak
cp /Users/myUserID/.netrc2 /Users/myUserID/.netrc
ftp 111.111.111.111
mv /Users/myUserID/.netrc.bak /Users/myUserID/.netrc
This script sends the result of ipconfig getifaddr en0 to a file called ip.txt. I needed to use the echo command to remove the endline character because I include the file as-is later on. It swaps my two .netrc files, so that the one with the macro definition is used and then swaps them back when it has disconnected. Then, I wrote a .shtml file to be placed on the campus webserver that would include a redirect javascript to my computer.
<html>
<head>
<title> Redirect </title>
</head>
<body>
<script language = "javascript">
<!-- the next line needs to be all on one line -->
document.location.href = "http://<!--#include virtual = 'ip.txt' -->/~myUserID"; </script>
</body>
</html>
I named this file redir.shtml. It has a server-side include, so you need to make sure that your webserver knows how to handle those. It also needs to be in the same directory as ip.txt.
do shell script "/Users/myUserID/bin/ftpmyip.sh"
You need the complete path to the script in AppleScript. Lastly, I set the AppleScript to run at login in the Login Items panel of the System Preferences.
Mac OS X Hints
http://hints.macworld.com/article.php?story=20031023172615302