perl不匹配的标记,问题=XML::Simple模块?
当我尝试运行此脚本将我的ip发送到cpanel时perl不匹配的标记,问题=XML::Simple模块?,perl,raspberry-pi,cpanel,raspberry-pi3,cpanel-xmlapi,Perl,Raspberry Pi,Cpanel,Raspberry Pi3,Cpanel Xmlapi,当我尝试运行此脚本将我的ip发送到cpanel时 #!/usr/bin/perl # ------------------------------------------------------------------------------- # dns_update_script.pl # # Version 1.0 - 16.01.2012 # # PERL script to dynamically update the IP of a host via the cPanel-API.
#!/usr/bin/perl
# -------------------------------------------------------------------------------
# dns_update_script.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 = "yourdomain.com";
my $user = "yourcpaneluser";
my $pass = "yourcpanelpassword";
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=fetchzon
e&domain=$domain" );
$request->header( Authorization => $auth );
my $response = $ua->request($request);
my $zone = $xml->XMLin($response->content);
my $linenumber="";
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'};
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";
}
在cPanel信息部分,我在我的文件(不是这个文件)中填写了我的cPanel用户名、密码和域。我像一个书呆子一样在公开文本中填写了这篇文章,却忽略了这件事:
my $auth = "Basic " . MIME::Base64::encode( $user . ":" . $pass );
但我认为这不是问题所在
我认为问题出在XML::Simple模块中,因为在调用该模块时脚本会停止,这在我看来似乎是idk。请提供帮助。由于传递给
XMLin
的字符串不是有效的XML,因此您将收到该错误。您需要将输入修复为有效的XML
然而,这并不是因为解析XML时出现问题。在这种情况下使用的底层解析器(expat
via XML::parser)不会错误地声明有效的XML无效
您的程序应该检查请求是否成功($response->is_success
)。我怀疑您遇到了某种错误(例如403禁止)<代码>$response->status\u line可用于添加到错误消息中
(如果请求成功,也许您应该将
$response->as_string
打印出来,以检查响应,更好地了解发生了什么。)我只是使用了这个shell脚本,并将它连接到我的crontab上,现在它每小时运行一次脚本,效果很好
对于我的raspberry pi web服务器来说,他们甚至说不要使用它,而是支持许多更好的选择。。。但是,如果没有看到您提供给它的xml示例,我们还不能真的责怪它。。。可能xml格式错误,如错误所示?$result=$expat->parse($arg);您需要在问题中包含一个您试图解析的xml数据的示例。该脚本在尝试使用其结果之前似乎不会执行任何错误检查,以确保http请求成功,这也是值得研究的问题。(我不知道这是复制粘贴错误还是什么,但其中一个URL字符串中有一个新行不应该出现)
encode($user.:“$pass)
应该是encode($user.:“$pass,””)
,但这不可能是问题所在。
my $auth = "Basic " . MIME::Base64::encode( $user . ":" . $pass );