吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3384|回复: 4
收起左侧

[Python 转载] python实现端口扫描low版本

[复制链接]
aiqianqian 发表于 2019-6-18 17:41
本帖最后由 wushaominkk 于 2019-6-18 21:36 编辑

一次偶然的机会:写一个脚本 扫描主机端口 low

代码如下:
[Python] 纯文本查看 复制代码
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import socket

# 端口扫描py脚本<主机>,开始端口至末尾端口

class scan():
    
    #定义打开、关闭的端口list
    host_port_open = list()
    host_port_close = list()

    def __init__(self, host_str, start_port, end_port):
        self.host_str = host_str
        self.start_port = int(start_port)
        self.end_port = int(end_port)

    def main_head(self):
        ## 遍历 用户传入的参数 起止端口
        for tmp in range(self.start_port, self.end_port):
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            try:
                # 如果检测端口打开,则执行
                s.connect((self.host_str, tmp))
                s.shutdown(2)
                print('%s:%d is open ' % (self.host_str, tmp))
                #host_port_open.append("%s %s open" % (self.host_str, tmp))
                #print("list %s"  %host_port_open)
            except:
                # 如果检测端口关闭,则执行
                print('%s:%d is close ' % (self.host_str, tmp))
                #host_port_close.append(" %s %s close" % (self.host_str, tmp))
                #print("list %s" %host_port_close)


def main():
    # 获取用户输入的字符长度
    if len(sys.argv) == 4:
        ## 捕获异常
        try:
            host_ip = sys.argv[1]
            start_port = int(sys.argv[2])
            end_port = int(sys.argv[3])

        except Exception as ret:
            print("端口输入错误。。。。。")
            return
    else:
        print("请按照以下方式运行:")
        print("python3 xxxx.py host_ip start_port end_port")
        return

    # 打印传入的参数
    print(host_ip, start_port, end_port)
    ## 执行 scan 类,传入参数
    scaning = scan(host_ip, start_port, end_port)
    # 执行scan类 main_head 方法
    scaning.main_head()


if __name__ == "__main__":
    main() 


使用方法  python xxx.py ip 起端口 结尾端口
例如: python 脚本名称.py 192.168.1.1 20 80 扫描主机192.168.1.1 20-80端口
闲的无聊,勿喷!

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

cyhcuichao 发表于 2019-6-18 18:20
学习了谢谢楼主
天使3号 发表于 2019-6-18 18:46
skye_hell 发表于 2020-7-21 14:16
Snowz 发表于 2020-7-21 14:28
可以再加上线程池来加快速度。
上次自己写也过一个类似的,
还测试连接上后,获取数据来判断端口情况。
还要设置timeout等。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-3-29 21:27

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表