CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Simple modification of XML file

 



cls78628
New User

Oct 29, 2015, 3:35 PM

Post #1 of 4 (1179 views)
Simple modification of XML file Can't Post

Total n00b, not a developer in any way... am a network security guy. I need to insert a tag and value for an element if it does not already exist.
Here's a snippet of my XML file, after that is what I'm logically trying to accomplish:
<NessusClientData_V2>
<Policy>
...
<Report>
...
<ReportHost name="1.2.3.4">
...
<HostProperties>
...
<tag name="HOST_END">Thu Sep 17 17:23:19 2015</tag>
<tag name="system-type">general-purpose</tag>
<tag name="operating-system">MS WIN</tag>
<tag name="ssh-auth-meth">password</tag>
<tag name="ssh-login-used">backup</tag>
<tag name="local-checks-proto">ssh</tag>
<tag name="host-ip">1.2.3.4</tag>
<tag name="HOST_START">Thu Sep 17 17:00:14 2015</tag>

</HostProperties>
...

1) If there is no entry for '<tag name="mac-address">' then create entry.
2) Continue to next host and do same until EOF.
My output needs to contain all of the original information, but only needs to be added to.
Freaking out here, any help would be GREATLY appreciated!!!


Laurent_R
Veteran / Moderator

Oct 29, 2015, 4:33 PM

Post #2 of 4 (1175 views)
Re: [cls78628] Simple modification of XML file [In reply to] Can't Post

Hm, you probably want the MAC address as an attribute, don't you? How do you figure it out (I mean, is it somewhere else to be picked up in the data?

Otherwise, using an XML module is probably the best solution, but it could be done with regex parsing of the data.


cls78628
New User

Oct 29, 2015, 6:00 PM

Post #3 of 4 (1167 views)
Re: [Laurent_R] Simple modification of XML file [In reply to] Can't Post

I've tried using xml::twig and xml::xsh2 but just can't figure it out. I'm trying java now. The stupid MAC doesn't matter, it just has to exist in order to upload into client system (or it rejects it).


Laurent_R
Veteran / Moderator

Oct 30, 2015, 4:31 AM

Post #4 of 4 (1148 views)
Re: [cls78628] Simple modification of XML file [In reply to] Can't Post

A five-minute test using regexes with the following data:


Code
<NessusClientData_V2> 

<Policy>
...
<Report>
...

<ReportHost name="1.2.3.4">
...

<HostProperties>
...

<tag name="HOST_END">Thu Sep 17 17:23:19 2015</tag>
<tag name="system-type">general-purpose</tag>
<tag name="operating-system">MS WIN</tag>
<tag name="ssh-auth-meth">password</tag>
<tag name="ssh-login-used">backup</tag>
<tag name="local-checks-proto">ssh</tag>
<tag name="host-ip">1.2.3.4</tag>
<tag name="HOST_START">Thu Sep 17 17:00:14 2015</tag>


</HostProperties>


<HostProperties>
<tag name="HOST_END">Thu Sep 17 17:23:19 2016</tag>
<tag name="system-type">general-purpose</tag>
<tag name="operating-system">MS WIN</tag>
<tag name="ssh-auth-meth">password</tag>
<tag name="ssh-login-used">backup</tag>
<tag name="local-checks-proto">ssh</tag>
<tag name="host-ip">1.2.3.4</tag>
<tag name="HOST_START">Thu Sep 17 17:00:14 2016</tag>
</HostProperties>


This is the code:

Code
use strict; 
use warnings;
my $xml = "text.xml";

open my $IN, "<", $xml or die "unable to open $xml $!";
my $mac; # flag: is there a mac line already for this host?
while (<$IN>) {
next unless /\w/;
if (/<HostProperties>/) {
$mac = 0;
}
$mac = 1 if /<tag name="mac-address"/;
if (/<\/HostProperties>/) {
print ' <tag name="mac-address"></tag>', "\n" unless $mac;
}
print;
}


This is the output I get:

Code
>$ perl xml_mac.pl 
<NessusClientData_V2>
<Policy>
<Report>
<ReportHost name="1.2.3.4">
<HostProperties>
<tag name="HOST_END">Thu Sep 17 17:23:19 2015</tag>
<tag name="system-type">general-purpose</tag>
<tag name="operating-system">MS WIN</tag>
<tag name="ssh-auth-meth">password</tag>
<tag name="ssh-login-used">backup</tag>
<tag name="local-checks-proto">ssh</tag>
<tag name="host-ip">1.2.3.4</tag>
<tag name="HOST_START">Thu Sep 17 17:00:14 2015</tag>
<tag name="mac-address"></tag>
</HostProperties>
<HostProperties>
<tag name="HOST_END">Thu Sep 17 17:23:19 2016</tag>
<tag name="system-type">general-purpose</tag>
<tag name="operating-system">MS WIN</tag>
<tag name="ssh-auth-meth">password</tag>
<tag name="ssh-login-used">backup</tag>
<tag name="local-checks-proto">ssh</tag>
<tag name="host-ip">1.2.3.4</tag>
<tag name="HOST_START">Thu Sep 17 17:00:14 2016</tag>
<tag name="mac-address"></tag>
</HostProperties>

So, this works in this case, the mac tag has been added for both hosts, but this is not very robust if the input file is more messy that what I have used.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives