Domanda

I am writing a program in Python and a part of it is scanning for active ports on a website. in the module scanports, while if I were to say scan ports 79 to 81, I know that it should return a list with 80 in it. I know this for sure because when I run scanport it shows port 80 is up. Sorry for not having any comments:

import subprocess, socket, urllib2, sys
class pymap:
    def __init__(self):
        pass

################################################################################
################################################################################

    def host(self, host):
        self.host = host
        socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock = socket1

################################################################################
################################################################################

    def getip(self):
        if self.host == None:
            print "Specify a host first."
        else:
            return socket.gethostbyname(self.host)

################################################################################
################################################################################

    def scanports(self, start, end):
        ports = []
        self.sock.settimeout(0.000001)
        for i in xrange(start, end+1):  #49151
            try:
                self.sock.connect((self.host, i))
                ports.append(i)
            except:
                pass
        return i

################################################################################
################################################################################

    def scanport(self, port1):
        self.sock.settimeout(0.000001)
        try:
            self.sock.connect((self.host, port1))
            return 1
        except:
            return 0

################################################################################
################################################################################

    def traceroute(self):
        if self.host == None:
            print "Specify a host first."

        else:
            proc=subprocess.Popen(('tracert', self.host), shell=True, stdout=subprocess.PIPE)
            output=proc.communicate()[0]
            return output

################################################################################
################################################################################

    def getsource(self, url):
        page = urllib2.urlopen(url)
        return page.read()

################################################################################
################################################################################

x = pymap()
x.host("www.google.com")
print x.scanports(70, 85)
print x.scanport(80)

EDIT: I changed it, thanks James Henstridge for pointing out that I was using the iteration variable, otherwise it would be much harder. However, it still doesn't work:

    def scanports(self, start, end):
        ports = []
        self.sock.settimeout(3)
        for i in xrange(start, end+1):  #49151
            try:
                self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                self.sock.connect((self.host, port1))
                self.sock.close()
                ports.append(i)
            except:
                pass
            return ports

EDIT: I figured it out, it was a problem with ports.append, thanks for your help.

È stato utile?

Soluzione

There are a few issues with your code:

  1. Your scanports method is returning the loop iteration variable, so it will always return end no matter what ports it detects. Perhaps you meant to return the ports list instead?

  2. You are reusing the same socket over and over. From the connect system call man page:

    Generally, connection-based protocol sockets may successfully connect() only once

    So if you want to test multiple connection attempts, create a new socket each time. You should also close those sockets after use.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top