Last active
May 13, 2022 16:54
-
-
Save Mr-xn/cf81d841d71adc5b8c9b92f9de56fe2b to your computer and use it in GitHub Desktop.
scan alive host by python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from scapy.all import * | |
import ipaddress | |
import argparse | |
# from https://npfs06.top/ | |
# need install scapy: | |
# pip isntall scapy | |
def icmp_request(ip_dst): | |
global a # call a | |
pocket = Ether()/IP(dst=ip_dst)/ICMP(type=8)/b'Hello' | |
req = srp1(pocket, timeout=2, verbose=False) | |
if req: | |
print('[+]', ip_dst, ' Host is up') | |
a += 1 | |
def icmp_speed(network): | |
threads = [] | |
length = len(network) | |
for ip in network: | |
t = threading.Thread(target=icmp_request, args=(str(ip),)) | |
threads.append(t) | |
for i in range(length): | |
threads[i].start() | |
for i in range(length): | |
threads[i].join() | |
def main(): | |
parser = argparse.ArgumentParser(description='imformation') | |
# type是要传入的参数的数据类型 help是该参数的提示信息 | |
parser.add_argument('network', type=str, help='eg: 192.168.1.0/24') | |
args = parser.parse_args() | |
print("----------------scan begin(%s)----------------"%time.ctime()) | |
network = list(ipaddress.ip_network(args.network)) | |
icmp_speed(network) | |
print("----------------scan end(%s)------------------"%time.ctime()) | |
print("Scan finished: ", len(network), "IP addresses (", a, "hosts up) scanned") | |
if __name__ == '__main__': | |
a = 0 | |
main() | |
# exp: | |
# python3 scan-host-alive.py 192.168.1.1/24 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment