Porting a Python 2 code to Python 3: Scan ICMP with errors

Asked

Viewed 134 times

1

I have the following code:

import random
import socket
import time
import ipaddress
import struct

from threading import Thread


def checksum(source_string):
    sum = 0
    count_to = (len(source_string) / 2) * 2
    count = 0
    while count < count_to:
        this_val = ord(source_string[count + 1]) * 256 + ord(source_string[count])
        sum = sum + this_val
        sum = sum & 0xffffffff
        count = count + 2
    if count_to < len(source_string):
        sum = sum + ord(source_string[len(source_string) - 1])
        sum = sum & 0xffffffff
    sum = (sum >> 16) + (sum & 0xffff)
    sum = sum + (sum >> 16)
    answer = ~sum
    answer = answer & 0xffff
    answer = answer >> 8 | (answer << 8 & 0xff00)
    return answer


def create_packet(id):
    header = struct.pack('bbHHh', 8, 0, 0, id, 1)
    data = 192 * 'Q'
    my_checksum = checksum(header + data)
    header = struct.pack('bbHHh', 8, 0, socket.htons(my_checksum), id, 1)
    return header + data


def ping(addr, timeout=1):
    try:
        my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
    except Exception as e:
        print (e)
    packet_id = int((id(timeout) * random.random()) % 65535)
    packet = create_packet(packet_id)
    my_socket.connect((addr, 80))
    my_socket.sendall(packet)
    my_socket.close()


def rotate(addr, file_name, wait, responses):
    print ("Sending Packets", time.strftime("%X %x %Z"))
    for ip in addr:
        ping(str(ip))
        time.sleep(wait)
    print ("All packets sent", time.strftime("%X %x %Z"))

    print ("Waiting for all responses")
    time.sleep(2)

    # Stoping listen
    global SIGNAL
    SIGNAL = False
    ping('127.0.0.1')  # Final ping to trigger the false signal in listen

    print (len(responses), "hosts found!")
    print ("Writing File")
    hosts = []
    for response in sorted(responses):
        ip = struct.unpack('BBBB', response)
        ip = str(ip[0]) + "." + str(ip[1]) + "." + str(ip[2]) + "." + str(ip[3])
        hosts.append(ip)
    file = open(file_name, 'w')
    file.write(str(hosts))

    print ("Done", time.strftime("%X %x %Z"))


def listen(responses):
    global SIGNAL
    s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
    s.bind(('', 1))
    print ("Listening")
    while SIGNAL:
        packet = s.recv(1024)[:20][-8:-4]
        responses.append(packet)
    print ("Stop Listening")
    s.close()

SIGNAL = True

responses = []

ips = '200.131.0.0/20' # Internet network
wait = 0.002  # Adjust this based in your bandwidth (Faster link is Lower wait)
file_name = 'log1.txt'

ip_network = ipaddress.ip_network(unicode(ips), strict=False)

t_server = Thread(target=listen, args=[responses])
t_server.start()

t_ping = Thread(target=rotate, args=[ip_network, file_name, wait, responses])
t_ping.start()

What I tried to do:

ip_network = ipaddress.ip_network(unicode(ips), strict=False)

I switched to:

ip_network = ipaddress.ip_network( ips, strict=False)

Because I was getting: "Nameerror: name 'Unicode' is not defined".

Then I got the error: "Typeerror: can’t Concat bytes to str" on this line:

my_checksum = checksum(header + data)

And then I changed:

data =  192 * 'Q' 

To:

data =  bytes(192 * 'Q').encode('utf8')

Now I get the bug:

"data = bytes (192 * 'Q'). Input('utf8') Typeerror: string argument without an encoding"

Could someone help me port the code to Python 3?

  • I have no experience with Python 3, but that I know to create a string of bytes just put b before quotation marks. Try using data = 192 * b'Q' and see if it solves.

  • P.S. From what I understand of built-in bytes, you need to pass the encoding on the function itself, and not call encode afterward - data = bytes(192 * 'Q', 'utf8'). See also bytearray. But my previous suggestion is simpler.

  • @mgibsonbr, I will test. Thank you!

  • I used date = bytes(192 * 'Q', 'utf8') and now the error has changed: " this_val = Ord(source_string[Count + 1]) * 256 + Ord(source_string[Count]) Typeerror: Ord() expected string of length 1, but int found"

  • @ stderr, thanks. If you can explain, I appreciate!

1 answer

2


"Nameerror: name 'Unicode' is not defined"

In Python 3, unicode was renamed to str, and str for bytes. More information.

# Altere essa linha
ip_network = ipaddress.ip_network(unicode(ips), strict=False)

# Ela deve ficar desse jeito
ip_network = ipaddress.ip_network(ips, strict=False)

Typeerror: can’t Concat bytes to str

The error happens in this function:

def create_packet(id):
    header = struct.pack('bbHHh', 8, 0, 0, id, 1)
    data = 192 * 'Q'
    my_checksum = checksum(header + data)
    # ....

The variable header receives struct.pack which returns an object of bytes, data is a string, in Python 3 cannot concatenate bytes and strings, because they are two different types of data. To concatenate, put b as a prefix to string, so she’ll be recognized as a sequence of bytes literal.

def create_packet(id):
    header = struct.pack('bbHHh', 8, 0, 0, id, 1)
    data = 192 * b'Q'
    my_checksum = checksum(header + data)
    # ....

Typeerror: Ord() expected string of length 1, but int found

In Python 3 the variable source_string is an object of bytes, which stores an immutable sequence of integers that are in a range of 0 to 255. Unlike a string, the indexation of an object of bytes returns an integer. See an example:

>>> foo = b'a'
>>> foo[0]
97
>>> 

In this case the function ord is redundant, no need to use it. Change:

# De
this_val = ord(source_string[count + 1]) * 256 + ord(source_string[count]) 
# Para
this_val = source_string[count + 1]) * 256 + source_string[count]

Repeat the process wherever you are using ord.


Along those lines:

ip = str(ip[0]) + "." + str(ip[1]) + "." + str(ip[2]) + "." + str(ip[3])

You can simplify it to:

ip = "{}.{}.{}.{}".format(ip[0], ip[1], ip[2], ip[3])

When saving the results, the file is opened but is not closed with the close:

file = open(file_name, 'w')
file.write(str(hosts))

file.close() # Faltou essa linha!

Alternatively, you can use with:

with open(file_name, 'w') as f:
    f.write('\n'.join(host for host in hosts))

The file identifier is closed automatically after use.


If you want to know more details about how to transition code to Python 3 and what its differences are to Python 2, see the content of the site below:

http://python3porting.com/preparaing.html#Preparing-for-python-3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.