Welcome, guest | Sign In | My Account | Store | Cart

Many users get a dynamic ip address when they sign on to the internet (e.g., cable modem). If you run a server on your home network, you probably know that you can't point a webpage address to that server with a dynamic ip b/c DNS servers need a static ip. However, if you get a home page with your ISP (e.g., users.someisp.com/myusername), this script will allow you to automatically update your home page with a web page that will redirect your users to your dynamic ip.

Run the code whenever you get a new ip address, and your users will always get to see your website. Best part of all - YOU CONTROL YOUR SERVER! You want to add PHP and MySQL support on your Linux server, go right ahead. The sky's the limit :)

Python, 66 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# By: Kevin T. Ryan (kevryan0701_AT_yahoo_DOT_com)
# 11/17/2004
#
# Inspired by a script created by  Daniel Santamaria at Tinyminds.org
# See the article "Running a Webserver with a Dynamic IP" at
# Tinyminds for the complete details.  Thanks Dan!
#
# This script simply creates a webpage that will re-direct users that type in
# your webpage name to your home server.  This would not be so interesting
# except for the fact it allows you to give people a static name (such as
# www.mywebsite.com) and then have them redirected to your server that could be
# using a DYNAMIC IP address!
#
# That is, even if you don't have a static ip address you can simply run this
# script whenever you boot your machine to automatically update the page at
# your website to point to your new ip address.
#
# One note - use at your own risk.  Not all ISP's might want you to be running a
# server from your cable or dsl modem, so check w/ them first! :)

import ftplib, socket

FTP_SERVER = "your.ftp.server"
USER_NAME = "your_username"
PASSWORD = "your_password" # Alternatively, you could request a pw every time you run this script for more security.

STANDARD_FILE_NAME = "index.html" # Can change to what you want - it will be the local and remote filename.

HTML_PAGE = """<head>
<title>Redirecting you now...</title>
<meta http-equiv='refresh' content='2;url=http://%(addr)s'>
</head>
<br />
<br />
<a href="http://%(addr)s">
<b>Click here if you are not automatically redirected to the webpage</b></a>"""

def create_redirection_page(ip=None):
    '''Creates and returns a string representation of an html page using the HTML_PAGE variable.'''
    if (ip):
        return HTML_PAGE % {'addr': ip}
    else:
        ip = get_my_ip()
        return HTML_PAGE % {'addr': ip}

def send_ftp(file_name=None):
    '''Sends the file_name (which should be a closed file object) to the server for storage.'''
    if file_name == None: file_name = STANDARD_FILE_NAME
    f = open(file_name, 'rb')
    server = ftplib.FTP(FTP_SERVER, USER_NAME, PASSWORD)
    server.storbinary("STOR " + STANDARD_FILE_NAME, f)
    f.close()
    server.quit()

def get_my_ip():
    return socket.gethostbyname(socket.gethostname())

if __name__ == "__main__":
    # First, we create the web page string.
    dynamic_redirection = create_redirection_page()
    # Then, we write it to a file.
    f = open(STANDARD_FILE_NAME, 'wb')
    f.write(dynamic_redirection)
    f.close()
    # Finally, we send it to our FTP server.
    send_ftp(STANDARD_FILE_NAME)

2 comments

Brian Hammond 19 years, 5 months ago  # | flag

Behind a router... If you use a machine behind a router connected to a cable/dsl modem, you most likely will get a LAN-internal IP address for the host (e.g. 192.168.1.2) using the above script. You want instead the WAN address of your LAN. This can be obtained by a number of means. My script (sorry it's in Perl b/c I had been doing similar work for my job as of late) will do this:

http://brianhammond.com/realip.pl

That script doesn't redirect you to the host with the dynamic ip. Instead, it updates your local hosts file so you can connect to the host with the dynamic IP (for any service [ssh, www, etc]) using a name. Run this script periodically using cron and you can always refer to the host by name. I find it very handy.

Cheers!

Fritz Cizmarov 19 years, 5 months ago  # | flag

get IP from the Internet. the following code gets the IP from behind a firewall, cablemodem, ...

import re
import httplib

_ip_regex = '([\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3})'
def get_IP():
    conn = httplib.HTTPConnection("checkip.dyndns.org")
    conn.request("GET", "")
    res = conn.getresponse()
    if res.reason == "OK":
        ip = re.split(ip_regex, res.read())[1]
    else:
        ip = None
    conn.close()
    return ip

Dookie