Python

Moderators: None (Apply to moderate this forum)
Number of threads: 474
Number of posts: 1166

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Parsing something with regex Posted by dbrandt on 12 Jun 2009 at 8:55 PM
I'm trying to parse some output of the program Nmap with regular expressions.

From this output:

Starting Nmap 4.76 ( http://nmap.org ) at 2009-06-12 22:53 CDT
Interesting ports on jb-Laptop (127.0.0.1):
Not shown: 984 closed ports
PORT      STATE SERVICE
21/tcp    open  ftp
22/tcp    open  ssh
25/tcp    open  smtp
110/tcp   open  pop3
139/tcp   open  netbios-ssn
143/tcp   open  imap
445/tcp   open  microsoft-ds
465/tcp   open  smtps
587/tcp   open  submission
631/tcp   open  ipp
993/tcp   open  imaps
995/tcp   open  pop3s
2020/tcp  open  xinupageserver
5222/tcp  open  unknown
5900/tcp  open  vnc
10000/tcp open  snet-sensor-mgmt

Interesting ports on 192.168.0.1:
Not shown: 999 closed ports
PORT   STATE SERVICE
80/tcp open  http
MAC Address: 00:09:5B:AA:7E:1A (Netgear)

Nmap done: 2 IP addresses (2 hosts up) scanned in 4.35 seconds


...I'd like to get this:

['21/tcp    open  ftp
22/tcp    open  ssh
25/tcp    open  smtp
110/tcp   open  pop3
139/tcp   open  netbios-ssn
143/tcp   open  imap
445/tcp   open  microsoft-ds
465/tcp   open  smtps
587/tcp   open  submission
631/tcp   open  ipp
993/tcp   open  imaps
995/tcp   open  pop3s
2020/tcp  open  xinupageserver
5222/tcp  open  unknown
5900/tcp  open  vnc
10000/tcp open  snet-sensor-mgmt',
'80/tcp open  http']



Right now, I can do as follows:
portdata = re.search("PORT\s+STATE\s+SERVICE.*\d/\S+\s+\S+\s+\S+", data, re.DOTALL)


This works when I'm getting the ports for just one host, but for more than one it won't match correctly. Any htelp?
Report
Re: Parsing something with regex Posted by ghostdog74 on 18 Jun 2009 at 8:10 PM
portdata="""
Starting Nmap 4.76 ( http://nmap.org ) at 2009-06-12 22:53 CDT
Interesting ports on jb-Laptop (127.0.0.1):
Not shown: 984 closed ports
PORT      STATE SERVICE
21/tcp    open  ftp
22/tcp    open  ssh
25/tcp    open  smtp
110/tcp   open  pop3
139/tcp   open  netbios-ssn
143/tcp   open  imap
445/tcp   open  microsoft-ds
465/tcp   open  smtps
587/tcp   open  submission
631/tcp   open  ipp
993/tcp   open  imaps
995/tcp   open  pop3s
2020/tcp  open  xinupageserver
5222/tcp  open  unknown
5900/tcp  open  vnc
10000/tcp open  snet-sensor-mgmt

Interesting ports on 192.168.0.1:
Not shown: 999 closed ports
PORT   STATE SERVICE
80/tcp open  http
MAC Address: 00:09:5B:AA:7E:1A (Netgear)

Nmap done: 2 IP addresses (2 hosts up) scanned in 4.35 seconds
"""
    
portdata=portdata.split("\n")
for items in portdata:
    if "/" in items and not "Starting" in items:
        print items        



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.