扫描 C 类网络 Python [英] Scanning a Class C network Python

查看:57
本文介绍了扫描 C 类网络 Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何扫描 C 类 ip 范围;例如,用户通过 cmd 行向我的脚本提供:python script.py 192.168.0.0/24 (或 192.168.0.1-255)

I'm trying to figure out how can I scan a class C ip range; For example a user provide by cmd line to my script : python script.py 192.168.0.0/24 (OR 192.168.0.1-255)

假设我的脚本只执行 tcp 连接操作:

Let's figure my script does only a tcp connect action:

import socket, sys

host = sys.argv[1],65535

s = socket(AF_INET, SOCK_STREAM)

s.connect(host)

s.send("helloworld")

我真的需要做一个for x in ip range"吗?!?或者内置模块可以做到这一点?

Do i really need to do a "for x in ip range" ?!? Or a build in module can do that ?

谢谢!

推荐答案

不需要太多代码,你想用纯 python 来做

It doesn't take too much code it you want to do it in pure python

import socket, struct

def atod(a): # ascii_to_decimal
    return struct.unpack("!L",socket.inet_aton(a))[0]

def dtoa(d): # decimal_to_ascii
    return socket.inet_ntoa(struct.pack("!L", d))

net,_,mask = sys.argv[1].partition('/')
mask = int(mask)
net = atod(net)

for host in (dtoa(net+n) for n in range(0, 1<<32-mask)):
    print host

这篇关于扫描 C 类网络 Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