Announcement

Collapse
No announcement yet.

perl script to update router's dynamic IP address to an html redirect page.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    perl script to update router's dynamic IP address to an html redirect page.

    # Script Name: Redirect.pl
    #
    # Purpose:
    # Many cable and DSL companies don't provide a static IP address with their
    # service. Instead, customers get a dynamic IP address which changes every so
    # often. Typically, a service provider also give some amount of "free"
    # web space.
    #
    # This script is intended to obtain the current IP address of your
    # firewall/router, create an HTML redirect page, and to FTP the redirect
    # page to your "free" web space. Users who hit your redirect page, will
    # be redirected to your home web server (like homeseer). Note: You will
    # have to set up your firewall/router appropriately for this.
    #
    # Also, this script assumes it is being run from homeseer as it uses a
    # couple of $hs variables.
    #
    # This script was tested using Active State's Active Perl 5.8.
    #
    # Installation Instructions:
    # 1) Make sure perl is installed on your computer.
    # 2) Place this script in the homeseer/script folder.
    # 3) Change the necessary variables below. See "SET THESE VARIABLES
    # BEFORE USE".
    # 4) Create an event in homeseer to automatically run this script periodically.


    sub main()
    {
    require LWP::UserAgent;
    use NET::FTP;
    use OLE;

    ############################################################ #
    # SET THESE VARIABLES BEFORE USE. #
    ############################################################ #
    # Set the Homeseer Web port number. 80 is the default.
    $port = "80";

    # Set the username and password for the LOCAL web server.
    # Typically this will be guest:guest.
    $userpass = "guest:guest";

    # Set the name of the redirect file you want to be posted on your external
    # web space.
    $redirect = "redirect.htm";

    # Specify the name of the FTP/Web server that you want the redirect
    # page sent to.
    $ftpserver = "upload.comcast.net";

    # Specify the username and password of the FTP server.
    $ftpusername = "XXXXX";
    $ftppassword = "xxxxx";

    ############################################################ #
    # DONE setting variables. #
    ############################################################ #


    # Get the path name and new file name.
    $p = $hs->GetAppPath();
    $Folder = CreateObject OLE ("Scripting.FileSystemObject");
    $TextStream = $Folder->CreateTextFile("$p\\scripts\\$redirect", True);

    # Obtain the IP address of my router.
    use HTTP::Request::Common;
    $ua = LWP::UserAgent->new;
    $resp = $ua->request(GET 'http://www.showmyip.com/simple/');

    # Tweek the result so that we only get the IP address itself.
    $result = $resp->content;
    $id = index($result," ");
    $ip = substr($result,0,$id);

    $template = "<\!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\">\n" .
    "<html>\n" .
    "<head>\n" .
    "<meta http-equiv=\"content-type\"\n" .
    "content=\"text\/html; charset=ISO-8859-1\">\n" .
    "<meta http-equiv=\"Refresh\" \n" .
    "content=\"1; URL=http:\/\/$userpass\@$ip:$port\"> \n" .
    "<title>Redirect Page<\/title>\n" .
    "<\/head>\n" .
    "<body>\n" .
    "<br>\n" .
    "<\/body>\n" .
    "<\/html>";

    # Write the contents to the output file.
    $TextStream->Write($template);

    # Close up the template, and the resulting file.
    $TextStream->Close();

    # FTP the new file to the web server.
    $ftp = Net::FTP->new($ftpserver, Debug => 0);
    $ftp->login($ftpusername,$ftppassword);
    $ftp->put("$p\\scripts\\$redirect",$redirect);
    $ftp->quit;

    # Write to the log.
    $hs->writelog("Info","Updated Redirect Page.");

    }

    http://mywebpages.comcast.net/verjinski

    [This message was edited by RichV on Mon, 19 January 2004 at 08:33 PM.]
Working...
X