Guest User

Untitled

a guest
Jun 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # These are the two directories I want to work with.
  4. # ip_path is the directory that store the old IP
  5. # new_ip_path is the the directory that has the update IP
  6. ip_path = '\Users\qnguyen\backup_conf'
  7. new_ip_path = 'Users\qnguyen\new_ips'
  8.  
  9. import os, re
  10.  
  11. # my_reg take all the characters in the string up to .ip
  12. my_reg = re.compile('(\w+)\.ip')
  13. # This command should give me access to the root, directories and files in new_ip_path
  14. for root,directories,files in os.walk(new_ip_path):
  15. # So I get all the name in that directory and put in in the list host_files
  16. host_files = [("%s.pl" % my_reg.match(h).group(1)) for h in files]
  17.  
  18. # for each name in host file, I want to open that file in the other path and replace the old IP with the new
  19. for f in host_files:
  20. # open the text file and read in the the line.
  21. oldIPfile = open(os.path.join(new_ip_path,f)
  22. my_newIP = oldIPfile.readline()
  23. # I don't get why there a syntax error. I'm just storing the line that I read in
  24. # Then I should open the directory that store the old IP and replace it with the new one
  25. my_string = re.comple('/^(\d+.?){4}$/')
  26. # for each line I want to find the old IP, which is in the line that contains ClientNameAlias
  27. # and replaces it with the new IP using sub()
  28. for line in open(os.path.join(ip_path,f))
  29. if "ClientNameAlias" in line:
  30. my_string.sub(my_newIP, line)
  31. print("%s 's old IP was replace." % f)
  32. else:
  33. print("%s could not be found" % f)
Add Comment
Please, Sign In to add comment