#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Parsing nmap output


    OK, i have nmap output like this below, i have it all parsed in variable $lines
    The lip adresses and the lines in format "22/tcp open ssh" are those which have importance to me
    Code:
    Starting Nmap 5.00 ( http://nmap.org ) at 2012-04-19 20:08 CEST
    Interesting ports on 192.168.70.6:
    Not shown: 1022 closed ports
    PORT   STATE SERVICE
    22/tcp open  ssh
    23/tcp open  telnet
    
    Interesting ports on 192.168.70.25:
    Not shown: 1021 closed ports
    PORT    STATE SERVICE
    21/tcp  open  ftp
    80/tcp  open  http
    443/tcp open  https
    
    Nmap done: 256 IP addresses (2 hosts up) scanned in 7.27 seconds
    there may be multiple block of results in output not just two.
    I need to get IP adresses and their corresponding Port_number protocol, state and name of service

    My code which take care of parsing "Port_number protocol, state and name of service" is>
    PHP Code:
    $port_mappings preg_grep('!^\d+/\S+ +\S+ !'$lines); // get the interesting lines
            
    $parsed_port_mappings = array();
            foreach(
    $port_mappings as $port_mapping)
            {
                
    preg_match('!^(?P<port>\d+)/(?P<protocol>\S+) +(?P<state>\S+) +(?P<service>\S+)!'$port_mapping$parsed_port_mappings[]);
            } 
    when i want to access some value its get done by $parsed_port_mappings[X]['service'] where X is the number of line which has format like "21/tcp open ftp"

    The problem is i need to make som adjustment which provide me some machanism to parse also IP addresses and their coresponding "Port_number protocol, state and name of service" best would be multiarray like "$parsed_port_mappings[X][Y]['service']" probably, where X will be the IP address and Y will be the corresponding lines or any other solution which will fit here. Thanks for your help.

    This is not entirely regex question, but i think it is more REGEX than PHP. thx
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2016
    Posts
    20
    Rep Power
    0
    Code:
    (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|^\d+\/.*)
    with /g/m options.

IMN logo majestic logo threadwatch logo seochat tools logo