Skip to content

Instantly share code, notes, and snippets.

@conanca
Created January 8, 2018 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conanca/2cb0ba21f84a3efb653e1d0935ff363d to your computer and use it in GitHub Desktop.
Save conanca/2cb0ba21f84a3efb653e1d0935ff363d to your computer and use it in GitHub Desktop.
查找局域网可用Ip的程序
# -*- coding: utf-8 -*-
# ! /usr/bin/env python
''' 查找局域网可用Ip的程序
by Gongchengdong
安装依赖:
sudo dnf install nmap
sudo pip install python-nmap
!!注意!!务必使用root权限运行。如:
sudo python freeIp.py
'''
import nmap
class FreeIpPicker:
def __init__(self, hosts):
self.hosts = hosts
self.free_ips = []
def get_free_ip(self):
nm = nmap.PortScanner()
nm.scan(hosts=self.hosts, arguments='-n -sP -PE')
print "scan finish!"
lives = [host[host.rfind('.') + 1:] for host in nm.all_hosts()]
prefix = self.hosts[:self.hosts.rfind('.') + 1]
for n in range(2, 254):
if str(n) not in lives:
self.free_ips.append(prefix + str(n))
return self.free_ips
if __name__ == '__main__':
picker = FreeIpPicker('172.18.24.2-254')
free_ip = picker.get_free_ip()
print "可用ip共计"+str(len(free_ip))+"个,enjoy it!"
print free_ip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment