首页
python
Win桌面应用自动化pywinauto
pywinauto遍历展示程序的所有菜单项
Python3使用串口
Python字符串处理
Python3实现配置文件差异对比(键值对key=value的形式)
正则
正则匹配两个字符之间的字符串
正则提取markdown中图片链接
正则表达式基本知识
字符串-正则实例
Python生成8位随机字符串的一些方法
python3批量ping检测
python3检测一批主机ip的tcp端口
Python3--DNS
python3备份交换机配置
Pyhton3--Win注册表
python远程开机工具
端口的问题以及绑定端口(Python)
Python-网络
检测到win电脑断网就发出告警声音
Python编写的DNS服务可靠性测试程序
Python编写的TCP/UDP端口探测程序
Python抓包-Pyshark
3D人体骨架检测(mediapipe)
VSCode编辑器
开源IT运维项目
python知识点
Python3使用snmp获取H3C交换机arp表
Python检测tcp端口状态并发送到企微群
Python3使用smtp发邮件
Python3使用smtp发邮件(带附件)
Python3拉取微软AD域所有用户信息
Python3使用微软AD域账号认证
自动更新公网域名的解析(阿里云)
每日定时推送Excel中排班表信息到企微群
Django
Django模板标签
Django在发送POST请求时返回403错误
Django-mysql数据库条件查询
uwsgi 配置 python virtualenv 虚拟环境目录 ( ini 配置)
常用收藏
python3数据类型转换
python处理文本
txt每行一个字符串_用英文逗号连接
txt_匹配并拆分出需要字符串
python获取日期时间
python执行linux命令的三种方式
python使用ssh连接到linux服务器执行命令
本文档由 内网文摘 发布,转载请注明出处
-
+
home page
python3检测一批主机ip的tcp端口
待检测的服务器ip地址;每行一个ip地址 `cat hostip.txt` ``` 100.50.235.116 100.50.118.26 ``` 要检测哪些tcp端口,修改脚本中 `portlist = [9090, 10000, 20000, 30000]` 运行后生成的日志文件;[+]表示通,[-]表示不通 `cat port_scan_host.log` ``` 2021 - 07 - 09 17 : 22 : 27 IP = 100.50.235.116 [+]网络tcp端口 9090 open 2021 - 07 - 09 17 : 22 : 29 IP = 100.50.118.26 [-]网络tcp端口 9090 close ``` ## V 0.1版本,扫描只针对IP用多并发 port_scan_net_v0.1.py ```python #import subprocess from socket import * import logging import datetime import time import threading from queue import Queue import re import sys # 实现日志导出 #批量扫描端口,根据批量ping修改为tcp端口扫描 #hostip.txt中每行写一个ip地址,运行时根据输入ip或net来扫描单个地址或ip对应的C类网段。 # def set_logging_format(): logging.basicConfig(level=logging.INFO, format='%(message)s', filename='port_scan_host.log', filemode='w' ) console = logging.StreamHandler() console.setLevel(logging.INFO) formatter = logging.Formatter('%(message)s') console.setFormatter(formatter) logging.getLogger('').addHandler(console) # 将需要ping 连接的IP加入队列 def insert_ip_queue(ip_list_path): IP_QUEUE = Queue() with open (ip_list_path,'r') as f: for ip in f.readlines(): ip = ip.strip('\n') # 判断一个字符串是否包含合法IP地址 ip = re.search(r'((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)', ip) if ip: ip = ip.group() # 提取出合法ip地址 IP_QUEUE.put(ip) f.close() return IP_QUEUE def IP_list (ip_list_path): ip_list = Queue() with open (ip_list_path,'r') as f: for ip in f.readlines(): ip = ip.strip('\n') # 判断一个字符串是否包含合法IP地址 ip = re.search(r'((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)', ip) if ip: ip = ip.group() # 提取出合法ip地址 list_segment = ip.split('.') ip_index = 1 # 将需要 ping 的 ip 加入队列 for i in range(0, 254): list_segment[-1] = str(ip_index + i) addr = ('.').join(list_segment) print(addr) ip_list.put(addr) f.close() return ip_list # print (IP_list()) #print (IP_list()) #定义 ping 函数 def scan_IP (IP_QUEUE): while not IP_QUEUE.empty(): ip = IP_QUEUE.get().strip('\n') #print (ip) ##res = subprocess.call('ping -w 1000 -n 1 %s' % ip , stdout=subprocess.PIPE,shell=True) portlist = [9090, 10000, 20000, 30000] for port in portlist: try: s = socket(AF_INET, SOCK_STREAM) s.connect((ip, port)) print('[+] %d open' % port) res = ('[+]网络tcp端口 %d open' % port) today = datetime.datetime.now().strftime("%Y - %m - %d %H : %M : %S") logging.info("%s IP = %s %s" % (today, ip, res)) s.close() except: print('[-] %d close' % port) res = ('[-]网络tcp端口 %d close' % port) today = datetime.datetime.now().strftime("%Y - %m - %d %H : %M : %S") logging.info("%s IP = %s %s" % (today, ip, res)) def main (): set_logging_format() ip_list_path = './hostip.txt' # IP_QUEUE = insert_ip_queue(ip_list_path) # IP_LIST = IP_list(ip_list_path) threads = [] THREAD_NUM = 200 user_iput = input('please input modren:(ip|net) ') if user_iput == 'net': IP_LIST = IP_list(ip_list_path) IP_L = IP_LIST if user_iput == 'ip': IP_QUEUE = insert_ip_queue(ip_list_path) IP_L = IP_QUEUE for i in range (THREAD_NUM): t = threading.Thread(target = scan_IP, args = (IP_L,)) threads.append(t) for i in range (THREAD_NUM): threads[i].start() for i in range (THREAD_NUM): threads[i].join() if __name__ == '__main__': main() ``` 设置超时时间、设置端口为所有 ``` # 需要导入模块: import socket [as 别名] # 或者: from socket import setdefaulttimeout [as 别名] # socket.setdefaulttimeout(1) from socket import * setdefaulttimeout(1) #设置默认超时1秒 0.08为80毫秒 portlist = list(range(1,65536)) ``` ## V 0.2版本,扫描IP和端口都用多并发 ```python #import subprocess from socket import * import logging import datetime import time import threading from queue import Queue import re import sys import traceback setdefaulttimeout(0.08) # 实现日志导出 #批量扫描端口,根据批量ping修改为tcp端口扫描 #hostip.txt中每行写一个ip地址,运行时根据输入ip或net来扫描单个地址或ip对应的C类网段。 # def set_logging_format(): logging.basicConfig(level=logging.INFO, format='%(message)s', filename='port_scan_host.log', filemode='w' ) console = logging.StreamHandler() console.setLevel(logging.INFO) formatter = logging.Formatter('%(message)s') console.setFormatter(formatter) logging.getLogger('').addHandler(console) # 将需要ping 连接的IP加入队列 def IP2ipp(dip): IP2ipp_list = [] portlist = list(range(1,65536))#[3306] for dport in portlist: try: dipport = (':').join([dip,str(dport)]) print(dipport) IP2ipp_list.append(dipport) #return dipport except Exception as e: print(traceback.format_exc()) # print("addipport-err") return IP2ipp_list def insert_ip_queue(ip_list_path): IP_QUEUE = Queue() with open (ip_list_path,'r') as f: for ip in f.readlines(): ip = ip.strip('\n') # 判断一个字符串是否包含合法IP地址 ip = re.search(r'((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)', ip) if ip: ip = ip.group() # 提取出合法ip地址 ip = IP2ipp(str(ip)) for i in ip: IP_QUEUE.put(i) f.close() return IP_QUEUE def IP_list (ip_list_path): ip_list = Queue() with open (ip_list_path,'r') as f: for ip in f.readlines(): ip = ip.strip('\n') # 判断一个字符串是否包含合法IP地址 ip = re.search(r'((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)', ip) if ip: ip = ip.group() # 提取出合法ip地址 list_segment = ip.split('.') ip_index = 1 # 将需要 ping 的 ip 加入队列 for i in range(0, 254): list_segment[-1] = str(ip_index + i) addr = ('.').join(list_segment) print(addr) ip = IP2ipp(addr) for i in ip: ip_list.put(i) f.close() return ip_list # print (IP_list()) #print (IP_list()) #定义 ping 函数 def scan_IP (IP_QUEUE): while not IP_QUEUE.empty(): # print(type(IP_QUEUE.get()),IP_QUEUE.get()) ipp = IP_QUEUE.get().strip('\n') #print (ip) ##res = subprocess.call('ping -w 1000 -n 1 %s' % ip , stdout=subprocess.PIPE,shell=True) ip_port = ipp.split(':') #time.sleep(10) ip = ip_port[0] port = eval(ip_port[1]) # portlist = list(range(1,65536))#[3306] #for port in portlist: try: s = socket(AF_INET, SOCK_STREAM) s.connect((ip, port)) print('[+] %d open' % port) res = ('[+]网络tcp端口 %d open' % port) today = datetime.datetime.now().strftime("%Y - %m - %d %H : %M : %S") logging.info("%s IP = %s %s" % (today, ip, res)) s.close() except: print('[-] %d close' % port) res = ('[-]网络tcp端口 %d close' % port) today = datetime.datetime.now().strftime("%Y - %m - %d %H : %M : %S") logging.info("%s IP = %s %s" % (today, ip, res)) def main (): set_logging_format() ip_list_path = './hostip.txt' # IP_QUEUE = insert_ip_queue(ip_list_path) # IP_LIST = IP_list(ip_list_path) threads = [] THREAD_NUM = 2000 user_iput = input('please input modren:(ip|net) ') if user_iput == 'net': IP_LIST = IP_list(ip_list_path) IP_L = IP_LIST if user_iput == 'ip': IP_QUEUE = insert_ip_queue(ip_list_path) IP_L = IP_QUEUE for i in range (THREAD_NUM): t = threading.Thread(target = scan_IP, args = (IP_L,)) threads.append(t) for i in range (THREAD_NUM): threads[i].start() for i in range (THREAD_NUM): threads[i].join() if __name__ == '__main__': main() ```
local
Aug. 3, 2023, 3:53 p.m.
Share documents
Collection documents
Last
Next
Scan wechat
Copy link
Scan your mobile phone to share
Copy link
关于 LocalNetwork
LocalNetwork
是由mrdoc开源
LocalNetwork.cn
修改的在线文档系统,作为个人和小型团队的云笔记、文档和知识库管理工具。
如果此文档给你或你的团队带来了帮助,欢迎支持作者持续投入精力更新和维护!内网文摘 & LocalNetwork
>>>主页
logo
logo
Download markdown file
share
link
type
password
Update password