运维脚本:python实现批量IP端口扫描

运维脚本:python实现批量IP端口扫描

专注网络运维,只发实用干货

扫描二维码关注公众

    今天不想更新,发一篇存货,分享一小段python代码给大家,能实现简单的批量端口扫描,废话不多说,先上代码:

===========================================================

# -*- coding: utf-8 -*-

import socket

import time

import xlrd

import threading

 

hostfile = xlrd.open_workbook('port_scan.xlsx')    #调用扫描目标表格文件

table_list = hostfile.sheet_by_index(0)                  #读取表格中的第一个sheet页

hang = table_list.nrows

lie = table_list.ncols

print ("Total hosts: %d" % (hang-1))

 

 

def scan(deviceID,hostname,hostip,PORT):

    try:

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        s.settimeout(5)    #超时时间

        s.connect((hostip,PORT))

        print ('%d_%s %s %s OPEN' % (deviceID,hostname,hostip,PORT))

    except:

        print ('%d_%s %s %s CLOSE' % (deviceID,hostname,hostip,PORT))

    s.close()

 

for deviceID in range(1, hang):

    hostname = table_list.cell(deviceID, 0).value

    hostip = table_list.cell(deviceID, 1).value

    PORT = int(table_list.cell(deviceID, 2).value)

    threading.Thread(target=scan,args=(deviceID,hostname,hostip,PORT)).start()

    time.sleep(0.2)    #间隔时间可以自己调

time.sleep(10)

input('Enter Quit:')

===========================================================

 

运行前准备工作:

1,制定扫描目标,保存为 .xlsx 的execl表格,格式如下:

业务名称 IP地址 端口号
邮箱 1.1.1.1 25
WEB 2.2.2.2 80
远程桌面 3.3.3.3 3389

2,安装python 3

3,需要安装的外部库:xlrd(表格读取)

     安装命令:pip install xlrd

 

亲测有效,需要扫描的主机和端口在表格中填好就行,适用于多个不同主机和端口的批量扫描,不适用单个IP的1-65535端口扫描。

 

 

专注网络运维,只发实用干货

扫描二维码关注公众

posted on 2018-09-04 10:48  zhanghz_fantasy  阅读(1040)  评论(0编辑  收藏  举报

导航