I have a script that logs in and returns a session ID. I need the session ID so that I can populate an XML file to do stuff with it, for example create a plant room for a nursery.
#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
use Data::Dumper;
use XML::Simple;
my $authenticate = do {
open my $fh, '<', '/tmp/login.xml' or die "Could not open file: $!";
local $/;
<$fh>;
};
my $webpage = "https://www.example.com/api";
my $ua = LWP::UserAgent->new;
my $response = $ua->post($webpage, Content_Type => 'text/xml',
Content => $authenticate);
if ( $response->is_success) {
my $xml = new XML::Simple;
my $x = $response->decoded_content;
# read XML file
my $data = $xml->XMLin($x);
my $sessionid = $data->{'sessionid'};
...
I need to take that sessionid variable and insert into another xml file that looks like this:
<xml>
<API>4.0</API>
<action>plant_room_add</action>
<enforce_rules_training>0</enforce_rules_training>
<id>3</id>
<location>123456</location>
<name>Test1</name>
<sessionid>$sessionid</sessionid>
<signature>Hello World</signature>
<terminal_id>xxxxxxx</terminal_id>
<training>1</training>
</xml>
I know that I can put the plant_root_add
XML in the same script, but I think this script is growing to grow over time so I might need a script to delete the plant room. This is for monitoring and it will be executed every 60 seconds.
What is the way to do this?