Skip to content

Instantly share code, notes, and snippets.

@linuxkidd
Created April 30, 2019 19:12
Show Gist options
  • Select an option

  • Save linuxkidd/e869684f50980e9fa9faf4fc720c62a0 to your computer and use it in GitHub Desktop.

Select an option

Save linuxkidd/e869684f50980e9fa9faf4fc720c62a0 to your computer and use it in GitHub Desktop.
Dreamhost DNS Api DynDNS updater
<?php
if(isset($_GET['api_key'])) {
$APIKEY=$_GET['api_key'];
} else {
die("No api key");
}
if(isset($_GET['myhost'])) {
$MYHOST=$_GET['myhost'];
} else {
die("No host");
}
header('Contet-type: text/xml');
function wget($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
function getUserIpAddr(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
//ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
//ip pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
if(isset($_GET["myip"])) {
$client_ip=$_GET["myip"];
} else {
$client_ip=getUserIpAddr();
}
$issame=false;
$current_data=unserialize(wget("https://api.dreamhost.com/?key=$APIKEY&cmd=dns-list_records&format=php"));
if($current_data["result"]=='success') {
for($recordid=0;$record=$current_data["data"][$recordid];$recordid++) {
if($record["record"]==$MYHOST) {
print "Found ".$MYHOST."\n";
if($record["value"]==$client_ip) {
print "same";
$issame=true;
} else {
$junk=wget("https://api.dreamhost.com/?key=$APIKEY&cmd=dns-remove_record&record=$MYHOST&type=A&value=${record["value"]}");
}
}
}
if(!$issame) {
$junk=wget("https://api.dreamhost.com/?key=$APIKEY&cmd=dns-add_record&record=$MYHOST&type=A&value=$client_ip");
print "Set ".$MYHOST." to: ".$client_ip;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment