Advertisement
Guest User

Untitled

a guest
Mar 9th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. import sys, urllib2, urllib
  2. from xml.etree.cElementTree import tostring, ElementTree
  3.  
  4. def updateFirewall(url):
  5. # Getting the username and password from the user
  6. username = sys.argv[1]
  7. password = sys.argv[2]
  8.  
  9. # Auth and getting key
  10. values = {"type":"keygen", "user":username, "password":password}
  11.  
  12. data = urllib.urlencode(values)
  13. req = urllib2.Request(url, data)
  14. response = urllib2.urlopen(req)
  15. response_body = response.read()
  16.  
  17. key = ElementTree(response_body).find('//key/text()')
  18.  
  19. # reading the elements we need to add to the existing config
  20. with open('policy.xml', 'rt') as f:
  21. tree = ElementTree.parse(f)
  22. services = tree.find("/config/devices/entry/vsys/entry/service")
  23. security_rules = tree.find("/config/devices/entry/vsys/entry/rulebase/security")
  24. default_security_rules = tree.find("/config/devices/entry/vsys/entry/rulebase/default-security-rules")
  25.  
  26. # adding services
  27. values1 = {"type":"config", "key":key, "action":"edit", "xpath":"/config/devices/entry/vsys/entry/service", "element":services}
  28. data1 = urllib.urlencode(values1)
  29. serviceResponse = urllib2.Request(url, data1)
  30.  
  31. # adding the security rules
  32. values2 = {"type":"config", "key":key, "action":"edit", "xpath":"/config/devices/entry/vsys/entry/rulebase/security", "element":security_rules}
  33. data2 = urllib.urlencode(values2)
  34. secRulesResonse = urllib2.Request(url, data2)
  35.  
  36. # adding the default security rules
  37. values3 = {"type":"config", "key":key, "action":"edit", "xpath":"/config/devices/entry/vsys/entry/rulebase/default-security-rules", "element":default_security_rules}
  38. data3 = urllib.urlencode(values3)
  39. secRulesResonse = urllib2.Request(url, data3)
  40.  
  41.  
  42. url = "https://192.168.10.1/api/"
  43. updateFirewall(url)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement