0

I have been working on a small toolkit for awhile now as a way to learn basic python. part of this toolkit is the code that follows this explanation. It is a basic network enumeration script, to simply ping all ip addresses in your local subnet. However I am having an issue implementing a few pieces. first the code

ip1=raw_input()

if ip1 == None:
    ip="192.168.0.x"
else:
    ip=ip1

ipend="1"

while ipend != 255:
    ip.replace("x", ipend) 

    attempt=subprocess.Popen(["ping.exe", ip], stdout=subprocess.PIPE).communicate()[0]

    if ("unreachable" in attempt):
        pass
    else:
        print "The host at ", ip, "is UP!"

    ipend += 1

if ipend == "255":
    subprocess.stop
        raw_input("Thank you for using PTK. Please press enter to exit.")

enum()

so my first issue, I believe is in my str.replace function, I am attempting to and an int to a str and due to this it seems to be unable to create, ping, and print the correct ip. I'm unsure if converting the entire string to a floating int would work, however its a thought I'm toying with. as previously stated I am very basic with python and am working on learning so please forgive me if this a stupid question with a simple fix.

1 Answer 1

2

This line doesn't actually do anything. You need to assign the result to something.

ip.replace("x", ipend) 

ipend is a string, incrementing a string won't work. Make it a int and convert it to a str in the replace function.

 ipend += 1

And it's usually better to use a for loop if you can, so you're safe from messing up and creating a infinite loop.

for ipend in range(1,256): #Range won't actually give you the last number here.
    #Do stuff
Sign up to request clarification or add additional context in comments.

7 Comments

so as an example of the first part i should add something like ip2=ip.replace
awesome thanks for the clean clear answer! that's exactly why I love this site!
You're welcome, did you see the part about for loops?
I'd suggest to add a comment on str.format method usage instead of replace.
Using str.replace seems reasonable. Asking the user of this program to input the ip with an x where it should do replacing seems intuitive. Where would you use str.format here?
|

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.