Hallo People. I have one question about HTTP PUT Request with separate values in csv. In this code example I compare hashes, but for my PUT and POST request I need separate values.
For example, I have two output files from different Systems:
output1.csv (name, ip) - Primary system
TEST_1,10.56.7.80
TEST_3,10.66.78.10
TEST_4,10.66.81.9
TEST_2,10.67.9.12
output2.csv (id,name,ip) - Secondary System
01,TEST_1,10.56.7.80
02,TEST_3,10.66.251.9
03,TEST_5,10.66.81.9
My result should be:
I do nothing with Test1 (because it is already in System 2)
I should update Test3 (because now I have a different ip address)
I should update Test5-->Test4 (because now I have a different names with the same ip adress)
I should add Test2, because I do not have it in the secondary System
use strict;
use warnings;
use feature qw(say);
use autodie;
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;
my ($file_1, $file_2) = ('output1.csv', 'output2.csv');
open my $fh, '<', $file_1 or die "Can't open $file_1: $!";
my %first = map { chomp; split /\s*,\s*/ } <$fh>;
open $fh, '<', $file_2 or die "Can't open $file_2: $!";
my %second = map { chomp; (split /\s*,\s*/)[1,2] } <$fh>;
#Create a user agent object
my $ua = LWP::UserAgent->new(ssl_opts=> {
SSL_verify_mode => SSL_VERIFY_NONE(),
verify_hostname => 0,
});
foreach my $name (sort keys %first) {
if (not exists $second{$name}) {
say "Devices should be added: $name";
next;
}
if ($first{$name} eq $second{$name}) {
say "Match found $name, $first{$name}";
} else {
say "UPDATE need be done for $second{$name}";
Till this place my code works. I compare my hushes and I see messenges in shell.
Now I want to make PUT (Update) request for these devices, those are nor identic
Idee ist to open separate both of files and to add to Secondary the ipaddress or names from Primary System
To make it this way I Need to take ip(or name) from the device from output1.csv and to make PUT request to secondary System (Update)
open ( my $input_1, '<', 'output1.csv' ) or die $!;
while ( <$input_1> ) {
chomp;
my ($name_1, $ip_1) = split /,/;
(my $id, $first{$name}, $second{$name}) = split /,/;
my $xml = XML::Twig -> new -> parsefile ( 'example.xml' );
$xml ->set_pretty_print('indented_a');
open ( my $input_2, '<', 'output2.csv' ) or die $!;
while ( <$input_2> ) {
chomp;
$xml -> root -> set_att('name', $name_1);
$xml -> get_xpath('//ipaddress',0) -> set_text($ip_1);
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.networkdevic
+e.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
#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";
}
}
}
}
}
example.xml - it ist the structure of XML data for update(PUT Request)
If I want to change Attribute, I add it in this file and make HTTP Request(POST, PUT)
Wenn I start my code, I get this message:
UPDATE need be done for 10.66.251.9
404 Not Found
404 Not Found
404 Not Found
404 Not Found
404 Not Found
404 Not Found
Match found TEST_1,10.56.7.80
But actually I Need this one :
UPDATE need be done for 10.66.251.9
UPDATE need be done for 10.66.81.9
200 ok
200 ok
Match found TEST_1,10.56.7.80
Devices should be added: Test2
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.