Paste Code
Paste Blends
Paste Images
#!/usr/bin/env python                                                 
##Copyright (C) 2010 Louis Taylor
## _ ___ _____
##| | | _ \_ _|
##| |__| _/ | |
##|____|_| |_| *programus optimus est*
##This program is free software: you can redistribute it and/or modify
##it under the terms of the GNU General Public License as published by
##the Free Software Foundation, either version 3 of the License, or
##(at your option) any later version.
##
##This program is distributed in the hope that it will be useful,
##but WITHOUT ANY WARRANTY; without even the implied warranty of
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
##GNU General Public License for more details.
##
##You should have received a copy of the GNU General Public License
##along with this program. If not, see http://www.gnu.org/licenses/
import os, sys, re

class connected:
'''
class for looking at the connected machines on the network.
Can give the same output available on the network router

Must be run under root, this is necessary because of nmap
depends on:
* nmap
* python 2.6
* ping
'''
def __init__(self):
self.ipregex = re.compile('\(.*?\)')
self.hostregex = re.compile('Host\.*is up')

self.cmd = 'nmap -sP 192.168.1.1-255'

self.peopleConnected = []
self.ipConnected = []
self.MACConnected = []

self.database = []
self.current = 0

if not os.getuid() == 0:
#raise Exception, 'must be run as root' #uncomment and use this if you want
#it to be run as part of a larger
#program
print 'must be run as root'
sys.exit(1)
else:
pass

def output(self, cmd):
'''outputs the text from cmd'''
self.outputtext=os.popen(cmd).read().split('\n')
return self.outputtext

def refresh(self):
'''refreshes the list of machines connected to the network
You must call this function every time you expect the values to change
and want to read these changes'''

ishost = False
hasMAC = True

for line in self.output(self.cmd):
splitline = line.split(' ')
if splitline[0] == 'Host':
if not hasMAC:
self.MACConnected += ['unknown']

self.peopleConnected += [splitline[1].replace('.home', '')]
ipAddress = self.ipregex.search(line)
self.ipConnected += [ipAddress.group().replace('(', '').replace(')', '')]

ishost = True
hasMAC = False

#check if the line begins with 'MAC' and is after a host line
elif ishost and splitline[0] == 'MAC':
brackets = self.ipregex.search(line)
brackets = brackets.group()
line = line.replace(brackets, '').replace('MAC Address: ', '')
self.MACConnected += [line]
hasMAC = True
ishost = False
current = -1
self.database = []

for name in self.peopleConnected:
current += 1
self.database.append(name)
self.database.append([self.ipConnected[current], self.MACConnected[current]])

def ip(self, name):
'''returns the ip address of the given computer name
returns 000.000.000.000 if none found e.g. computer not connected'''
try:
index = self.database.index(name)+1 #get the index of the item
#add 1 to get to the next index
except ValueError:
return '000.000.000.000'
return self.database[index][0]

def mac(self, name):
'''returns the MAC address of the given computer name
returns 00:00:00:00:00:00 if none found e.g. computer not connected'''
try:
index = self.database.index(name)+1 #get the index of the item
#add 1 to get to the next index
except ValueError:
return '00:00:00:00:00:00'
return self.database[index][1]

def os(self, name):
'''will try to find the name and version of the os that the ip is running.
v. slow, so run in multithreaded or prepare for a freeze
* name can be an ip address or the bssid

Warning! this function does not work, do not use'''

cmd = 'sudo nmap -O %s' % name
output = self.output(cmd)

def up(self, ip):
'''returns True if the ip address replies to a ping request'''
cmd = 'ping -q -c 1 %s' % ip
output = os.popen(cmd).read()
if 'ping: unknown host' in output:
#trys to see if the ip address entered is invalid.
return False
elif '1 received' in output:
#try to see if the computer has receved any packets.
return True
else:
#return false because there is nothing else to do if the
#output is in a different format than usual.
return False

def names(self):
'''returns the names of the computers connected to the network'''
return self.peopleConnected

