summaryrefslogtreecommitdiffstats
path: root/modules/people/src/arping.py
blob: eea176e0f861f63add21bec7b3d03bf5c9f5ceab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/python

import logging 
log = logging.getLogger('arpingy')
logging.disable(logging.WARNING)

import os,sys
try:
  if (os.geteuid() != 0):
    raise Exception('no root permissions')
  from scapy.all import * #might throws "no such module"

  def arpingy(iprange="10.42.1.0/24",iface='eth0'):
    log.debug("pinging "+ str(iprange))
    """Arping function takes IP Address or Network, returns nested mac/ip list"""
    try:
      conf.verb=1
      ans,unans=arping(iprange,iface=iface,timeout=1,retry=3)

      collection = []
      for snd, rcv in ans:
        result = rcv.sprintf(r"%ARP.psrc% %Ether.src%").split()
        log.debug(result)
        return result # take just the first arp reply
    except Exception as e:
      print ("something went wrong while arpinging " + str(e))
    return []

except Exception as e:
  log.error("Cannot load arping functions!" + str(e))
  def arpingy(iprange='',iface=''):
    raise Exception ('arping not available')


if __name__ =='__main__':
  logging.basicConfig(level=logging.DEBUG)
  arpingy(sys.argv[1],sys.argv[2])