perl 使用更新的xml模板的多个HTTP POST请求

n8ghc7c1  于 2023-03-23  发布在  Perl
关注(0)|答案(1)|浏览(130)

我想做多个HTTP POST请求。我的template.xml为POST请求上传从Input.scv数据,其中包含有关设备的信息,那些应该被添加(名称,ipaddresses).

Input.csv(Name,IP Address)

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

xriantvc

xriantvc1#

您需要向请求传递一个字符串而不是对象,因此必须将$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);

相关问题