StayCalm
StayCalm

Reputation: 145

Add information from a CSV file to a URL

I need to make multiple HTTP PUT requests.

I have an XML template for them, and a CSV file with IDs, names and IPs from my devices. (I want to change names or IPs in my system).

The devices have exactly the IDs in my CSV data.

Input.csv (id, name, ip)

123,test1, 10.56.22.1
124,test2, 10.56.2

My Perl script

use strict;
use warnings;

use XML::Twig;
use LWP::UserAgent;
use HTTP::Headers;
use HTTP::Request;
use File::Slurp;
use JSON -support_by_pp;
use LWP 5.64;
use MIME::Base64;
use IO::Socket::SSL;
use File::Slurp;

# Create a user agent object
my $ua = LWP::UserAgent->new( ssl_opts => {
    SSL_verify_mode => SSL_VERIFY_NONE(),
    verify_hostname => 0,
} );

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

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

    while ( <$input> ) {
        chomp;
        my ($id, $name, $ip) = split /,/;

        $xml->root->set_att('name', $name);
        $xml->get_xpath('//ipaddress', 0)->set_text($ip);

        my $uri = 'https://hostname:9060/ers/config/networkdevice/($id)';
        my $req = HTTP::Request->new('PUT', $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
    );

    $req->content($xml->sprint);
    $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";
    }
}

I get this error:

404 Not Found

But I know that these IDs are in my system. How should I add my IDs to the URL for doing multiple PUT requests?

Upvotes: 0

Views: 63

Answers (1)

Borodin
Borodin

Reputation: 126722

The most obvious problem is that $uri won't be expanded in this statement

my $uri = 'https://hostname:9060/ers/config/networkdevice/($id)'

There are no embedded double-quotes in the string, so there's no problem with using them as delimiters, which will interpolate the scalar

my $uri = "https://hostname:9060/ers/config/networkdevice/($id)"

I cannot guarantee that this will solve the immediate problem as I have no way of testing it, but it seems likely

Upvotes: 1

Related Questions