第8章:网络

痴心易碎 提交于 2019-11-27 08:14:22

1.列出网络上所有活跃的主机

1).使用Python判断主机是否活跃

import subprocess
import threading

def is_reacheable(ip):
    if subprocess.call(["ping", "-c", "10", ip]):
        print("{0} is alive".format(ip))
    else:
        print("{0} is unreacheable".format(ip))

def main():
    with open('ips.txt') as f:
        lines = f.readlines()
        threads = []
        for line in lines:
            thr = threading.Thread(target=is_reacheable, args=(line,))
            thr.start()
            threads.append(thr)

        for thr in threads:
            thr.join()

if __name__ == '__main__':
    main()

2).使用生产者消费者模型减少线程的数量

import subprocess
import threading
from Queue import Queue
from Queue import Empty

def call_ping(ip):
    if subprocess.call(["ping", "-c", "10", ip]):
        print("{0} is alive".format(ip))
    else:
        print("{0} is unreacheable".format(ip))

def is_reacheable(q):
    try:
        while True:
            ip = q.get_nowait()
            call_ping(ip)
    except Empty:
        pass

def main():
    q = Queue()
    with open('ips.txt') as f:
        for line in f:
            q.put(line)

        threads = []
        for i in range(10):
            thr = threading.Thread(target=is_reacheable, args=(q,))
            thr.start()
            threads.append(thr)

        for thr in threads:
            thr.join()

if __name__ == '__main__':
    main()

 

2.端口扫描

1).使用Python编写端口扫描器

使用简单的socket接口编写一个端口扫描器
from socket import *

def conn_scan(host, port):
    conn = socket(AF_INET, SOCK_STREAM)
    try:
        conn.connect((host, port))
        print(host, port, ' is available')
    except Exception as e:
        print(host, port, ' is not available')
    finally:
        conn.close()

def main():
    host = "192.168.147.135"
    for port in range(3000,4000):
        conn_scan(host, port)

if __name__ == '__main__':
    main()

 

2).使用nmap扫描端口

3).使用python-nmap进行端口扫描

 

3.使用IPy进行IP地址管理

    IPy模块是一个处理IP地址的模块

    pip install ipy

 

4.使用dnspython解析DNS

    dnspython是Python实现的一个DNS工具集

    pip install dnspython

 

5.网络嗅探器Scapy

1).Scapy简介与安装

2).Scapy的基本使用

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!