通过Python写人个脚本实现自动化批量fping扫描,快速扫描B段和C段存活靶机,把结果存放在TXT文档
时间: 2024-11-25 12:22:26 浏览: 31
在Python中实现自动化批量`fping`扫描并存储结果到TXT文件,你可以借助`subprocess`模块来执行系统命令,并利用文件操作来保存结果。这里是一个简单的示例:
```python
import subprocess
import time
# 定义要扫描的目标网络范围
networks = ['192.168.0.0/24', '192.168.1.0/24'] # B段和C段示例
output_file = "scan_results.txt"
def fping_subnet(subnet):
command = ["fping", "-c", "1", subnet]
try:
result = subprocess.check_output(command, timeout=5)
with open(output_file, 'a') as file:
file.write(f"{subnet}\n{result.decode()}")
except Exception as e:
print(f"Error scanning subnet {subnet}: {e}")
# 扫描每个子网
for network in networks:
start_time = time.time()
print(f"Scanning subnet {network}...")
fping_subnet(network)
print(f"Scan of {network} completed in {time.time() - start_time} seconds.")
print("All scans finished, results saved to {}.".format(output_file))
```
这个脚本会依次对指定的两个子网进行`fping`扫描,每扫完一个子网就追加结果到`scan_results.txt`文件中。请注意,实际的`fping`路径、网络地址、以及超时时间需要根据实际情况调整。
阅读全文