【发布时间】:2017-04-24 04:23:46
【问题描述】:
我有一个登录并返回会话 ID 的脚本。我需要会话 ID,以便我可以填充一个 XML 文件来处理它,例如为托儿所创建一个机房。
#!/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'};
...
我需要把那个 sessionid 变量插入到另一个看起来像这样的 xml 文件中:
<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>
我知道我可以将plant_root_add XML 放在同一个脚本中,但我认为这个脚本会随着时间的推移而增长,所以我可能需要一个脚本来删除机房。这是为了监控,每 60 秒执行一次。
这样做的方法是什么?
【问题讨论】: