0

I am trying to convert a file which contains ip address in the traditional format to a file which contains ip address in the binary format.

The file contents are as follows.

src-ip{ 192.168.64.54 }
dst-ip{ 192.168.43.87 }


The code I have is as follows.

import re
from decimal import *

filter = open("filter.txt", "r")

output = open("format.txt", "w")

for line in filter:
        bytePattern = "([01]?\d\d?|2[0-4]\d|25[0-5])"
        regObj = re.compile("\.".join([bytePattern]*4))
        for match in regObj.finditer(line):
            m1,m2,m3,m4 = match.groups()
            line = line.replace((' '.join([bin(256 + int(x))[3:] for x in '123.123.123.123'.split('.')])),bytePattern)
            print line

The portion line.replace() does not seem to be working fine. The first parameter to line .replace is working fine.(i.e it is converting the ip address into the binary format) But line.replace doesn't seem to work. Any help or clues as to why this happens is appreciated.

4 Answers 4

2
with open('filter.txt') as filter_:
    with open("format.txt", "w") as format: 
        for line in filter_:
            if line != '\n':
                ip = line.split()
                ip[1] = '.'.join(bin(int(x)+256)[3:] for x in ip[1].split('.'))
                ip[4]= '.'.join(bin(int(x)+256)[3:] for x in ip[4].split('.'))
                ip = " ".join(ip) + '\n'
                format.write(ip)
Sign up to request clarification or add additional context in comments.

Comments

1

Why not take advantage of re.sub() instead, to both make your replacements easier and simplify your regex?

import re
from decimal import *

filter = open("filter.txt", "r")

output = open("format.txt", "w")

pattern = re.compile(r'[\d.]+') # Matches any sequence of digits and .'s

def convert_match_to_binary(match)
    octets = match.group(0).split('.')
    # do something here to convert the octets to a string you want to replace
    # this IP with, and store it in new_form
    return new_form

for line in filter:
    line = pattern.sub(convert_match_to_binary, line)
    print line

Comments

0

Your code is very odd:

line = line.replace(
    (' '.join([bin(256 + int(x))[3:] for x in '123.123.123.123'.split('.')])),
    bytePattern
    )

The first argument is a constant that evaluates to '01111011 01111011 01111011 01111011', and bytePattern is the regex "([01]?\d\d?|2[0-4]\d|25[0-5])", so it's effectively this:

line = line.replace('01111011 01111011 01111011 01111011', "([01]?\d\d?|2[0-4]\d|25[0-5])")

This won't do anything if your file doesn't have 01111011 01111011 01111011 01111011 in it.

The .replace() method only replaces literal strings, not regexes.

1 Comment

He wants to convert the original format into a file with the address in binary format.
0

If it is any help here is my old code from DaniWed IP number conversion between dotnumber string and integer with some error check added.

def ipnumber(ip): 
    if ip.count('.') != 3: 
        raise ValueError, 'IP string with wrong number of dots' 
    ip=[int(ipn) for ipn in ip.rstrip().split('.')]
    if any(ipn<0 or ipn>255 for ipn in ip):
        raise ValueError, 'IP part of wrong value: %s' % ip
    ipn=0 
    while ip: 
        ipn=(ipn<<8)+ip.pop(0)
    return ipn 

def ipstring(ip): 
    ips='' 
    for i in range(4): 
        ip,n=divmod(ip,256)
        print n
        if (n<0) or (n>255): 
            raise ValueError, "IP number %i is not valid (%s, %i)." % (ip,ips,n) 
        ips = str(n)+'.'+ips 
    return ips[:-1] ## take out extra point

inp = "src-ip{ 192.168.64.544 } dst-ip{ 192.168.43.87 }"

found=' '
while found:
    _,found,ip = inp.partition('-ip{ ')
    ip,found,inp = ip.partition(' }')
    if ip:
         print ipnumber(ip)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.