Filtrage fichier XML via Dom

Filtrage fichier XML via Dom - PHP - Programmation

Marsh Posté le 21-11-2011 à 02:01:21    

Bonsoir  :hello:  
 
 
 
J'essaie de filtrer le fichier XML ci-dessous afin d'afficher uniquement les adresses ip des machines up et dont les ports sont ouvert
 

Code :
  1. <?xml version="1.0" ?>
  2. <?xml-stylesheet href="file:///usr/share/nmap/nmap.xsl" type="text/xsl"?>
  3. <!-- Nmap 5.21 scan initiated Sun Nov 20 23:44:08 2011 as: /usr/bin/nmap -oX nmap_output.xml -&#45;open -sU -p 161-162 192.168.10.0/24 -->
  4. <nmaprun scanner="nmap" args="/usr/bin/nmap -oX nmap_output.xml -&#45;open -sU -p 161-162 192.168.10.0/24" start="1321829048" startstr="Sun Nov 20 23:44:08 2011" version="5.21" xmloutputversion="1.03">
  5. <scaninfo type="udp"  protocol="udp" numservices="2" services="161-162" />
  6. <verbose level="0" />
  7. <debugging level="0" />
  8. <host starttime="1321829048" endtime="1321829050"><status state="up" reason="arp-response"/>
  9. <address addr="192.168.10.1" addrtype="ipv4" />
  10. <address addr="E0:91:F5:FF:FF:FF" addrtype="mac" />
  11. <hostnames>
  12. <hostname name="VM0" type="PTR"/>
  13. </hostnames>
  14. <ports><extraports state="closed" count="1">
  15. <extrareasons reason="port-unreach" count="1"/>
  16. </extraports>
  17. <port protocol="udp" portid="161"><state state="open" reason="udp-response" reason_ttl="64"/><service name="snmp" method="table" conf="3" /></port>
  18. </ports>
  19. <times srtt="651" rttvar="3042" to="100000" />
  20. </host>
  21. <host starttime="1321829048" endtime="1321829050"><status state="up" reason="arp-response"/>
  22. <address addr="192.168.10.3" addrtype="ipv4" />
  23. <address addr="52:54:00:FF:FF:FF" addrtype="mac" vendor="QEMU Virtual NIC" />
  24. <hostnames>
  25. <hostname name="VM1" type="PTR"/>
  26. </hostnames>
  27. <ports><extraports state="closed" count="2">
  28. <extrareasons reason="port-unreaches" count="2"/>
  29. </extraports>
  30. </ports>
  31. <times srtt="1494" rttvar="3043" to="100000" />
  32. </host>
  33. <host starttime="1321829048" endtime="1321829051"><status state="up" reason="arp-response"/>
  34. <address addr="192.168.10.6" addrtype="ipv4" />
  35. <address addr="52:54:00:FF:FF:FF" addrtype="mac" vendor="QEMU Virtual NIC" />
  36. <hostnames>
  37. <hostname name="VM2" type="PTR"/>
  38. </hostnames>
  39. <ports><port protocol="udp" portid="161"><state state="open|filtered" reason="no-response" reason_ttl="0"/><service name="snmp" method="table" conf="3" /></port>
  40. <port protocol="udp" portid="162"><state state="open|filtered" reason="no-response" reason_ttl="0"/><service name="snmptrap" method="table" conf="3" /></port>
  41. </ports>
  42. <times srtt="964" rttvar="5000" to="100000" />
  43. </host>
  44. <runstats><finished time="1321829054" timestr="Sun Nov 20 23:44:14 2011" elapsed="6.04"/><hosts up="8" down="248" total="256" />
  45. <!-- Nmap done at Sun Nov 20 23:44:14 2011; 256 IP addresses (8 hosts up) scanned in 6.04 seconds -->
  46. </runstats></nmaprun>


 
J'ai réussi a filtrer le fichier pour avoir les machines up mais pour afficher uniquement les ip dont les port sont ouvert je galère  :cry:  
 

Code :
  1. <?php
  2. $domDocument = new domDocument();
  3. $domDocument->load('nmap_output.xml');
  4. $nodesStatus = $domDocument->getElementsByTagName('host');
  5. foreach($nodesStatus as $status)
  6. {
  7. $stas = $status->getElementsByTagName('status');
  8. foreach($stas as $sta)
  9. {
  10.  if ($sta->getAttribute('state') == 'up')
  11.  {
  12.   $addrs = $status->getElementsByTagName('address');
  13.    foreach ($addrs as $addr)
  14.    {
  15.     if ($addr->getAttribute('addrtype') == 'ipv4')
  16.     {
  17.     $tabIp[] = $addr->getAttribute('addr');
  18.     }
  19.    }
  20.  }
  21. }
  22. }
  23. foreach($tabIp as $element)
  24. {
  25.     echo $element . '<br />';
  26. }
  27. ?>


 
Si quelqu'un peut m'aider
 
Merci  :jap:


Message édité par bobl'eponge le 21-11-2011 à 02:04:56
Reply

Marsh Posté le 21-11-2011 à 02:01:21   

Reply

Marsh Posté le 21-11-2011 à 14:16:42    

Bonjour,
 
Voilà qui devrait faire ce que tu souhaites (testé sur un PHP 5.3.8)

Code :
  1. $domDocument = new DOMDocument();
  2. $domDocument->load('nmap_output.xml');
  3.  
  4. $xpath = new DOMXPath($domDocument);
  5. foreach ($xpath->query("/nmaprun/host[status[@state='up'] and ports/port/state[contains(@state, 'open')]]/address[@addrtype='ipv4']" ) as $element) {
  6.    echo $element->getAttribute('addr')."\n";
  7. }


---------------
Mon Feedback !
Reply

Marsh Posté le 21-11-2011 à 16:32:48    

Nickel merci beaucoup  :jap:  
Ton code est vachement plus compréhensible que le mien  
J'ai apporté un petite correction
 

Code :
  1. <?php
  2.    $domDocument = new DOMDocument();
  3.    $domDocument->load('nmap_output.xml');
  4.    
  5.    $xpath = new DOMXPath($domDocument);
  6.    foreach ($xpath->query("/nmaprun/host[status[@state='up'] and ports/port/state[@state='open']]/address[@addrtype='ipv4']" ) as $element) {
  7.       echo $element->getAttribute('addr')."\n";
  8.     echo '<br />';
  9.    }
  10. ?>


Message édité par bobl'eponge le 21-11-2011 à 16:33:16
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed