Guest User

Untitled

a guest
Feb 17th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. # Importing modules
  2. import paramiko
  3. import datetime
  4. import sys
  5.  
  6. def validate_ip(s):
  7. a = s.split('.')
  8. if len(a) != 4:
  9. return False
  10. for x in a:
  11. if not x.isdigit():
  12. return False
  13. i = int(x)
  14. if i < 0 or i > 255:
  15. return False
  16. return True
  17.  
  18. # input no of IP address and IP address from user
  19.  
  20. print '*************************************'
  21. print ' BLOCK IP ADDRESSES VIA LIST '
  22. print '*************************************'
  23.  
  24. while 1:
  25. try:
  26. path_of_file = raw_input('Enter filename with complete path:') #call for path of file to block IP addresses
  27. break
  28. except:
  29. print 'You have entered incorrect path or filename, please try again'
  30. IP=[] #it will store valid IP addresses to block
  31. with open(path_of_file) as f:
  32. content = f.readlines()
  33. content = [x.strip() for x in content]
  34. for value in content:
  35. if validate_ip(value) is True:
  36. IP.append(value)
  37. else:
  38. pass
  39.  
  40. #log the IP address just blocked
  41. blacklist_file = open("blacklisted_IP.txt", "ab+")
  42. for ip_elem in IP:
  43. blacklist_file.write(str(ip_elem) + ' was added on ' + str(datetime.date.today()) + '\n')
  44. blacklist_file.close()
  45.  
  46. # add string "_blacklist" to each IP address
  47. string = '_SIEM_blacklist'
  48. IP = [x + string for x in IP]
  49.  
  50. # setting parameters like host IP, username, passwd
  51. HOST = "a.b.c.d" #Your IP address here
  52. USER = "User" # Your Username here
  53. PASS = "####" #Your password here
  54.  
  55. client1 = paramiko.SSHClient()
  56. # Add missing client key
  57. client1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  58. # connect to switch
  59. client1.connect(HOST, username=USER, password=PASS)
  60. print "SSH connection to firewall %s established" % HOST
  61.  
  62. # Create address object
  63. for bl_ip_name in IP:
  64. bl_ip = bl_ip_name.strip('_SIEM_blacklist')
  65. command1 = 'config firewall address \n edit "' + str(bl_ip_name) + '_' + str(datetime.date.today()) + '"\nset subnet ' + str(bl_ip) + ' 255.255.255.255' + '\nnext\nend'
  66. stdin, stdout, stderr = client1.exec_command(command1)
  67. print ('firewall Object %s created..!!' %bl_ip)
  68.  
  69. #Add addresses to address groups
  70. for bl_ip_name_02 in IP:
  71. command2 = 'config firewall addrgrp \nedit "Blacklisted_ip" \nappend member ' + str(bl_ip_name_02) + '_' + str(datetime.date.today()) + '\nend'
  72. stdin, stdout, stderr = client1.exec_command(command2)
  73. # stdout = stdout.readlines()
  74. bl_ip_02 = bl_ip_name_02.strip('_SIEM_blacklist')
  75. print ('IP address %s has been added to blacklisted address group' %bl_ip_02)
  76. # print (str(stdout))
  77. client1.close()
  78. print "Logged out of firewall %s" % HOST
  79. print 'Task completed'
  80. raw_input('PRESS ANY KEY TO CONTINUE...... .')
Add Comment
Please, Sign In to add comment