python 扫描局域网主机、爆破端口

简介: python 扫描局域网主机、爆破端口
import telnetlib
from multiprocessing import Pool 
import threading
import time
import sys
import re
import os
class Scan():
    def __init__(self,ip,mode):
        self.__ip=ip
        self.__open=[]
        self.__mode=mode
    def check_host(self,ip):
        output = os.popen('ping -n 1 {}'.format(ip)).readlines()
        if('无法访问目标主机' not in ''.join(output)):
            return ip
    def scanning(self):    #探测ip类型  
        ip_list=[]
        if(re.match(r'^\d+?\.\d+?\.\d+?\.\d+?$',self.__ip)):             #单一ip
            output = os.popen('ping -n 1 {}'.format(self.__ip)).readlines()
            if('无法访问目标主机'not  in ''.join(output)):                
                ip_list.append(self.__ip)
        elif(re.match(r'^(\d+?\.\d+?\.\d+?\.)(\d+?)/24$',self.__ip)):    #检测局域网存活主机
            ip_list.clear()
            t=self.__ip.split()[0]
            ip_result=[re.match(r'(\d+?\.\d+?\.\d+?\.)(\d+?)/24',self.__ip).group(1)+str(i) for i in range(0,256)]
            with  Pool(10) as pool:
                ip_result=pool.map(self.check_host,ip_result)
                for i in ip_result:
                    if(i!=None):
                        ip_list.append(i)
        elif(re.match(r'^(\d+?\.\d+?\.\d+?\.)(\d+?)-(\d+?\.\d+?\.\d+?\.)(\d+)$',self.__ip)):   #检测范围内ip
            ip_list.clear()
            min=int(re.match(r'(\d+?\.\d+?\.\d+?\.)(\d+?)-(\d+?\.\d+?\.\d+?\.)(\d+)',self.__ip).group(2))
            max=int(re.match(r'(\d+?\.\d+?\.\d+?\.)(\d+?)-(\d+?\.\d+?\.\d+?\.)(\d+)',self.__ip).group(4))+1
            ip_result=[re.match(r'(\d+?\.\d+?\.\d+?\.)(\d+?)-(\d+?\.\d+?\.\d+?\.)(\d+?)',self.__ip).group(1)+str(i) for i in range(min,max)]
            with  Pool(10) as pool:
                ip_result=pool.map(self.check_host,ip_result)
                for i in ip_result:
                    if(i!=None):
                        ip_list.append(i)
        elif(re.match(r'^(\d+?\.\d+?\.\d+?\.)(\d+?),',self.__ip)):   #检测多个自定义ip
            ip_list.clear()
            list_result=self.__ip.split(',')
            for ip in list_result:
                if(re.match(r'\d+?\.\d+?\.\d+?\.\d+?',ip)):
                    output = os.popen('ping -n 1 {}'.format(ip)).readlines()
                    if('无法访问目标主机'not in ''.join(output)):
                        ip_list.append(ip)
        else:
            sys.exit('\n\nthe input of ip is wrong! please try again!')
        if(len(ip_list)==0):
            sys.exit('\n\nthe host of ip inputed is not exits!!!\n\n')
        print("\n\n","扫描进行中".center(21,'*'),"\n")   
        print("\n存活的主机:\n\n"+'\n'.join(ip_list)+'\n\n')
        for ip in ip_list:
            self.__ip=ip
            if(self.__mode==0):
                port_list=[80,8080,443,3306,3389,21,22]
            elif(self.__mode==1):
                port_list=[x for x in range(1,65535)]
            else:
                sys.exit('\nthe input of mode is wrong! please try again!')
            pool=Pool(6)
            list_result=pool.map(self.is_open,port_list)
            pool.close()      
            pool.join()    
            for i in list_result:
                if(i != None):
                    self.__open.append(i)
            self.__open.sort()
            self.Get()
    def is_open(self,port): #判断是否开启
        check=telnetlib.Telnet()  #扫描函数
        try:
            check.open(self.__ip,port)
            return str(port)
        except:
            pass
    def Get(self):
        if(len(self.__open)!=0):
            print("\n "+self.__ip+"开放的端口: \n "+'\n '.join(self.__open)+"\n")
        else:
            print("\n "+self.__ip+"开放的端口: \n "+'\n 没有开放端口'+"\n")
