简体   繁体   English

Python脚本可自动实现子网中可用IP地址的数量

[英]Python script to automate number of available ip addresses on my subnet

Can someone help me with creating a python script that will do these things. 有人可以帮助我创建可完成这些操作的python脚本。 I am trying to learn how to automate things. 我正在尝试学习如何使事情自动化。

Run nmap quick scan and export to text file: 运行nmap快速扫描并导出到文本文件:

nmap -T4 -F 10.0.0.1/24 > nmap.txt

Cut text to list of IP's: 将文本剪切到IP列表:

cat nmap.txt | grep "Nmap scan report" | awk '{print $5}' | cut -d '.' -f 4 > ip_sub.txt

Create list of possible hosts (list from 1-255): 创建可能的主机列表(列表为1-255):

python -c 'for i in range(255): print i+1' > ip_all.txt

Sort files to prepare for diff (could have done this earlier): 排序文件以准备差异(可以早些做):

cat ip_all.txt | sort -n > ip_all_sort.txt
cat ip_sub.txt | sort -n > ip_sub_sort.txt

Create diff file in columns: 在列中创建差异文件:

diff -y ip_all_sort.txt ip_sub_sort.txt > ip_sub_all_diff.txt

Now, count how many unused IP addresses: 现在,计算有多少未使用的IP地址:

grep '<' -o ip_sub_all_diff.txt | wc -l

Start with installing nmap to python. 首先将nmap安装到python。

pip install python-nmap

usage looks like this: 用法如下所示:

import nmap

nm = nmap.PortScanner() 
nm.scan('10.0.0.1')
res = nm.all_hosts()
print res

This way you can skip the whole writing to text files part. 这样,您可以跳过整个写入文本文件的部分。 res contains your information you can use regex for the grep part, iterate over the results with your for loop. res包含您的信息,您可以将regex用于grep部分,并使用for循环遍历结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM