Advertisement
Guest User

Untitled

a guest
Sep 17th, 2010
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. #########################################################################
  2. ## Update a file with a list of mac adress from (wireless routers)      #
  3. ##                                      #
  4. ##  (working on ubuntu with superuser right)            #
  5. ##                                  #
  6. ## command: 'sudo python macfisher.py'                  #
  7. #########################################################################
  8.  
  9. ## set import
  10. import os,string
  11. from __future__ import with_statement
  12.  
  13. ## set command line and filename output destination
  14. command="iwlist wlan0 scan" # define command : scann wireless network
  15. filename = "test" # define output file
  16. command_line= command + ">" + filename # define command line
  17.  
  18. ### open MAC adress archive file
  19. print '\n'+ "Open old archive file ..."
  20. archivemacfilename="maclist" # define archive name
  21. try:
  22.     # with was imported by from __future__ import with_statement
  23.     with open(archivemacfilename,"r") as archivemacfile # open archive file. This syntax open and close file automatically
  24.         stringarchivemac=archivemacfile.read() # to read the content
  25. except:
  26.     stringarchivemac=""
  27.  
  28. ## run command line
  29. print "Make a scan ..."
  30. val = os.system(command_line) # run it !
  31.  
  32. ## file processing
  33. with open(filename, 'r') as FILE: # open command output file. This syntax open and close file automatically
  34. cp_new=0 # define the count for new adress
  35. for line in FILE: # read the file
  36.     if line.find('Cell')>-1: # if the line contain a MAC adress
  37.         C = line.split()[-1] # split the line
  38.         if stringarchivemac.find(C)==-1:  # if the adress is new
  39.             stringarchivemac=stringarchivemac+C+"\n" # add
  40.             cp_new=cp_new+1
  41. os.remove(filename) # delete file
  42.  
  43. ### update du fichier avec les nouvelles
  44. if cp_new<>0: # if new adress(es) detected
  45.     print "Archive file were updated with " + str(cp_new) + " new address(es)."    
  46.  
  47.     archivemacfilename="maclist" # open archive mac adress file
  48.     with open(archivemacfilename,"a") as archivemacfile: # to write
  49.         archivemacfile.write(stringarchivemac) # the update
  50. else:
  51.     print "No update for archive file."  
  52.  
  53. print "\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement