指定端口号,多线程扫描局域网内IP地址
小白第一次发博客,请各路大神不要喷,有错的地方还请不吝啬指教,谢谢。。。。。。。
因为注释基本上已经说清楚啦,在这里就不多说什么啦,知识不够怕误人子弟
# -*- coding:utf-8 -*-
import socket
import time
import threading def Scan(IpAddr,port,thread_num,i): number = 254 / thread_num #f每个线程将要处理的IP数
# 通过变量i来判断线程处理的哪个IP段,然后循环处理
for p in range(number * (i + 1),number * i,-1):
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # 通过 IPV4协议 数据流 形式创建socket对象
addr = IpAddr + '.' + str(p) # IP地址
try:
s.connect((addr,int(port))) # 通过对端口进行连接来判断的开关
s.close()
print(addr) # 输出开该端口的IP地址
except socket.error:
pass
#如果该端口没有打开,是会抛出[10061] Connection refused等异常 def main(IpAddr,port,thread_num):
threads = [] # 线程列表
number = 254 / thread_num # 每个线程的处理的IP数
number_end = 254 % thread_num # 剩下的IP地址
num = range(thread_num) # 将创建的所有线程加入线程列表中
for i in num:
t = threading.Thread(target=Scan,args=(IpAddr,port,thread_num,i))
threads.append(t) # 启动所有线程
for i in num:
threads[i].start() # 挂起程序,等待所有线程结束
for i in num:
threads[i].join() # 这里是为了当在函数Scan传入的thread_num线程数不能整除254时,
# 会导致后面的IP地址漏扫描
if number_end > 0:
for i in range(255,number * thread_num):
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
addr_e = IpAddr + '.' + str(i)
try:
s.connect((addr_e,int(port)))
s.close()
print(addr_e)
except socket.error:
pass 测试代码:
if __name__=='__main__':
IpAddr = raw_input('please input the IP segement:')
port = raw_input('please input the port:')
threadNum = raw_input('please input the number of threads:')
start_time = time.time()
main(IpAddr,port,int(threadNum))
end_time = time.time()
print('total time is : %s' %(end_time-start_time))
效果:
please input the IP segement:172.16.135
please input the port:3389
please input the number of threads:20
172.16.135.12
172.16.135.11
172.16.135.48
172.16.135.60
....
指定端口号,多线程扫描局域网内IP地址的更多相关文章
- 使用Python检测局域网内IP地址使用情况
来源:https://www.cnblogs.com/donlin-zhang/p/6812675.html 在测试环境搭建的过程中,经常需要给服务器分配静态IP地址,由于不清楚当前局域网内部哪些IP ...
- 局域网内ping [局域网内ip地址]命令详解
一.工作过程 主机A向主机B发送一个ICMP请求报文[类型字段为8,代码字段为0],若收到ICMP回复报 文[类型字段为0,代码字段为0]则说明主机B处于活动状态:若超时未收到回复,则可能是 因为(1 ...
- 在windows命令行批量ping局域网内IP
参考了博客园Alfred Zhao的文章<Windows平台ping测试局域网所有在用IP> 在cmd命令行运行如下命令即可: ,,) -w .%i | find "回复&quo ...
- Snmp协议应用-扫描局域网内打印机
.h2cls { background: #6fa833 none repeat scroll 0 0 !important; color: #fff; font-family: "微软雅黑 ...
- MongoDB官方下载安装设置配置文件指定端口号
1.)下载 官网(https://www.mongodb.com/)右上角try free 进入下载中心,下载指定版本 ZIP和MSI随便 如果浏览器下载的慢,可以直接使用下载地址,然后迅雷下 操作 ...
- Linux 开放指定端口号
1.开启防火墙 [root@localhost opt]# systemctl start firewalld 2.查看当前防火墙状态 [root@localhost opt]# systemctl ...
- wifi智能插座 一键扫描局域网内插座Ip及其它信息 Python源代码API
转载请保留原地址. http://www.cnblogs.com/lsqandzy 最近买了几个智能插座玩,插座安装在家里,连接好wifi,不管你人在哪里,通过手机,一键开启或关闭插座电源,想象一下, ...
- 扫描局域网内的ip和主机名
1. 目的 今天发现我配置的一台电脑ip被人占用了,所以准备写个程序扫描一下局域网内所有正在使用的ip和主机名 2. 实现--直接上代码 import time import threading im ...
- 局域网内IP冲突怎么办
对于在Internet和Intranet网络上,使用TCP/IP协议时每台主机必须具有独立的IP地址,有了IP地址的主机才能与网络上的其它主机进行通讯.但IP地址冲突会造成网络客户不能正常工作,只 ...
随机推荐
- bootstrap之 Badge 角标
添加 .am-badge class 到 <div> 或者 <span> 元素. 默认样式 <span class="am-badge"> ...
- HDU-1049
Description An inch worm is at the bottom of a well n inches deep. It has enough energy to climb u i ...
- inflate, findViewById与setContentView的区别与联系
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV ...
- Windows 下 Apache HTTP Server 安装、配置以及与 Tomcat 的整合(附图)
如果您能点开这篇文章,说明您已对熟悉Apache HTTP Server(下文用Apache简称)配置的重要性已很清楚了,本文不在赘述,直接介入正题,请往下阅读: 为便于阅读,列出文章目录: 一.Ap ...
- 播放器音乐源之天天动听API
搜索歌曲API:http://so.ard.iyyin.com/s/song_with_out?q={0}&page={1}&size={2} {0}=需要搜索的歌曲或歌手 {1}=查 ...
- PLSQL实例(游标)
在PLSQL块中执行SQL语句 A. 数据定义DDL: create,drop,truncate,不能直接执行,truncate执行时只做数据删除,不写日起,不维护索引 在PLSQL块中执行字符串 ...
- API例子:用Python驱动Firefox采集网页数据
1,引言 本文讲解怎样用Python驱动Firefox浏览器写一个简易的网页数据采集器.开源Python即时网络爬虫项目将与Scrapy(基于twisted的异步网络框架)集成,所以本例将使用Scra ...
- 转:C4项目中验证用户登录一个特性就搞定
转:C4项目中验证用户登录一个特性就搞定 在开发过程中,需要用户登陆才能访问指定的页面这种功能,微软已经提供了这个特性. // 摘要: // 表示一个特性,该特性用于限制调用 ...
- Oracle EBS-SQL (BOM-14):检查工艺路线明细.sql
select msi.segment1 装配件编码, msi.description ...
- logstash grok正则调试
logstash 正则调试: nginx 配置: log_format main '$remote_addr [$time_local] "$request" '; logstas ...