python

时间:2016-02-05 02:15:16

标签: python

我是一名新的Python学习者。任何人都可以帮助我使用基本脚本扫描http和https端口并在用户输入子网(如192.168.1.0/24)后返回IP地址吗?谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

前段时间,我用Python编写了一个TCP端口扫描程序。它工作得很好,很容易配置。您键入IP或Internet地址,然后输出您扫描的端口。它有50行Python 2.7。玩得开心!

#!/usr/bin/env python
# PIES v1.1
# SYZYGY-DEV333
# Simple TCP port scanner in Python
# Apache Version 2

from socket import *
import sys, time
from datetime import datetime

host = ''
max_port = 5000
min_port = 1

def scan_host(host, port, r_code = 1):
    try:
        s = socket(AF_INET, SOCK_STREAM)
        code = s.connect_ex((host, port))
        if code == 0:
            r_code = code
        s.close()
    except Exception, e:
        pass
    return r_code

try:
    host = raw_input(">> Enter Target Host Address: ")
except KeyboardInterrupt:
    print("\n\n>> User Requested An Interrupt.")
    print(">> Application Shutting Down.")
    sys.exit(1)

hostip = gethostbyname(host)
print("\n>> Host: %s IP: %s" % (host, hostip))
print(">> Scanning Started At %s...\n" % (time.strftime("%H:%M:%S")))
start_time = datetime.now()

for port in range(min_port, max_port):
    try:
        response = scan_host(host, port)
        if response == 0:
            print(">> Port %d: Open" % (port))
    except Exception, e:
        pass

stop_time = datetime.now()
total_time_duration = stop_time - start_time
print("\n>> Scanning Finsihed At %s ..." % (time.strftime("%H:%M:%S")))
print(">> Scanning Duration: %s ..." % (total_time_duration))
print(">> Have a nice day!")

在此处查看我的项目:https://github.com/SYZYGY-DEV333/PIES