Python 编写扫描存活主机工具

发布于 2020-04-01  457 次阅读


0x01 代码

import platform
import os
import threading
import time
import sys
 
def my_os():
    return platform.system()
#判断当前系统
def ping_ip(ip):
    if my_os() == 'Windows':
        p_w = 'n'
    elif my_os() == 'Linux':
        p_w = 'c'
    else:
        print('不支持此操作系统')
        sys.exit()
    output = os.popen('ping -%s 1 %s'%(p_w,ip)).readlines()
    for w in output:
        if str(w).upper().find('TTL')>=0:
            print(f"[+] {ip}")        
#构造扫描ip段
def ping_all(ip):
    all_ip = (ip.split('.')[:-1])
    for i in range(256):
        add = ('.'.join(all_ip)+'.'+str(i))
        threading._start_new_thread(ping_ip,(add,))
        time.sleep(0.01)
 
if __name__ == '__main__':
    if len(sys.argv) == 2:
        ip = sys.argv[1]
    else:
        print("Tips:python3 check_up.py <ip>")
    try:
        ping_all(ip)
    except:
        pass

0x02 运行结果


我不懂世界到底在热闹些什么