使用更新的xml模板

时间:2018-01-31 14:36:46

标签: xml perl csv http-post

我想制作多个HTTP POST请求。我的template.xml用于从Input.scv数据上传POST请求,其中包含有关设备的信息,应添加的信息(名称,ipaddresses)。

Input.csv(名称,IP地址)

LAB-1,10.26.0.1
LAB-2,10.26.0.2
LAB-3,10.26.0.3

TEMPLATE.XML

<?xml version="1.0" encoding="UTF-8"?>
<ns0:networkdevice
        xmlns:ns0="network.ers.ise.cisco.com" 
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:ns1="ers.ise.cisco.com"
        xmlns:ers="ers.ise.cisco.com"
        name="name1">
    <authenticationSettings>
        <enableKeyWrap>true</enableKeyWrap>
        <keyEncryptionKey>1234567890123456</keyEncryptionKey>
        <keyInputFormat>ASCII</keyInputFormat>
        <networkProtocol>RADIUS</networkProtocol>
        <radiusSharedSecret>aaa</radiusSharedSecret>
    </authenticationSettings>
    <coaPort>1700</coaPort>
    <NetworkDeviceIPList>
        <NetworkDeviceIP>
            <ipaddress>1.1.1.1</ipaddress>
            <mask>32</mask>
        </NetworkDeviceIP>
    </NetworkDeviceIPList>
</ns0:networkdevice>

我现在更新了XML,这是对的。我现在需要将我的设备添加到管理系统。

   use strict;
   use warnings;
   use XML::Twig;
   use LWP::UserAgent;
   use HTTP::Headers;
   use HTTP::Request;
   use File::Slurp;
   use LWP 5.64;
   use MIME::Base64;
   use IO::Socket::SSL;

  my $xml = XML::Twig -> new -> parsefile ( 'template.xml' );
  $xml ->set_pretty_print('indented_a');

  open ( my $input, '<', 'input.csv' ) or die $!;

  while ( <$input> ) {
  chomp;
  my ( $name, $ip ) = split /,/;
  $xml -> root -> set_att('name', $name );
  $xml -> get_xpath('//ipaddress',0) -> set_text($ip);

  $xml -> sprint;

  #Create a user agent object
  my $ua = LWP::UserAgent->new(ssl_opts=> {
  SSL_verify_mode => SSL_VERIFY_NONE(),
  verify_hostname => 0,
  }
  );
  my $uri='https://hostname:9060/ers/config/networkdevice';
  my $req = HTTP::Request->new('POST', $uri, 
  [Accept=>'application/vnd.com.cisco.ise.network.networkdevice.1.1+xml',
  Content_Type=>'application/vnd.com.cisco.ise.network.networkdevice.1.1+xml; 
  charset=utf-8'], $xml);

  $req->content($xml);
  $req->authorization_basic("user", "user");

  #Pass request to the user agent and get a response back
  my $res = $ua->request($req);
  #Check the outcome of the response
  if ($res->is_success) {
    print $res->status_line, "n";
       } else {
         print $res->status_line, "n";
      }

}

脚本运行后我有这个错误(每次尝试):

500 Not a SCALAR referencen

1 个答案:

答案 0 :(得分:1)

您需要将字符串传递给请求而不是对象,因此您必须将$xml转换为字符串

my $req = HTTP::Request->new('POST', $uri, 
[Accept=>'application/vnd.com.cisco.ise.network.networkdevice.1.1+xml',
Content_Type=>'application/vnd.com.cisco.ise.network.networkdevice.1.1+xml; 
charset=utf-8'], $xml->sprint);