Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with parsing using re
#1
I'm trying to read in a .txt file, parse out the IP addresses and then write those addresses to a new .txt file using the following code. I can pars out the IP addresses but I can't send the output to the output file. Here's the code:
#!/usr/bin/python
#print "Hello, Python!"

import re

with open('Test_nmap.txt', 'r') as myfile:
	with open('output.txt','w') as myoutput:
		myfile_contents = myfile.read()
		ip = re.findall(r'\d+\.\d+\.\d+\.\d+', myfile_contents)
			for ip in myfile_contents
			myoutput.write(ip)
The .py is in the same directory as the Test_nmap.txt file. Everything works except for writing to the new .txt file.

Here's the content of the Test_nmap.txt file:

Starting Nmap 7.70 ( https://nmap.org ) at 2018-07-12 13:07 Central Daylight Time
Nmap scan report for 192.168.1.108
Host is up (0.00s latency).
Not shown: 991 filtered ports
PORT STATE SERVICE
135/tcp open msrpc
139/tcp open netbios-ssn
445/tcp open microsoft-ds
2869/tcp open icslap
49152/tcp open unknown
49153/tcp open unknown
49154/tcp open unknown
49155/tcp open unknown
49156/tcp open unknown
MAC Address: 74:D4:35:29:6F:D9 (Giga-byte Technology)
Warning: OSScan results may be unreliable because we could not find at least 1 open and 1 closed port
Device type: general purpose|media device
Running: Microsoft Windows 2008|10|7|8.1, Microsoft embedded
OS CPE: cpe:/o:microsoft:windows_server_2008::sp2 cpe:/o:microsoft:windows_10 cpe:/h:microsoft:xbox_one cpe:/o:microsoft:windows_7::- cpe:/o:microsoft:windows_7::sp1 cpe:/o:microsoft:windows_8 cpe:/o:microsoft:windows_8.1
OS details: Microsoft Windows Server 2008 SP2, Microsoft Windows Server 2008 SP2 or Windows 10 or Xbox One, Microsoft Windows 7 SP0 - SP1, Windows Server 2008 SP1, Windows Server 2008 R2, Windows 8, or Windows 8.1 Update 1
Network Distance: 1 hop

Nmap scan report for 192.168.1.105
Host is up (0.00s latency).
Not shown: 989 filtered ports
PORT STATE SERVICE
135/tcp open msrpc
139/tcp open netbios-ssn
445/tcp open microsoft-ds
2869/tcp open icslap
5357/tcp open wsdapi
49152/tcp open unknown
49153/tcp open unknown
49154/tcp open unknown
49155/tcp open unknown
49156/tcp open unknown
49158/tcp open unknown
MAC Address: B0:83:FE:B0:4B:B9 (Dell)
Warning: OSScan results may be unreliable because we could not find at least 1 open and 1 closed port
Device type: general purpose|media device
Running: Microsoft Windows 2008|10|7|8.1, Microsoft embedded
OS CPE: cpe:/o:microsoft:windows_server_2008::sp2 cpe:/o:microsoft:windows_10 cpe:/h:microsoft:xbox_one cpe:/o:microsoft:windows_7::- cpe:/o:microsoft:windows_7::sp1 cpe:/o:microsoft:windows_8 cpe:/o:microsoft:windows_8.1
OS details: Microsoft Windows Server 2008 SP2, Microsoft Windows Server 2008 SP2 or Windows 10 or Xbox One, Microsoft Windows 7 SP0 - SP1, Windows Server 2008 SP1, Windows Server 2008 R2, Windows 8, or Windows 8.1 Update 1
Network Distance: 1 hop

Nmap scan report for 192.168.1.102
Host is up (0.000037s latency).
Not shown: 991 filtered ports
PORT STATE SERVICE
135/tcp open msrpc
139/tcp open netbios-ssn
445/tcp open microsoft-ds
5357/tcp open wsdapi
49152/tcp open unknown
49153/tcp open unknown
49154/tcp open unknown
49155/tcp open unknown
49156/tcp open unknown
MAC Address: B0:83:FE:AF:F2:E3 (Dell)
Warning: OSScan results may be unreliable because we could not find at least 1 open and 1 closed port
Device type: general purpose
Running: Microsoft Windows 7|2008|8.1
OS CPE: cpe:/o:microsoft:windows_7::- cpe:/o:microsoft:windows_7::sp1 cpe:/o:microsoft:windows_server_2008::sp1 cpe:/o:microsoft:windows_server_2008:r2 cpe:/o:microsoft:windows_8 cpe:/o:microsoft:windows_8.1
OS details: Microsoft Windows 7 SP0 - SP1, Windows Server 2008 SP1, Windows Server 2008 R2, Windows 8, or Windows 8.1 Update 1
Network Distance: 1 hop

Nmap scan report for 192.168.1.101
Host is up (0.00s latency).
Not shown: 996 filtered ports
PORT STATE SERVICE
5357/tcp open wsdapi
16992/tcp open amt-soap-http
49154/tcp open unknown
49155/tcp open unknown
MAC Address: 00:0A:CD:25:B6:C0 (Sunrich Technology Limited)
Warning: OSScan results may be unreliable because we could not find at least 1 open and 1 closed port
Device type: general purpose|media device
Running: Microsoft Windows 2008|10|7|8.1, Microsoft embedded
OS CPE: cpe:/o:microsoft:windows_server_2008::sp2 cpe:/o:microsoft:windows_10 cpe:/h:microsoft:xbox_one cpe:/o:microsoft:windows_7::- cpe:/o:microsoft:windows_7::sp1 cpe:/o:microsoft:windows_8 cpe:/o:microsoft:windows_8.1
OS details: Microsoft Windows Server 2008 SP2, Microsoft Windows Server 2008 SP2 or Windows 10 or Xbox One, Microsoft Windows 7 SP0 - SP1, Windows Server 2008 SP1, Windows Server 2008 R2, Windows 8, or Windows 8.1 Update 1
Network Distance: 1 hop

Nmap scan report for 192.168.1.100
Host is up (0.00s latency).
Not shown: 991 filtered ports
PORT STATE SERVICE
135/tcp open msrpc
139/tcp open netbios-ssn
445/tcp open microsoft-ds
2869/tcp open icslap
49152/tcp open unknown
49153/tcp open unknown
49154/tcp open unknown
49155/tcp open unknown
49159/tcp open unknown
MAC Address: FC:4D:D4:D3:C0:A7 (Universal Global Scientific Industrial)
Warning: OSScan results may be unreliable because we could not find at least 1 open and 1 closed port
Device type: general purpose
Running: Microsoft Windows Vista|7
OS CPE: cpe:/o:microsoft:windows_vista cpe:/o:microsoft:windows_7::sp1
OS details: Microsoft Windows Vista or Windows 7 SP1
Network Distance: 1 hop

Nmap scan report for 192.168.1.10
Host is up (0.00s latency).
Not shown: 987 filtered ports
PORT STATE SERVICE
135/tcp open msrpc
139/tcp open netbios-ssn
445/tcp open microsoft-ds
2968/tcp open enpp
3389/tcp open ms-wbt-server
8888/tcp open sun-answerbook
49152/tcp open unknown
49153/tcp open unknown
49154/tcp open unknown
49155/tcp open unknown
49156/tcp open unknown
49158/tcp open unknown
49159/tcp open unknown
MAC Address: F8:BC:12:97:CC:7B (Dell)
Warning: OSScan results may be unreliable because we could not find at least 1 open and 1 closed port
Device type: general purpose
Running: Microsoft Windows 7|2008|8.1
OS CPE: cpe:/o:microsoft:windows_7::- cpe:/o:microsoft:windows_7::sp1 cpe:/o:microsoft:windows_server_2008::sp1 cpe:/o:microsoft:windows_server_2008:r2 cpe:/o:microsoft:windows_8 cpe:/o:microsoft:windows_8.1
OS details: Microsoft Windows 7 SP0 - SP1, Windows Server 2008 SP1, Windows Server 2008 R2, Windows 8, or Windows 8.1 Update 1
Network Distance: 1 hop

OS detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 6 IP addresses (6 hosts up) scanned in 277.77 seconds


Any help would be greatly appreciated.
Reply
#2
The code you gave won't run, but assuming you dedent the for loop and give it a colon, you are looping over the wrong thing. You are looping over myfile_contents, which is just the original, unparsed file. You want to loop over ip, which is where you stored all of the ip addresses.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
It will work if you change what @ichabod801 posted.

Here to show other way with re.finditer()
Taken read()(all to string) and findall()(all to list) out of picture.
Now only line bye line get read to memory.
This can be useful if your file was bigger that this.
import re

with open('test.txt') as f,open('ip.txt','w') as f_out:
    for line in f:
        ip = re.compile(r'\d+\.\d+\.\d+\.\d+')
        for match in ip.finditer(line):
            f_out.write(f'{match.group()}\n')
Output:
192.168.1.108 192.168.1.105 192.168.1.102 192.168.1.101 192.168.1.100 192.168.1.10
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020