Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

    In reply to HTTP PUT Request with separate values in csv by StayCalm

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post; it's "PerlMonks-approved HTML":



    • Are you posting in the right place? Check out Where do I post X? to know for sure.
    • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
      <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
    • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
    • Want more info? How to link or How to display code and escape characters are good places to start.
    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (5)
    As of 2024-03-29 06:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found