用于在 CPanel 中编辑 DNS 记录的 PHP 脚本


PHP Script to Edit DNS Records in CPanel

我想自给自足,因此没有DNSDynamic和DYNDNS等服务。而且我不喜欢为我可以自己做的服务付费。

所以这是场景 - 我的主要网站托管在托管公司。我还有一个家庭服务器,上面有我的音乐等。但问题是我的ISP(BT)没有向消费者提供静态IP地址。

我想让我的主域(指向我的主主机)的子域指向我的家庭 IP。这是通过一个简单的A记录完成的 - 我自己做了。

这归结为我想制作一个PHP脚本(由我的家庭服务器上的cron作业运行)将cPanel中的A记录更新为我家庭服务器的当前IP。

这里有一些示例代码 - 当然缺少的是与cPanel通信的API代码,我非常感谢任何可以为我填补空白的人。

<?php
    $current_ip = file_get_contents("http://mydomain.com/getip.php");
    $username = "CPANEL_USERNAME";
    $password = "CPANEL_PASSWORD";
    $domain = "CPANEL_DOMAIN";
    $request = file_get_contents("http://someapipage?username=".$username."&pw=".$password."&domain=".$domain."&ip=".$current_ip);
?>

http://mydomain.com/getip.php中的代码类似于

<?php echo $_SERVER["REMOTE_ADDR"]; ?>

我已经掌握了如何设置 cron 作业,因为我的家庭服务器正在运行 Ubuntu,并且我已经阅读了使用 wget 在我的本地主机目录中调用我的cron.php的教程。

我已经尝试过这个链接,但我无法理解他在做什么。提前谢谢。

我刚刚根据他们的文档和该文档的 jordih.net 链接为cPanel的JSON-API编写了这个库。它没有很好的文档记录,但它的要点是:

通过调用创建zone_records对象

$zones = new zone_records("cpaneluser", "pass", "website_to_login", "domain_of_records")

请注意,如果要从要更改记录的服务器运行此站点,则要登录的网站通常是 127.0.0.1。

调用后,您可以访问成员$zones->DNSrecords。它包含 DNS A 记录和 CNAME 记录(两者都是 DNSrecord 对象)的数组。其他的(TXT 除外)是无关紧要的,因为如果不对类进行额外的添加(函数),就无法编辑它们。

每个DNSrecord都有一些成员(例如目标,ttl,名称,类型),虽然private,但可以通过$record->ttl访问,因为我添加了"魔术"__get方法。实现"magic"__set方法仅用于更改 ttl 和目标(不能使用此 API 函数更改其他属性,如果尝试这样做,对象将引发异常)。

您可以使用

$zones->addrecord($type, $target, $name, $ttl)

添加记录,或

$zones->deleterecord($line)

要删除区域文件中第 $line 行的记录 - 您可以通过 $record->line 找到它。

如果你想在 ZoneEdit 模块中进行一些自己的查询,你可以调用