if __name__ == "__main__":
    start=time.time()
    s=Scan(input('\n\nthe type of ip  1. xxx.xxx.xxx.xxx  2. xxx.xxx.xxx.xxx-xxx.xxx.xxx.xxx\
    3. xxx.xxx.xxx.xxx/24 4. xxx.xxx.xxx.xxx,xxx.xxx.xxx.xxx,…… \ninput ip: '),int(input("\n\n0. scan the usual ports  1. scan all ports\nmode: ")))
    s.scanning()
    # s.Get()
    print("\n扫描用时: "+str(round(time.time()-start,2))+ "s")
    print("\n\n","扫描完成,欢迎下次使用".center(21,'*'),'\n\n')
本程序采用了多线程来自动扫描指定局域网主机、端口
目录
相关文章
|
2月前
|
存储 NoSQL 数据库连接
在Python程序中实现LevelDB的海量key的分批次扫描
通过本文的步骤,您可以在Python程序中实现对LevelDB海量key的分批次扫描。这样不仅能够有效地管理大规模数据,还可以避免一次性加载过多数据到内存中,提高程序的性能和稳定性。希望这篇指南能为您的开发工作提供实用的帮助。
85 28
|
5月前
|
Python
Python编程--使用NMAP端口扫描
Python编程--使用NMAP端口扫描
59 1
|
5月前
|
网络安全 Python
Python编程--目标IP地址段主机指定端口状态扫描
Python编程--目标IP地址段主机指定端口状态扫描
89 1
|
5月前
|
运维 安全 网络协议
Python 网络编程:端口检测与IP解析
本文介绍了使用Python进行网络编程的两个重要技能:检查端口状态和根据IP地址解析主机名。通过`socket`库实现端口扫描和主机名解析的功能,并提供了详细的示例代码。文章最后还展示了如何整合这两部分代码,实现一个简单的命令行端口扫描器,适用于网络故障排查和安全审计。
96 0
|
5月前
|
安全 网络协议 IDE
使用Python编写网络扫描程序
使用Python编写网络扫描程序
83 0
|
6月前
|
监控 网络协议 数据库连接
Python3 监控端口:使用 socket 库
Python3 监控端口:使用 socket 库
89 0
|
Linux Python
使用python扫描文件夹获取所有文件路径
使用python扫描文件夹获取所有文件路径
221 0
|
12天前
|
机器学习/深度学习 存储 设计模式
Python 高级编程与实战:深入理解性能优化与调试技巧
本文深入探讨了Python的性能优化与调试技巧,涵盖profiling、caching、Cython等优化工具,以及pdb、logging、assert等调试方法。通过实战项目,如优化斐波那契数列计算和调试Web应用,帮助读者掌握这些技术,提升编程效率。附有进一步学习资源,助力读者深入学习。
|
12天前
|
机器学习/深度学习 数据可视化 TensorFlow
Python 高级编程与实战:深入理解数据科学与机器学习
本文深入探讨了Python在数据科学与机器学习中的应用,介绍了pandas、numpy、matplotlib等数据科学工具,以及scikit-learn、tensorflow、keras等机器学习库。通过实战项目,如数据可视化和鸢尾花数据集分类,帮助读者掌握这些技术。最后提供了进一步学习资源,助力提升Python编程技能。
|
12天前
|
设计模式 机器学习/深度学习 前端开发
Python 高级编程与实战:深入理解设计模式与软件架构
本文深入探讨了Python中的设计模式与软件架构,涵盖单例、工厂、观察者模式及MVC、微服务架构,并通过实战项目如插件系统和Web应用帮助读者掌握这些技术。文章提供了代码示例,便于理解和实践。最后推荐了进一步学习的资源,助力提升Python编程技能。

热门文章

最新文章