本文目录导读:

Windows PowerShell 脚本(推荐)
# scan_lan_shares.ps1
# 扫描本地网络中的共享文件夹
# 设置扫描参数
$subnet = "192.168.1" # 根据实际网络修改
$startRange = 1
$endRange = 254
# 清空历史结果
$results = @()
Write-Host "开始扫描局域网共享..." -ForegroundColor Green
# 扫描IP范围
for ($i = $startRange; $i -le $endRange; $i++) {
$ip = "$subnet.$i"
$isOnline = Test-Connection -ComputerName $ip -Count 1 -Quiet
if ($isOnline) {
Write-Host "正在检查 $ip..." -ForegroundColor Yellow
try {
# 获取共享信息
$shares = Get-WmiObject -Class Win32_Share -ComputerName $ip -ErrorAction Stop
if ($shares) {
Write-Host " 发现共享:" -ForegroundColor Cyan
foreach ($share in $shares) {
Write-Host " $($share.Name) -> $($share.Path)"
$results += [PSCustomObject]@{
IP = $ip
ShareName = $share.Name
Path = $share.Path
Description = $share.Description
}
}
}
}
catch {
Write-Host " 无共享或拒绝访问" -ForegroundColor DarkGray
}
}
}
# 导出结果
if ($results.Count -gt 0) {
$results | Export-Csv -Path "shares_report.csv" -NoTypeInformation
Write-Host "`n发现 $($results.Count) 个共享,结果已保存到 shares_report.csv" -ForegroundColor Green
} else {
Write-Host "`n未发现任何共享" -ForegroundColor Red
}
使用方法:
- 以管理员身份运行 PowerShell
- 修改
$subnet为你的网段 - 执行:
.\scan_lan_shares.ps1
Linux Bash 脚本
#!/bin/bash
# scan_lan_shares.sh
# 配置参数
SUBNET="192.168.1"
START=1
END=254
OUTPUT_FILE="shares_report.txt"
echo "开始扫描局域网共享..."
echo "扫描结果 - $(date)" > $OUTPUT_FILE
echo "=========================" >> $OUTPUT_FILE
# 扫描函数
check_share() {
local ip=$1
# 检查主机是否在线
if ping -c 1 -W 1 $ip &> /dev/null; then
echo "检查 $ip..."
# 使用 smbclient 扫描 Samba 共享
shares=$(smbclient -L //$ip -N -N 2>/dev/null | grep "^\\\\" | awk '{print $1}' | tr -d '\\')
if [ -n "$shares" ]; then
echo " 发现共享:"
echo "$shares" | while read share; do
echo " $share"
done
# 写入结果文件
echo "IP: $ip" >> $OUTPUT_FILE
echo "$shares" >> $OUTPUT_FILE
echo "---" >> $OUTPUT_FILE
fi
fi
}
# 并行扫描(使用后台进程)
echo "正在扫描..."
for i in $(seq $START $END); do
ip="$SUBNET.$i"
check_share $ip &
done
# 等待所有后台进程完成
wait
echo "扫描完成!结果已保存到 $OUTPUT_FILE"
安装依赖:
sudo apt-get install smbclient # Debian/Ubuntu sudo yum install samba-client # CentOS/RHEL
Windows 批处理脚本 (简单版)
@echo off
:: scan_shares.bat
setlocal enabledelayedexpansion
echo ================================
echo 局域网共享扫描工具
echo ================================
:: 配置网络
set SUBNET=192.168.1
:: 扫描范围
for /l %%a in (1,1,254) do (
set IP=%SUBNET%.%%a
echo 检查 !IP!...
:: 使用 net view 查看共享
net view \\!IP! 2>nul | findstr /r /c:"^\\" >nul
if !errorlevel! equ 0 (
echo 发现共享在 !IP!:
net view \\!IP! 2>nul
echo.
echo !IP! >> shares_list.txt
)
)
echo 扫描完成!
echo 结果已保存到 shares_list.txt
pause
Python 脚本 (跨平台)
#!/usr/bin/env python3
# scan_shares.py
import socket
import subprocess
import sys
import threading
from queue import Queue
# 配置
SUBNET = "192.168.1"
START = 1
END = 254
TIMEOUT = 1
def check_host(ip):
"""检查主机是否在线"""
try:
socket.create_connection((ip, 445), timeout=TIMEOUT)
return True
except:
return False
def scan_shares(ip, results):
"""扫描单个IP的共享"""
try:
if sys.platform == "win32":
# Windows 使用 net view
output = subprocess.run(
["net", "view", f"\\\\{ip}"],
capture_output=True,
text=True,
timeout=5
).stdout
else:
# Linux 使用 smbclient
output = subprocess.run(
["smbclient", "-L", f"//{ip}", "-N"],
capture_output=True,
text=True,
timeout=5
).stdout
if "Disk" in output or "Printer" in output:
results.append((ip, output))
print(f" 发现共享在 {ip}")
except subprocess.TimeoutExpired:
pass
except Exception as e:
pass
def main():
print("开始扫描局域网共享...")
results = []
threads = []
# 创建线程池
for i in range(START, END + 1):
ip = f"{SUBNET}.{i}"
if check_host(ip):
print(f"检查 {ip}...")
t = threading.Thread(target=scan_shares, args=(ip, results))
t.start()
threads.append(t)
# 限制并发线程数
if len(threads) >= 20:
for t in threads:
t.join()
threads = []
# 等待所有线程完成
for t in threads:
t.join()
# 输出结果
print(f"\n扫描完成!发现 {len(results)} 个共享")
for ip, shares in results:
print(f"\n{ip}:")
print(shares[:200]) # 显示前200个字符
if __name__ == "__main__":
main()
高级扫描脚本 (nmap + 参数定制)
#!/bin/bash
# advanced_scan.sh
# 安装 nmap: sudo apt-get install nmap
SUBNET="192.168.1.0/24"
OUTPUT="scan_results.xml"
echo "使用 nmap 扫描 SMB 共享..."
nmap -p 445 --open -oG - $SUBNET | awk '/445\/open/{print $2}' > online_hosts.txt
echo "发现的在线主机:"
cat online_hosts.txt
echo "扫描共享详情..."
while read ip; do
echo "检查 $ip..."
smbclient -L //$ip -N -N 2>/dev/null | grep -E "^\\|Disk|IPC|Printer" || echo " 无共享或拒绝访问"
done < online_hosts.txt
# 清理临时文件
rm online_hosts.txt
使用说明
-
权限要求:
- Windows:需要管理员权限
- Linux:可能需要 root 权限访问某些端口
-
网络配置:
- 确保你与目标在同一网段
- 可能需要关闭防火墙或添加例外规则
-
常见问题:
- 如果扫描不到任何共享,检查网络是否正常
- Windows 共享需要启用"网络发现"功能
- Linux 需要安装 Samba 客户端工具
-
安全提示:
- 仅在授权的网络中使用
- 遵守企业IT政策
- 不要用于非法目的
选择适合你系统的脚本,修改网络配置后运行即可,对于大多数Windows环境,推荐使用PowerShell脚本;Linux环境推荐Bash脚本或Python脚本。