Skip to content

Instantly share code, notes, and snippets.

@tezvi
Created July 2, 2017 10:47
Show Gist options
  • Save tezvi/ca368c78ee547c9ecddeaf6bfe11f2d5 to your computer and use it in GitHub Desktop.
Save tezvi/ca368c78ee547c9ecddeaf6bfe11f2d5 to your computer and use it in GitHub Desktop.
PHP script to automatically update NO-IP hostname WAN IP address
<?php
/**
* Script that returns client IP address back to the client.
*/
// this password should be used in original script that calls this one via HTTP request.
$configPassword = 'PASSWORD_TO_USE_THIS_SCRIPT';
// check credentials
if (empty($_POST['passwd']) || $_POST['passwd'] != $configPassword) {
header('Status: 403 Forbidden');
exit('Access Denied');
}
$ip = empty($_SERVER['REMOTE_ADDR']) ? 'error' : $_SERVER['REMOTE_ADDR'];
die($ip);
#!/usr/bin/env php
<?php
/*
* Simple script to update your NO-IP hostname IP address.
* Change the following configuration values.
*/
// URI to script that will return your WAN IP. Create your own or use existing one from the same gist.
$ipDetectUrl = 'http://examplehost.com/myip.php';
// Your no-ip hostname you want to update
$configIpHost = 'YOUR_NO_IP_HOSTNAME';
// enter password for your ip check script or use ENV variable here
$configIpPassword = 'YOUR_IP_SCRIPT_PASSWORD';
// enter NO-IP API username for you no-ip service or use ENV variable here
$configNoIpUsername = 'YOUR_NO_IP_USERNAME';
// enter NO-IP API password for you no-ip service or use ENV variable here
$configNoIpPassword = 'YOUR_NO_IP_PASSWORD';
// always display errors so we can log them or mail them from cronjob
ini_set('display_errors', true);
/*
* Get your WAN IP by making a request to IP check script.
*
* NOTE: You may change the following call to obtain new WAN IP address by calling some other service
* instead of making a HTTP request to other host.
*/
$ipDetectPost = http_build_query(['passwd' => $configIpPassword]);
$ipDetectPostLen = strlen($ipDetectPost);
$myIp = file_get_contents(
$ipDetectUrl,
false,
stream_context_create(
[
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
],
'http' => [
'method' => 'POST',
'header' => "Connection: close\r\n" .
"Content-Type: application/x-www-form-urlencoded\r\n" .
"Content-Length: {$ipDetectPostLen}\r\n",
'content' => $ipDetectPost
]
]
)
);
if (empty($myIp) || $myIp == 'error') {
throw new Exception("Invalid IP returned '{$myIp}'");
}
$dynUrl = sprintf(
'https://dynupdate.no-ip.com/nic/update?hostname=%s&myip=%s', urlencode($configIpHost), urlencode($myIp)
);
// finally update NO-IP hostname IP address
$response = file_get_contents(
$dynUrl,
false,
stream_context_create(
[
'http' => [
'method' => 'GET',
'header' => sprintf(
"Authorization: Basic %s\r\n",
base64_encode($configNoIpUsername . ':' . $configNoIpPassword)
)
]
]
)
);
printf("ip: %s, dyn response: %s\n", $myIp, $response);
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment