1
class Interface():
    def __init__(self, localIP, remoteIP, numHops, hops):
        self.localIP = localIP
        self.remoteIP = remoteIP
        self.numHops = numHops
        self.hops = []

I want to create an instance like this:

hops, hopIps = stripRoute(ssh.run("traceroute -I " + str(dstHost.ip), hide=True))
host.interfaces.append(Interface(host.ip, dstHost.ip, hops, hopIps))
print(hops)
print(hopIps)

From the print statements I can see that hopIps has a value and length 1 which is expected. However when I then query the new instance of Interface, only the numHops value was updated, hops remains empty.

2
  • 1
    Side note: Is it really necessary to have numHops? Shouldn't it always be equal to len(hops)? Commented Nov 20, 2019 at 13:52
  • If the packet only passes through a switch it wont be recorded as a hop(ttl value of packet wont decrease) and therefore len(hops) would be zero but the numHops value would remain 1 Commented Nov 20, 2019 at 13:59

1 Answer 1

6

You passed a list into __init__ but never used it

class Interface():
    def __init__(self, localIP, remoteIP, numHops, hops):
        self.localIP = localIP
        self.remoteIP = remoteIP
        self.numHops = numHops
        self.hops = []

Just assign your list to the member

class Interface():
    def __init__(self, localIP, remoteIP, numHops, hops):
        self.localIP = localIP
        self.remoteIP = remoteIP
        self.numHops = numHops
        self.hops = hops
Sign up to request clarification or add additional context in comments.

1 Comment

maybe you want to make make a copy of hops like this self.hops = hops.copy() to prevent some unwanted behavior?

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.