今天添加端口探测功能,主要实现方式是通过nmap模块调用,扫描1-65535端口。上一篇中已经将UP的PC机全部获取到,这里直接从已知在线的PC中进行端口扫描就可以了,会节省很多时间。

代码如下,还是非常简单的:

 import nmap
import time
import csv
import os class Port_Scan(object):
def __init__(self):
self.rootpath = 'C:/cunhuo/'
self.dirpath = 'C:/portScan/'
self.savepath = 'C:/portScan/result.csv'
self.csv_write = [] def get_ip(self, file):
ipList = []
with open(file, 'r') as f:
for ip in f.readlines()[1:]:
ipList.append(ip.strip()[:-3])
return ipList def scan(self, ip):
if not os.path.exists(self.dirpath):
os.mkdir(self.dirpath)
nm = nmap.PortScanner()
try:
nm.scan(ip, arguments='1-65535')
print '[*]Host:%s' % ip
for proto in nm[ip].all_protocols():
lport = nm[ip][proto].keys()
lport.sort()
for port in lport:
self.csv_write.append([ip, port, nm[ip][proto][port]['state']])
print 'port : %s\tstate : %s' % (port, nm[ip][proto][port]['state'])
except:
self.csv_write.append([ip, 'unknown', 'unknown'])
print 'port : unknown\tstate : unknown' def run(self):
print '[+]Scanning...'
startTime = time.time()
files = os.listdir(self.rootpath)
for file in files:
ips = self.get_ip(self.rootpath + file) for ip in ips:
self.scan(ip)
with open(self.savepath,'w') as f:
fieldnames = ['ip', 'port', 'state']
w = csv.writer(f, dialect='excel')
w.writerow(fieldnames)
w.writerows(self.csv_write)
endTime = time.time()
print '------------------------------------------------------'
print '[+]Scanning cost %ss, result have saved %s' % (endTime - startTime, self.savepath)
print '[+]Done.' if __name__ == '__main__':
myScan = Port_Scan()
myScan.run()

这里对于错误捕获的实现有点偷懒了,我们之前扫描出的存活主机可能在端口探测这个脚本执行时离线了,所以如果不加try...except判断的话会跑出keyerror的错误,说结果中的port、state不在字典中。但是这样加判断也有失偏颇...除了离线主机无法探测到,其他情况导致的也一并将port和state设置为unknown了,不知道以后的实验中会不会发生误报,到时候会进行修改调整。

扫描用时比较长,我就展示一下运行中的截图了:

result.csv,为什么中间会有空格呢,奇怪,慢慢改吧

python nmap模块 端口探测的更多相关文章

  1. python nmap模块使用进行主机探测(ICMP)

    终于审核通过了......第一次用博客,想记录自己的学习情况,分享知识. 废话不多说,第一篇blog,大牛请轻喷. 资产清点首先需要进行主机探测,将存活主机统计下来再进行进一步的指纹识别及端口探测.若 ...

  2. python(nmap模块、多线程模块)

    http://xael.org/pages/python-nmap-en.html       nmap模块 http://www.tutorialspoint.com/python/python_m ...

  3. 7.python实现高效端口扫描器之nmap模块

    对于端口扫描,使用的最多的就是nmap这个工具,不想python已经强大到,提供了nmap这个扫描端口的模块. 本片文章主要介绍nmap模块的两个常用类: PortScanner()类,实现一个nma ...

  4. 端口扫描器--利用python的nmap模块

    安装nmap模块挺麻烦的,搞了半天 不仅要安装pip install nmap 还要sudo apt install nmap 给出代码,没有设多线程,有点慢,注意端口的类型转换,搞了很久 #!/us ...

  5. 7.安装pycharm----导入Nmap模块----netcat反向shell----metasploitable靶机环境

    安装pycharm www.jetbrains.com/pycharm/ tar -zxvf p补全 ls cd p补全 ls cd bin ls ./pycharm.sh 新建项目 pythonRo ...

  6. Python脚本写端口扫描器(socket,python-nmap)

    目录 Socket模块编写 扫描给定主机是否开放了指定的端口 python-nmap模块编写 扫描给定ip或给定网段内指定端口是否开放 一个用python写的简单的端口扫描器,python环境为 3. ...

  7. python 各模块

    01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支 ...

  8. Python ftplib模块

    Python ftplib模块 官方文档:https://docs.python.org/3/library/ftplib.html?highlight=ftplib#module-ftplib 实例 ...

  9. Python(五)模块

    本章内容: 模块介绍 time & datetime random os sys json & picle hashlib XML requests ConfigParser logg ...

随机推荐

  1. do while循环

    do while循环: 语法格式: do{ 循环体 }while(循环条件); 执行流程: 先执行循环体,然后判断条件,当条件为true时,则继续执行循环体,然后再判断条件... 一直到循环条件为fa ...

  2. gulp优化hexo方法

    gulp通过对站点使用的静态资源进行压缩,来优化网站的访问速度. 首先安装gulp以及所需要的模块: npm install gulp -g npm install gulp-htmlclean gu ...

  3. pat03-树2. List Leaves (25)

    03-树2. List Leaves (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a t ...

  4. Springboot - 集成 JPA

    1.什么是 JPA? JPA就是Java Persistence API的意思,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. 2. JPA 具有什么优 ...

  5. springboot 之 使用jetty web容器

    springboot 中默认的web容器是tomcat. 在maven 的pom 文件中加入如下依赖,便可使用tomcat 容器. <dependency> <groupId> ...

  6. K&R C风格函数

    前些天在看getopt源码, 一种前所未见的函数定义方法 int getopt(nargc, nargv, ostr) int nargc; char * const nargv[]; const c ...

  7. 【linux】基础操作命令

    1.查找文件或者程序位置 root@ROUTER:~# which is hostapd/sbin/hostapd

  8. ie8点击焦点有虚线框兼容问题

    a标签的: 方法一:在IE下是使用html属性:hideFoucs,在HTML标签中加上hidefocus=”true” 属性即可,但这个属性是IE私有的,Firefox是不认的. <a hre ...

  9. indexOf.substr,substring,charAt的区别

    var a = "asdfghjkl" alert(a.substr(1, 3));        //  从下标为1开始,往右数3个长度的数, 显示 sdf; alert(a.s ...

  10. 4、加载:Loading

    /* ---html----*/ <ion-content> <button (click)="manual()">手动关闭</button> ...