r/PythonLearning • • Aug 21 '25

Just Escaped Tutorial Hell : Built My Own Python Port Scanner + Os Detection 🔥

45 Upvotes

11 comments sorted by

1

u/Twenty8cows Aug 21 '25

Question. On lines 53-59. What happens to the result variable if you enter that except block? Since you’re not assigning anything to result in the except block would output = result.stdout return a None? Or an undefined error? Or an error at all?

2

u/ahmedhamza268 Aug 21 '25

If subprocess.run() fails, the code jumps into the except block. It prints the failure message, then hits return. In Python, return with no value means the function returns None. Because of that, execution never reaches the output = result.stdout line.

1

u/CarefulFun420 Aug 21 '25

TTL is set by your clients ping command

Time to live, basically how many hops the packet is allowed before it fails

Using it to detect OS is incorrect

1

u/ahmedhamza268 Aug 21 '25

Not exactly ,the TTL in a ping reply isn’t set by my client. It’s set by the remote host’s OS when it builds the packet. Different OSes use different default TTL values (Windows=128, Linux=64, Cisco=255, etc.), which is why TTL can give a decent OS fingerprint. Of course, it’s not 100% accurate because routing hops decrement TTL, but it’s a legit technique (even nmap uses it as part of OS detection). 😉"

2

u/CarefulFun420 Aug 21 '25

Do ping -h, there's an option to set the ttl

1

u/ahmedhamza268 Aug 21 '25

Yes bro, you’re technically right I can set the TTL for my outgoing packets. But that’s not what I’m looking at. I’m checking the TTL in the reply from the remote host, which is set by its OS, not mine.

2

u/CarefulFun420 Aug 21 '25

Arhhh good to know, thanks

1

u/ahmedhamza268 Aug 21 '25

You're welcome !

1

u/Kbang20 Aug 24 '25

Looks awesome! Next thing you can do since the script name is nmap, you could import nmap and now do more complex port scan with nmap like -sCV or something like that and that way you dont have to hard code the common port values.

1

u/ahmedhamza268 Aug 24 '25

Thanks for your feedback . I will try !.