$zones->doquery("function_from_API", array("parameters=>"here"), array("headers"=>"here"))

它将返回 cPanel 响应(addrecord 和 deleterecord 方法也是如此)。最后,我建议您使用try {...} catch (Exception $e) {...}因为我的对象在出现问题时会引发异常(当然,您可以编辑它们)。

这段代码在公共领域 - 你可以在 https://github.com/ShadowLNC/cpanel_dns(classdns.php是主文件,dns_update.php显示了一个示例)获取它。

编辑:下面的链接现已断开。以下是供参考的整个脚本:

#!/usr/bin/perl
# -------------------------------------------------------------------------------
# neobitti_update_ip.pl
#
# Version 1.0 - 16.01.2012
#
# PERL script to dynamically update the IP of a host via the cPanel-API. This
# script was written to work with the Finnish hoster Neobitti but it might work
# with other hosters which use cPanel too.
#
# Copyright (C) 2012 Stefan Gofferje - http://stefan.gofferje.net/
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
# -------------------------------------------------------------------------------
use strict;
use LWP::UserAgent;
use MIME::Base64;
use XML::Simple;
use Data::Dumper;
# --- Command line parameters ------------------------------------------------
my $param_domain=$ARGV[0];
my $param_host=$ARGV[1];
my $param_ip=$ARGV[2];
# --- cPanel information -----------------------------------------------------
# Storing passwords in clear text is ugly!
my $cpanel_domain = "example.com";
my $user = "username";
my $pass = "password";
my $auth = "Basic " . MIME::Base64::encode( $user . ":" . $pass );
# --- Deactivate SSL certificate validation ----------------------------------
# This is ugly but neccessary because Neobitti uses self-signed SSL
# certificates which will fail validation
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
# --- Find out the linenumber for the A-record we want to change -------------
sub getlinenumber_a {
  my $domain=$_[0];
  my $hostname=$_[1].".";
  my $xml = new XML::Simple;
  my $request = HTTP::Request->new( GET => "https://$cpanel_domain:2083/xml-api/cpanel?cpanel_xmlapi_module=ZoneEdit&cpanel_xmlapi_func=fetchzone&domain=$domain" );
  $request->header( Authorization => $auth );
  my $response = $ua->request($request);
  my $zone = $xml->XMLin($response->content);
  my $linenumber="";
  print $response->content . "'n";
  print $zone . "'n";
  print $zone->{'data'}->{'status'} . "'n";
  print $zone->{'data'}->{'record'} . "'n";
  if ($zone->{'data'}->{'status'} eq "1") {
    my $count = @{$zone->{'data'}->{'record'}};
    my $oldip="";
    for (my $item=0;$item<=$count;$item++) {
        my $name=$zone->{'data'}->{'record'}[$item]->{'name'};
        my $type=$zone->{'data'}->{'record'}[$item]->{'type'};
        print $name;
        if ( ($name eq $hostname) && ($type eq "A") ) {
          $linenumber=$zone->{'data'}->{'record'}[$item]->{'Line'};
          $oldip=$zone->{'data'}->{'record'}[$item]->{'record'};
          print "Found $hostname in line $linenumber with IP $oldip.'n"; # DEBUG
        }
    }
  } else {
    $linenumber="0";
    print $zone->{'event'}->{'data'}->{'statusmsg;'}
  }
  return($linenumber);
}
# --- Change the IP address record for a certain linenumber ------------------
sub setip {
  my $domain=$_[0];
  my $linenumber=$_[1];
  my $newip=$_[2];
  my $result="";
  my $xml = new XML::Simple;
  my $request = HTTP::Request->new( GET => "https://$cpanel_domain:2083/xml-api/cpanel?cpanel_xmlapi_module=ZoneEdit&cpanel_xmlapi_func=edit_zone_record&domain=$domain&line=$linenumber&address=$newip" );
  $request->header( Authorization => $auth );
  my $response = $ua->request($request);
  my $reply = $xml->XMLin($response->content);
  if ($reply->{'data'}->{'status'} eq "1") {
    $result="1";
  } else {
    $result=$reply->{'data'}->{'statusmsg'};
  }
  return($result);
}
# --- Main procedure ---------------------------------------------------------
print "Trying to find the linenumber for $param_host in $param_domain...'n";
my $line=getlinenumber_a($param_domain,$param_host);
if ( ($line ne "0") && ($line ne "") ) {
  print "Trying to update IP...'n";
  my $result=setip ($param_domain,$line,$param_ip);
  if ($result eq "1") {
    print "Update successful!'n";
  } else {
    print "$result'n";
  }
} else {
  print "Error - check domain and hostname!'n";
}

您需要将"用户名"和"密码"替换为您自己的凭据。

运行脚本并传入子域的域名、子域名和新 IP:

./neobitti_update_ip.pl <domain> <subdomain> <ip>

例:

./neobitti_update_ip.pl example.com subdomain.example.com 93.184.216.34

以下是我的原始答案,并附有指向来源的链接:


有一个Perl脚本正在做你在这里要求的事情:

http://stefan.gofferje.net/it-stuff/scripts/50-dynamic-dns-update-via-cpanel-api

如果它必须是PHP,我想从Perl翻译成PHP(或任何其他语言)应该相当简单。剧本作者在这里发布了一些背景信息。

linux 有一个 bash 脚本来解决最初的问题,即如何自动更新网址以指向不断变化的 IP。众所周知且运行良好的ddclient是许多提供商的标准工具,但不幸的是(尚未)为cPanel驱动的提供商工作(我使用 serverprofis.de 但有一长串其他工具)。

bash脚本可以在github上找到,我刚刚在代码中编辑了cPanel上的凭据等,然后添加了一个cron作业以每隔几分钟执行一次。与 ddclient 一样,该脚本会检查 IP 是否已更改,如果与提供程序的上次更新相比未检测到更改,则停止。如果 IP 已更改,则会更改提供程序处的记录以指向新 IP。

到目前为止,它运行良好。