扫描并过滤本地网络上的设备。
项目描述
hello lan
直观的端口扫描!此模块只是几个封装nmap的功能,并作为命令行工具公开。
基本上,我创建这个是因为我经常需要在本地网络上扫描树莓派,而且总是忘记正确的命令。然后我需要在Python中执行一些自动扫描,所以我只是将所有内容封装在几个Python函数中,并带有命令行界面。
安装
# mac
brew install nmap
# linux
apt-get install nmap
pip install hellolan
用法
# by default, we scan ports 21-443
hellolan scan
# there are some presets too
hellolan ssh # --port=22
hellolan web # --port=80,443
示例输出
$ hellolan ssh
hostname ip ports
----------------- ------------- -------
192.168.1.127 [22]
node-b827e9j315hf 192.168.1.236 [22]
-
Took 3.3 seconds
如果您使用sudo
运行,您还可以看到MAC地址和厂商。
$ sudo hellolan ssh --watch
hostname ip ports mac vendor
---------------------- ------------- ------- ----------------- ---------------------------
raspberrypi 192.168.1.242 [22] DC:A6:32:C4:F4:A3 Raspberry Pi Trading
raspberrypi 192.168.1.65 [22] DC:A6:32:A9:F0:82 Raspberry Pi Trading
192.168.1.66 [22] 52:D4:F7:18:5F:74
192.168.1.67 [22] 00:1A:62:03:F3:FF Data Robotics, Incorporated
Scan 1 finished at Tue Jan 19 23:08:55 2021. took 10.2s. Found 4 hosts.
还有更多
# you can filter the results too
hellolan ssh node- # match a partial hostname
hellolan ssh 'host*' --ignore='badhost-*' # matching glob
hellolan ssh 192.168.1.84 # match an exact ip
# NOTE: lmk if you'd prefer regex over fnmatch
# you can change the net hosts to scan.
# by default it's the local router subnet --net=192.168.1.0/24
# so it's not exclusively for lan ...!! helloevbody!
hellolan scan --net=scanme.nmap.org
hellolan scan --net=198.116.0-255.1-127
hellolan scan --net=216.163.128.20/20
# you can also poll localhost:
# here I'm checking what I have for jupyter lab instances running.
# they autoincrement ports as 8888 + i. I'm assuming I don't have
# more than 7.
hellolan scan --net=localhost --port=8888-8893
# Outputs:
# hostname ip ports
# ---------- --------- ------------
# localhost 127.0.0.1 [8888, 8889]
# -
# Took 11.4 seconds
您可以使用更多可解析的格式打印它
$ hellolan ssh -ip
# 192.168.1.127
# 192.168.1.236
$ hellolan ssh -ip -json
# ["192.168.1.127", "192.168.1.236"]
$ hellolan ssh -json
# [{"hostname": "", "ip": "192.168.1.127", "ports": [22]}, {"hostname": "raspberrypi", "ip": "192.168.1.236", "ports": [22]}]
现在您可以使用该命令直接ssh到设备!!!
它的工作原理 - 它将根据'@'符号分割,轮询主机名为'abc'的设备,并用找到的IP地址替换'abc'。
hellolan ssh- user@abc
例如
mbp $ hellolan ssh- user@abc
#
# Multiple hosts found:
# hostname ip ports
# -- -------- ------------- -------
# 0 abcdejkl 192.168.1.127 [22]
# 1 abcdefgh 192.168.1.236 [22]
#
# Which host to use? [0]: >> 1
# Using host: abcdefgh
#
# ---------------------
# Starting SSH Session: $ ssh sonyc@192.168.1.236
# ---------------------
#
# Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
# permitted by applicable law.
# Last login: Wed Apr 15 18:40:32 2020 from 192.168.1.214
user@abcdefgh:~ $ hostname
abcdefgh
user@abcdefgh:~ $ exit
logout
Connection to 192.168.1.236 closed.
#
# -------------------
# Ended SSH Session. (ssh sonyc@192.168.1.236)
# -------------------
#
mbp $
它也可以导入!
import hellolan
for d in hellolan.scan(port='21-22'):
print('{hostname} ({ip}) - {ports}'.format(**d))
注意
-
这使用默认的
nmap.PortScanner().scan(host, ports=ports)
设置。我不确定我是否足够了解来选择更好的默认设置,但可能有方法可以加快速度。 -
更新:在调整设置后,确实有方法可以加快速度。
- 我使版本检查成为可选的。nmap.PortScanner.scan默认情况下会进行版本检查(
-sV
)。我移除了它,现在您可以使用--services
重新启用它。我没有看到任何丢失的信息,并且查询现在要快得多。 - 如果您尝试加速
--services
,可以指定版本强度(--intensity 0-9
),使用0
可以使用更少的检查(更快),而使用9
则使用最多(最全面)。我发现当使用--services
与--intensity 0
时,运行时间显著减少。 - 您可以指定使用多少个顶级端口(默认为
--top 200
),越少越快(nmap 默认为1000
)。
- 我使版本检查成为可选的。nmap.PortScanner.scan默认情况下会进行版本检查(
项目详情
关闭
hellolan-0.0.12.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | efca0a222f0113a041eef0e32f7620b2d146034da0b8ec181f237104aaa7f660 |
|
MD5 | 1609e9d6d2af90c8a4aaf432b34eb7f5 |
|
BLAKE2b-256 | 7b135e93a0b72c9db7ebd898fd8dc046edc9ebe7ead874809e0b697d32503f8a |