Skip to content

Instantly share code, notes, and snippets.

@flxxyz
Created January 6, 2018 11:37
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 flxxyz/915d5f140b22fa8f5cda065b7b57e4e2 to your computer and use it in GitHub Desktop.
Save flxxyz/915d5f140b22fa8f5cda065b7b57e4e2 to your computer and use it in GitHub Desktop.
nmap插件扫描本地ip段内在线的机器
#encodeing:utf8
import os
try:
import netifaces
import nmap
except ImportError:
try:
command_to_execute = "pip install netifaces python-nmap || easy_install netifaces python-nmap"
os.system(command_to_execute)
except OSError:
print "Can NOT install netifaces and python-nmap, Aborted!"
sys.exit(1)
import netifaces
import nmap
nm = nmap.PortScanner()
def routingGateway():
L = []
routingNicName = netifaces.gateways()['default'][netifaces.AF_INET][1]
for interface in netifaces.interfaces():
if interface == routingNicName:
routingNicMacAddr = netifaces.ifaddresses(interface)[netifaces.AF_LINK][0]['addr']
try:
routingIPAddr = netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr']
L.extend([routingIPAddr, routingNicMacAddr])
except KeyError:
pass
return L
def main():
route = routingGateway()
ip = route[0]
mac = route[1]
display_format = '%-20s %-10s'
print display_format % ('You ip address:', ip)
print display_format % ('You ip address:', mac)
print
ip_getway = ip.split('.')
ip_prefix = '.'.join(ip_getway[:-1])
ip_prefix += '.0/24'
nm.scan(hosts=ip_prefix, arguments='-n -sP')
hosts_list = [(x, nm[x].state(), nm[x]['addresses']) for x in nm.all_hosts()]
for host, state, addr in hosts_list:
if host == ip:
continue
mac = ''
if addr.has_key('mac'):
mac = addr['mac']
print "%s:%s (%s)" % (host, state, mac)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment