HowTo/Create Your Own DynDNS service

From S23Wiki
Jump to: navigation, search

Index.py (Python-script) behind htaccess gets the IP from client via http and writes to /var/lib/noip/noip.log

Get client IP

Python


#!/usr/bin/python

import os

# Required header that tells the browser how to render the text.
print "Content-Type: text/plain\n\n"

env = os.environ

outline = "S23-dyndns service \nupdated \n%s.aus.fnordanien.com should resolve to %s within a minute" % (env["REMOTE_USER"], env["REMOTE_ADDR"])
print outline
handle = open("/var/lib/noip/noip.log", "a")
line = env["REMOTE_USER"]+" "+env["REMOTE_ADDR"]+"\n"
handle.write(line)
handle.close()

OR you can use a PHP script like this or any other cgi script:

PHP

<html>
<head>
<title>S23 No-IP Service</title>
</head>
<body>
<?php
$logfile = '/var/lib/noip/noip.log';
echo "Hello $REMOTE_USER!<br /><br /> I will bind $REMOTE_USER.aus.fnordanien.com to $REMOTE_ADDR<br /><br />";
echo "Is that your correct IP? Then you are done already.<br /><br />If not, update manually: <br />
<form action=\"index.php\" method=\"POST\"><input type=\"text\" name=\"IP\" size=\"16\" />";
echo "<input type=\"submit\" value=\"submit\" /></form><br /><br />";
if ($_POST["IP"]) {
$fp = fopen($logfile, "a");
$string = "$REMOTE_USER $IP\n";
echo "<br />Ok, writing your manual input $IP instead. Thank you. Please close the page.";
$write = fputs($fp, $string);
fclose($fp);
die();
}

$fp = fopen($logfile, "a");
$string = "$REMOTE_USER $REMOTE_ADDR\n";
$write = fputs($fp, $string);
fclose($fp);

?>
</body>
</html>

Once you have the client IP this or that way:

Update Bind

update_dns.pl (Perl) script (cron'ed) updates bind.

#!/usr/bin/perl -w
# Seti23 DNS Update script aus.fnordanien.com
# Author: seti23 d4 cr3w

#config
my $noip_www_file = "/var/lib/noip/noip.log";
my $nsupdatetmpfile = "/tmp/nsupdate.txt";
my $private_key = "/etc/bind/fnord.private";

# Programm start
# setup a pid file...
my $pid_file = "/var/run/update_dns.pid";

# Make sure we're not already running...
if (-f $pid_file) {
  open (PID, "<$pid_file") or die("$0: Cannot open \"$pid_file\" :$!~\n");
  while (<PID>){
    chop;
    ($session_id) = split (/\n/);
    print "update_dns.pl appears to be already running --> PID $session_id\n";
    print "If you're sure update_dns.pl is not running, whack (rm) $pid_file and try again\n";
  }
  close PID;
  exit;
}

# Write a pid file if we're not already running...
else {
  open (PID, ">$pid_file") or die("$0: Cannot open \"$pid_file\" :$!~\n");
  $session_id = $$;
  select PID;
  print "$session_id\n";
  select STDOUT;
  close PID;
}


if (!open (NOIPLOG, "<$noip_www_file")) { unlink($pid_file); exit(0); }

open (NSUPDATETMP, ">$nsupdatetmpfile") or die("nsupdatetmpfile \"$nsupdatetmpfile\" could not be opened");
select NSUPDATETMP;
print "server s23.org\nzone aus.fnordanien.com\n";
while(<NOIPLOG>) {
  chop;
  ($user, $ip) = split(/\s+/);
  print "update delete $user.aus.fnordanien.com. A\n";
  print "update add $user.aus.fnordanien.com. 1 A $ip\n";
}
print "send\n";
close NOIPLOG;
close NSUPDATETMP;
select STDOUT;

system("/usr/bin/nsupdate -k $private_key < $nsupdatetmpfile");
unlink($nsupdatetmpfile);
unlink($noip_www_file);
unlink($pid_file);

This page is still under construction.