Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

How to auto-redirect to a dynamic IP address Internet
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.

Then from here, I simply wanted to shell script to run each time I log in. I could have created a conditional statement so that if the contents of ip.txt do not match the current IP then upload the new file, but bandwidth is not an issue here, and the script takes about one second to run. Furthermore, I'm a little bit lazy when it comes to making everything as efficient as possible. So what I did was called the shell script from an AppleScript, because it's easy to call AppleScripts at login:

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.

Now, I can call up www.ourserver.edu/redir.shtml, and I'm taken directly to my computer. Because of the firewall and IP issues, this is only good on campus. From elsewhere, I'm not able to access my computer.
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[13,843 views]  

How to auto-redirect to a dynamic IP address | 7 comments | Create New Account
Click here to return to the 'How to auto-redirect to a dynamic IP address' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
How to auto-redirect to a dynamic IP address
Authored by: diamondsw on Oct 29, '03 12:34:47PM

Or, you simply set up dynamic dns (http://www.dyndns.org/) and forget about it.

I currently have this set up and can access my computer via SSH from anywhere, then use SFTP (built-in) for file transfer, and VNC (tunneled through SSH) for remote administration. All completely secure and completely free.



[ Reply to This | # ]
How to auto-redirect to a dynamic IP address
Authored by: amacaulay on Oct 29, '03 08:59:15PM

That's fine if all the IPs are real. If the DHCP assignment is a private block, then dyndns is no use to this hinter as they're connecting within the same network as their machine.



[ Reply to This | # ]
You're working too hard...
Authored by: blatherskyte on Oct 31, '03 04:33:15PM
Why not set up your server how you like and use something like Ipanema to have it deliver your IP via mail.app (in my case I use it to send a text message to my cell phone). It can send a message once a day or when the IP changes.
That app can be found on versiontracker.com -j.

[ Reply to This | # ]
How to auto-redirect to a dynamic IP address
Authored by: craigsheppard on Oct 29, '03 01:34:51PM

Nice! I'm doing something similar, except using cron to run the job every 10 minutes, since my machine is logged-in all of the time. Basically, I have a main webserver, but I'm hosting media and a phpBB on my server at home. The home server updates the main server as to it's location, so the link to the BB and media is only ever 10 minutes out if my IP changes.

I'd much rather do it this way, then have to rely on dyndns. It's much more flexible.



[ Reply to This | # ]
How to auto-redirect to a dynamic IP address
Authored by: Cerberus on Oct 31, '03 04:12:57PM

I have not had any cause NOT to trust dyndns.org but then the argument should be that these scripts are a compliment to a dyndns-type spftware DNS solution.

If I can use putty and get to (mysite).dyndns.org OR via a known webpage updated (at login or every x minutes) then I am covered in either case.

The statement could be made then that thse scripts then could help you to find your Maci f it is ever stolen as it is obvious (to the theif) that Dyndns is running but not that obvious that a script is running... I will be implementing something like this so I can locate my Mac Just In Case! Esp when my 17" arrives!



[ Reply to This | # ]
How to auto-redirect to a dynamic IP address
Authored by: Anonymous on Nov 01, '03 06:49:57PM

Can you share how you are doing this with a cron job? Is your procedure the same as detailed above with the exception of the Applescript on the login?
Thanks.



[ Reply to This | # ]
How to auto-redirect to a dynamic IP address
Authored by: david-bo on Nov 26, '03 07:43:59AM

I simply always keep an ssh- or kerberos-telnet connection to some gateway-server between the private net and internet open. When I need my ip I login in on this gateway server and look at 'last login from', run finger, w, who etc.

You can automate this by running a cron-job that checks for an open connection every minute and if it can't be found open a new connection. This requires ssh-keys or a valid kerberos-ticket.

---
http://www.google.com/search?as_q=%22Authored+by%3A+david-bo%22&num=10&hl=en&ie=ISO-8859-1&btnG=



[ Reply to This | # ]