Skip to content

Instantly share code, notes, and snippets.

@stypr stypr/ping.php
Created Feb 6, 2015

Embed
What would you like to do?
cloudflare auto ddns + memory flush
#!/usr/bin/php
<?php
/*
* crontab -e
* 0 5 * * * ~/ping.php
*/
error_reporting(0);
set_time_limit(0);
$domain = "stypr.com";
$subdomain = "home";
$zone_id = "you need to get this from cloudflare";
$email = "git@stypr.com";
$token = "you need to get this from cloudflare";
header("Content-Type: text/html; charset=utf-8");
@system("/sbin/sysctl vm.drop_caches=3");
function getfirstIP(){
$fp = @fsockopen("ip4.me", "80", $errno, $errstr, 3);
if(!$fp) die("Killed");
$packet = "GET / HTTP/1.0\r\n";
$packet .= "Host: ip4.me\r\n";
$packet .= "User-Agent: Mozilla/5.0\r\n\r\n";
@fwrite($fp, $packet);
while($buffer = fgets($fp, 2048)){
$test .= fgets($fp, 2048);
}
fclose($fp);
$test = explode("+3>",$test);
$test = explode("</font", $test[1]);
return $test[0];
}
function getsecondIP(){
$fp = @fsockopen("ipip.kr", "80", $errno, $errstr, 3);
if(!$fp) die("Killed");
$packet = "GET / HTTP/1.0\r\n";
$packet .= "Host: ipip.kr\r\n";
$packet .= "User-Agent: Mozilla/5.0\r\n\r\n";
@fwrite($fp, $packet);
while($buffer = fgets($fp, 2048)){
$test .= fgets($fp, 2048);
}
fclose($fp);
$test = explode("Your IP is ",$test);
$test = explode("</title>", $test[1]);
return $test[0];
}
function getreceiver(){
$fp = @fsockopen("stypr.com", "80", $errno, $errstr, 3);
if(!$fp) die("Killed");
$packet = "GET /receiver.php?mode=receiver HTTP/1.0\r\n";
$packet .= "Host: stypr.com\r\n";
$packet .= "User-Agent: Mozilla/5.0\r\n\r\n";
@fwrite($fp, $packet);
$test = fgets($fp, 128);
fclose($fp);
die("Success");
}
$first = getfirstIP();
$second= getsecondIP();
if($first == $second){
@system("curl -s \"https://www.cloudflare.com/api_json.html?a=rec_edit&z=".$domain."&id=".$zone_id."&content=".$first."&ttl=1&name=".$subdomain."&type=A&email=".$email."&tkn=".$token."\"");
}else{
getreceiver();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.