Created
February 5, 2012 11:40
-
-
Save AlexanderPavlenko/1744916 to your computer and use it in GitHub Desktop.
YPDNS IP updater
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php5 | |
<?php | |
class AUpdater { | |
private $cookies; | |
private $credentials = array( | |
'username' => '...', | |
'password' => '...', | |
'mysubmit' => 'Sign+In' | |
); | |
private $domain_id = array( | |
'example.com' => 1234 | |
); | |
private $A_id = array( | |
1234 => 12345 | |
); | |
public function login() { | |
$result = self::curl( | |
'https://host.ypdns.com/login_up.php', | |
$this->credentials, null, true, false, | |
'http://www.ypdns.com/' | |
); | |
$this->cookies = self::fetchCookies($result); | |
} | |
public function logout() { | |
$result = self::curl( | |
'https://host.ypdns.com/login_up.php?action=logout', | |
null, $this->cookies, true, false, | |
'http://www.ypdns.com/' | |
); | |
$this->cookies = self::fetchCookies($result); | |
} | |
public function isAuthorized() { | |
return stripos($this->cookies, 'dnsmanager') !== false; | |
} | |
public function update($domain, $ip) { | |
$record_id = $this->A_id[$this->domain_id[$domain]]; | |
$domain_id = $this->domain_id[$domain]; | |
$parts = explode('.', $domain); | |
$data = array( | |
'dns_template_record_form_cmd' => 1, | |
'dns_entry_type' => 'A', | |
'prev_record_type' => 'A', | |
'host_hidden' => '', | |
'opt' => '', | |
'last_mask' => 0, | |
'id' => $record_id, | |
'status' => 1, | |
'host' => count($parts) > 2 ? $parts[0] : '', | |
'entry_value' => $ip | |
); | |
$result = self::curl( | |
'https://host.ypdns.com/content.php?screen=zone/zone_record_edit&id=' . | |
$record_id . '&dns_record_type=A&domain_id=' . $domain_id, | |
$data, $this->cookies, true | |
); | |
} | |
public function updateAll($ip) { | |
foreach($this->domain_id as $domain=>$id) { | |
$this->update($domain, $ip); | |
} | |
} | |
protected static function fetchCookies($str) { | |
preg_match_all('#Set-Cookie:\s([\S]+?);#i', $str, $gets); | |
return join(';', $gets[1]); | |
} | |
protected static function curl($get, $post = '', $cookies = '', $getHead = false, $getBody = true, $referer = '') { | |
$cl = curl_init($get); | |
if (empty($referer)) { | |
$referer = explode($get, '?'); | |
$referer = $referer[0]; | |
} | |
curl_setopt($cl, CURLOPT_HTTPHEADER, array('Expect:')); | |
curl_setopt($cl, CURLOPT_COOKIESESSION, true); | |
curl_setopt($cl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($cl, CURLOPT_HEADER, $getHead); | |
curl_setopt($cl, CURLOPT_NOBODY, !$getBody); | |
curl_setopt($cl, CURLOPT_COOKIE, $cookies); | |
curl_setopt($cl, CURLOPT_REFERER, $referer); | |
curl_setopt($cl, CURLOPT_FOLLOWLOCATION, false); | |
curl_setopt($cl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11'); | |
curl_setopt ($cl, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt ($cl, CURLOPT_SSL_VERIFYHOST, false); | |
if ($post) { | |
curl_setopt($cl, CURLOPT_POST, true); | |
curl_setopt($cl, CURLOPT_POSTFIELDS, $post); | |
} | |
$line = curl_exec($cl); | |
if ($line === false) { | |
echo curl_error($cl); | |
} | |
curl_close($cl); | |
return $line; | |
} | |
} | |
$update_all = true; | |
if ($argc < 2) { | |
exit('Not enough arguments'); | |
} | |
$ip = array_pop($argv); | |
$updater = new AUpdater(); | |
$updater->login(); | |
if ($update_all) { | |
$updater->updateAll($ip); | |
} | |
$updater->logout(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment