簡體   English   中英

perl 標簽不匹配,問題 = XML::Simple 模塊?

[英]perl mismatched tag, problem = XML::Simple module?

當我嘗試運行此腳本將我的 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. 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";
}

...我收到此錯誤:

mismatched tag at line 37, column 2, byte 4634 at /usr/lib/arm-linux-gnueabihf/perl5/5.28/XML/Parser.pm line 187.
XML::Simple called at dns_update_script.pl line 55.

在 cPanel 信息部分,我在我的文件中填寫了我的 cPanel 用戶名、密碼和域(不是這個)。 我像菜鳥一樣在開放文本中填寫了這個並忽略了這個東西:

  my $auth = "Basic " . MIME::Base64::encode( $user . ":" . $pass );

但我不認為這是問題所在。

我認為問題出在 XML::Simple 模塊中,因為在調用該模塊時腳本會停止,但在我看來似乎是 idk。 請幫忙。

您收到該錯誤是因為您傳遞給XMLin的字符串不是有效的 XML。 您需要將輸入修正為有效的 XML。

雖然我強烈建議不要使用 XML::Simple ,但這並不是因為解析 XML 的問題。 在這種情況下使用的底層解析器( expat via XML::Parser)並不因錯誤地聲稱有效的 XML 無效而為人所知。

您的程序應該檢查請求是否成功( $response->is_success )。 我懷疑您遇到了某種錯誤(例如 403 Forbidden)。 $response->status_line可用於添加到您的錯誤消息中。

(如果請求成功,也許您應該打印$response->as_string來檢查響應以更好地了解正在發生的事情。)

我只是使用了這個 shell 腳本並將它連接到我的 crontab,現在它每小時運行一次腳本並且它工作正常。

https://github.com/CpanelInc/cpanel-dynamicdns-tools

用於我的樹莓派網絡服務器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM