Port scanner in python is not working properly

725 views Asked by At

Hey everyone i was trying to make a port scanner using python3. I tried one earlier and it worked but it was not taking arguments directly rather i had to run the program and then give it all the details. So I decided to come up wit a port scanner which could take arguments. This was my code:

#!/usr/bin/env python3

import argparse
import os
import subprocess
import socket

#parsing arguments
parser=argparse.ArgumentParser()
#taking arguments
parser.add_argument("ip_addr", help="IPv4 address of the target")
parser.add_argument("-v", "--verbose", help="give output in verbosity (show even the closed ports)", action="store_true")

args=parser.parse_args()
print(args.ip_addr)


def __port__(port):
    try:
        s=socket.socket()
        s.settimeout(0.5)
        s.connect((args.ip_addr,port))
        print(args.ip_addr)
        return True
    except:
        return False
    finally:
        s.close()




if args.verbose:
    for x in range (0,1025):
        if __port__:
            print("[+] {}:{} is open".format(args.ip_addr,x))
        else:
            print("[+] {}:{} is closed".format(args.ip_addr,x))

else:
    for x in range (0,1025):
        if __port__:
            print("[+] {}:{} is open".format(args.ip_addr,x))

The problem is every time I run this script on any ip address i get all the ports open. This is the result of my script: 192.168.1.1

[+] 192.168.1.1:1 is open
[+] 192.168.1.1:2 is open
[+] 192.168.1.1:3 is open
[+] 192.168.1.1:4 is open
[+] 192.168.1.1:5 is open
[+] 192.168.1.1:6 is open
[+] 192.168.1.1:7 is open
[+] 192.168.1.1:8 is open
[+] 192.168.1.1:9 is open
[+] 192.168.1.1:10 is open
[+] 192.168.1.1:11 is open
[+] 192.168.1.1:12 is open
[+] 192.168.1.1:13 is open
[+] 192.168.1.1:14 is open
[+] 192.168.1.1:15 is open
[+] 192.168.1.1:16 is open
[+] 192.168.1.1:17 is open
[+] 192.168.1.1:18 is open
[+] 192.168.1.1:19 is open
[+] 192.168.1.1:20 is open
[+] 192.168.1.1:21 is open
[+] 192.168.1.1:22 is open
[+] 192.168.1.1:23 is open
[+] 192.168.1.1:24 is open
[+] 192.168.1.1:25 is open
[+] 192.168.1.1:26 is open
[+] 192.168.1.1:27 is open
[+] 192.168.1.1:28 is open
[+] 192.168.1.1:29 is open
...
...
...
[+] 192.168.1.1:1002 is open
[+] 192.168.1.1:1003 is open
[+] 192.168.1.1:1004 is open
[+] 192.168.1.1:1005 is open
[+] 192.168.1.1:1006 is open
[+] 192.168.1.1:1007 is open
[+] 192.168.1.1:1008 is open
[+] 192.168.1.1:1009 is open
[+] 192.168.1.1:1010 is open
[+] 192.168.1.1:1011 is open
[+] 192.168.1.1:1012 is open
[+] 192.168.1.1:1013 is open
[+] 192.168.1.1:1014 is open
[+] 192.168.1.1:1015 is open
[+] 192.168.1.1:1016 is open
[+] 192.168.1.1:1017 is open
[+] 192.168.1.1:1018 is open
[+] 192.168.1.1:1019 is open
[+] 192.168.1.1:1020 is open
[+] 192.168.1.1:1021 is open
[+] 192.168.1.1:1022 is open
[+] 192.168.1.1:1023 is open
[+] 192.168.1.1:1024 is open

my nmap result shows that only port 80 is open. This is my nmap output:

Starting Nmap 7.91 ( https://nmap.org ) at 2020-10-18 11:54 IST
Nmap scan report for 192.168.1.1
Host is up (0.022s latency).
Not shown: 999 closed ports
PORT   STATE SERVICE
80/tcp open  http

Nmap done: 1 IP address (1 host up) scanned in 0.45 seconds
1

There are 1 answers

3
Pierre-Loic On BEST ANSWER

You need to give the value of the port to your function __port__ call (otherwise if __port__ is always true because __port__ is the name of a function) :

if args.verbose:
    for x in range (0,1025):
        if __port__(x):
            print("[+] {}:{} is open".format(args.ip_addr,x))
        else:
            print("[+] {}:{} is closed".format(args.ip_addr,x))

else:
    for x in range (0,1025):
        if __port__(x):
            print("[+] {}:{} is open".format(args.ip_addr,x))