if __name__ == '__main__':
'''small self test/example of use'''
people = connected()
people.refresh()
for name in people.names():
print name
print '... MAC:', people.mac(name)
print '... ip:', people.ip(name)
  1. #!/usr/bin/env python                                                
  2. ##Copyright (C) 2010 Louis Taylor                                    
  3. ## _    ___ _____                                                    
  4. ##| |  | _ \_   _|                                                    
  5. ##| |__|  _/ | |                                                      
  6. ##|____|_|   |_| *programus optimus est*                              
  7. ##This program is free software: you can redistribute it and/or modify
  8. ##it under the terms of the GNU General Public License as published by
  9. ##the Free Software Foundation, either version 3 of the License, or  
  10. ##(at your option) any later version.                                
  11. ##                                                                    
  12. ##This program is distributed in the hope that it will be useful,    
  13. ##but WITHOUT ANY WARRANTY; without even the implied warranty of      
  14. ##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      
  15. ##GNU General Public License for more details.                        
  16. ##                                                                    
  17. ##You should have received a copy of the GNU General Public License  
  18. ##along with this program.  If not, see http://www.gnu.org/licenses/  
  19. import os, sys, re
  20.  
  21. class connected:
  22.     '''
  23.    class for looking at the connected machines on the network.
  24.    Can give the same output available on the network router
  25.    
  26.    Must be run under root, this is necessary because of nmap
  27.    depends on:
  28.    * nmap
  29.    * python 2.6
  30.    * ping
  31.    '''
  32.     def __init__(self):
  33.         self.ipregex = re.compile('\(.*?\)')
  34.         self.hostregex = re.compile('Host\.*is up')
  35.        
  36.         self.cmd = 'nmap -sP 192.168.1.1-255'
  37.        
  38.         self.peopleConnected = []
  39.         self.ipConnected = []
  40.         self.MACConnected = []
  41.        
  42.         self.database = []
  43.         self.current = 0
  44.        
  45.         if not os.getuid() == 0:
  46.             #raise Exception, 'must be run as root' #uncomment and use this if you want
  47.                                                     #it to be run as part of a larger
  48.                                                     #program
  49.             print 'must be run as root'
  50.             sys.exit(1)
  51.         else:
  52.             pass
  53.  
  54.     def output(self, cmd):
  55.         '''outputs the text from cmd'''
  56.         self.outputtext=os.popen(cmd).read().split('\n')
  57.         return self.outputtext
  58.    
  59.     def refresh(self):
  60.         '''refreshes the list of machines connected to the network
  61.        You must call this function every time you expect the values to change
  62.        and want to read these changes'''
  63.        
  64.         ishost = False
  65.         hasMAC = True
  66.        
  67.         for line in self.output(self.cmd):
  68.             splitline = line.split(' ')
  69.             if splitline[0] == 'Host':
  70.                 if not hasMAC:
  71.                     self.MACConnected += ['unknown']
  72.                    
  73.                 self.peopleConnected += [splitline[1].replace('.home', '')]
  74.                 ipAddress = self.ipregex.search(line)
  75.                 self.ipConnected += [ipAddress.group().replace('(', '').replace(')', '')]
  76.                
  77.                 ishost = True
  78.                 hasMAC = False
  79.                
  80.             #check if the line begins with 'MAC' and is after a host line
  81.             elif ishost and splitline[0] == 'MAC':
  82.                 brackets = self.ipregex.search(line)
  83.                 brackets = brackets.group()
  84.                 line = line.replace(brackets, '').replace('MAC Address: ', '')
  85.                 self.MACConnected += [line]
  86.                 hasMAC = True
  87.                 ishost = False
  88.         current = -1
  89.         self.database = []
  90.        
  91.         for name in self.peopleConnected:
  92.             current += 1
  93.             self.database.append(name)
  94.             self.database.append([self.ipConnected[current], self.MACConnected[current]])
  95.        
  96.     def ip(self, name):
  97.         '''returns the ip address of the given computer name
  98.        returns 000.000.000.000 if none found e.g. computer not connected'''
  99.         try:
  100.             index = self.database.index(name)+1 #get the index of the item
  101.                                                 #add 1 to get to the next index
  102.         except ValueError:
  103.             return '000.000.000.000'
  104.         return self.database[index][0]
  105.    
  106.     def mac(self, name):
  107.         '''returns the MAC address of the given computer name
  108.        returns 00:00:00:00:00:00 if none found e.g. computer not connected'''
  109.         try:
  110.             index = self.database.index(name)+1 #get the index of the item
  111.                                                 #add 1 to get to the next index
  112.         except ValueError:
  113.             return '00:00:00:00:00:00'
  114.         return self.database[index][1]
  115.    
  116.     def os(self, name):
  117.         '''will try to find the name and version of the os that the ip is running.
  118.        v. slow, so run in multithreaded or prepare for a freeze
  119.        * name can be an ip address or the bssid
  120.        
  121.        Warning! this function does not work, do not use'''
  122.        
  123.         cmd = 'sudo nmap -O %s' % name
  124.         output = self.output(cmd)
  125.        
  126.     def up(self, ip):
  127.         '''returns True if the ip address replies to a ping request'''
  128.         cmd = 'ping -q -c 1 %s' % ip
  129.         output = os.popen(cmd).read()
  130.         if 'ping: unknown host' in output:
  131.             #trys to see if the ip address entered is invalid.
  132.             return False
  133.         elif '1 received' in output:
  134.             #try to see if the computer has receved any packets.
  135.             return True
  136.         else:
  137.             #return false because there is nothing else to do if the
  138.             #output is in a different format than usual.
  139.             return False
  140.        
  141.     def names(self):
  142.         '''returns the names of the computers connected to the network'''
  143.         return self.peopleConnected
  144.    
  145. if __name__ == '__main__':
  146.     '''small self test/example of use'''
  147.     people = connected()
  148.     people.refresh()
  149.     for name in people.names():
  150.         print name
  151.         print '... MAC:', people.mac(name)
  152.         print '... ip:', people.ip(name)
go to heaven