手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Python批量扫描服务器80端口状态

[日期:2016-09-29] 来源:Linux社区  作者:XuHoo [字体: ]

Python写了一个简陋的端口扫描脚本,其简单的逻辑如下:

1. python DetectHostPort.py iplist.txt(存放着需要扫描的IP地址列表的文本,每行一个地址)

2. 输入扫描端口、扫描时间和扫描间隔。

3. 输出扫描信息。

下面贴上源码,欢迎拍砖。

#!/usr/bin/env python

import sys
import time
import socket


def getaddresslist(addr):
    """
    getaddresslist(addr) -> IP address file

    IP address read from the file.
    :param addr: IP file
    :return: Scan ip address list, or error message.
    """
    address = []
    try:
        with open(addr, "r") as iplist:
            line = iplist.readlines()
            for item in line:
                address.append(item.strip("\n"))
        return address

    except (IOError, IndexError), e:
        return str(e)


def scan(iplist, port=80):
    """
    scan() -> getaddresslist()

    getaddresslist() function returns the IP address of the list.
    :param iplist: getaddresslist() Function return value.
    :param port: Need to scan the port.
    :return: None
    """
    if not isinstance(iplist, list):
        sys.exit("Function getaddresslist() return error message: %s" % iplist)
    # start_time = time.time()

    for addr in iplist:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(1)
        host = (addr, int(port))
        try:
            s.connect(host)
            print "Host %s:%s connection success." % (host[0], host[1])
        except Exception, e:
            print "Host %s:%s connection failure: %s" % (host[0], host[1], e)

        s.close()


if __name__ == '__main__':

    addrs = sys.argv[1]
    ScanPort = input("Enter the scan port: ")
    Total = input("Enter the scan time <minutes>: ")
    Interval = input("Enter the scanning interval <minutes>: ")
EndTime
= time.time() + Total * 60 while time.time() < EndTime: scan(getaddresslist(addrs), ScanPort) time.sleep(Interval * 60) continue else: print "\nwhile end."

运行时只能扫描一个端口,但是可以对代码进行修改,扩展为扫描多个端口。

 

Ubuntu 14.04安装Python 3.3.5  http://www.linuxidc.com/Linux/2014-05/101481.htm

 

CentOS上源码安装Python3.4  http://www.linuxidc.com/Linux/2015-01/111870.htm

 

《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

 

《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htm

 

Python脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

 

在Ubuntu下用Python搭建桌面算法交易研究环境 http://www.linuxidc.com/Linux/2013-11/92534.htm

 

Python 语言的发展简史 http://www.linuxidc.com/Linux/2014-09/107206.htm

 

Python 的详细介绍请点这里
Python 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2016-09/135621.htm

 

linux
相关资讯       Python扫描端口状态 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删���其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款