Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[adavance] 增加批量IP扫描的可能性 #3

Open
jingyuexing opened this issue Sep 10, 2022 · 1 comment
Open

[adavance] 增加批量IP扫描的可能性 #3

jingyuexing opened this issue Sep 10, 2022 · 1 comment

Comments

@jingyuexing
Copy link

jingyuexing commented Sep 10, 2022

class IPv4:
    ip = ""
    integer = 0
    end = 0
    def __init__(self, address =0):
        if isinstance(address, int):
            self.integer = address
            self.ip = self.toString(address)
        if isinstance(address, str):
            self.ip = address
            self.integer = self.toInteger(address)


    def toString(self, integer:int):
        return f'{(integer>>24)&0xff}.{(integer>>16)&0xff}.{(integer>>8)&0xff}.{(integer>>0)&0xff}'

    def toInteger(self, address:str="0.0.0.0"):
        """[summary]

        [description]

        Arguments:
            address {[type]} -- [description]

        Returns:
            [type] -- [description]
        """
        ipNumber = 0
        partIp = address.split('.')
        i = 0
        for x in partIp:
            ipNumber = ipNumber + int(x)*(0x100**(3-i))
            i = i+1
        return ipNumber

    def range(self, start: int = 0, end: int = 0, ipType=""):
        self.end = end
        IPRange = {
            "A": ["0.0.0.0", "127.255.255.255"],
            "B": ["128.0.0.0", "191.255.255.255"],
            "C": ["192.0.0.0", "223.255.255.255"],
            "D": ["224.0.0.0", "239.255.255.255"],
            "E": ["240.0.0.0", "247.255.255.255"]
        }
        this = self

        class IpRangeIter:
            def __iter__(self):
                self.count = start
                return self

            def __next__(self):
                if(ipType == 'A'):
                    this.integer = this.toInteger(IPRange['A'][0])
                    this.end = this.toInteger(IPRange['A'][1])
                elif(ipType == 'B'):
                    this.integer = this.toInteger(IPRange['B'][0])
                    this.end = this.toInteger(IPRange['B'][1])
                elif(ipType == 'C'):
                    this.integer = this.toInteger(IPRange['C'][0])
                    this.end = this.toInteger(IPRange['C'][1])
                elif(ipType == 'D'):
                    this.integer = this.toInteger(IPRange['D'][0])
                    this.end = this.toInteger(IPRange['D'][1])
                elif(ipType == 'E'):
                    this.integer = this.toInteger(IPRange['E'][0])
                    this.end = this.toInteger(IPRange['E'][1])
                if(self.count < this.end):
                    integervalue = this.integer + self.count
                    val = this.toString(integervalue)
                    self.count += 1
                    return (val, integervalue)
                else:
                    raise StopIteration()
        return iter(IpRangeIter())
@jingyuexing
Copy link
Author

用法如下:

for ip,_ in IPv4().range(ipType="C"):
        print(ip)

扫描全网C段IP

或者你也可以指定某个ip段

for ip,_ in IPv4("127.0.0.1").range(0,255):
        print(ip)

会生成 从127.0.0.1 - 127.0.0.255, 从零开始,到255结束

由于你的代码实在是不好下手,我只是看到了scan 函数就放弃了,实在是不怎么好的阅读体验

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant