chrisluiz16

Untitled

Jul 25th, 2023
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. ## access_log
  2.  
  3. access_log = "access.log"
  4. logfile = open(access_log,"r")
  5. for line in logfile:
  6. print(line)
  7. break
  8.  
  9. ##
  10.  
  11. access_log = "access.log"
  12. logfile = open(access_log,"r")
  13. for line in logfile:
  14. print(line.split()[3][1:12])
  15. break
  16.  
  17. ##
  18.  
  19. access_log = "access.log"
  20. logfile = open(access_log,"r")
  21. ipcounter = {}
  22. for line in logfile:
  23. host = line.split()[0]
  24. if host not in ipcounter:
  25. ipcounter[host] = 1
  26. else:
  27. ipcounter[host] += 1
  28.  
  29. print(ipcounter)
  30.  
  31. ##
  32.  
  33. access_log = "access.log"
  34. logfile = open(access_log,"r")
  35. ipcounter = {}
  36. for line in logfile:
  37. host = line.split()[0]
  38. if host not in ipcounter:
  39. ipcounter[host] = 1
  40. else:
  41. ipcounter[host] += 1
  42.  
  43. for ip in ipcounter:
  44. freq = ipcounter[ip]
  45. if freq >= 500 and freq <=1000:
  46. print(ip,freq)
  47.  
  48. ## Format
  49.  
  50. webserver = 'nginx'
  51. version = '1.2.7'
  52.  
  53. s = "Hi the webserver {} is running in version {}".format(webserver,version)
  54. print(s)
  55.  
  56. ##
  57.  
  58. access_log = "access.log"
  59. logfile = open(access_log,"r")
  60. ipcounter = {}
  61. for line in logfile:
  62. host = line.split()[0]
  63. if host not in ipcounter:
  64. ipcounter[host] = 1
  65. else:
  66. ipcounter[host] += 1
  67.  
  68. for ip in ipcounter:
  69. freq = ipcounter[ip]
  70. if freq >= 500 and freq <=1000:
  71. out = "{} - {}".format(ip,freq)
  72. print(out)
  73.  
  74. ##
  75.  
  76. access_log = "access.log"
  77. logfile = open(access_log,"r")
  78. ipcounter = {}
  79. for line in logfile:
  80. host = line.split()[0]
  81. if host not in ipcounter:
  82. ipcounter[host] = 1
  83. else:
  84. ipcounter[host] += 1
  85.  
  86. for ip in ipcounter:
  87. freq = ipcounter[ip]
  88. if freq >= 500 and freq <=1000:
  89. out = "{:20} - {}".format(ip,freq)
  90. print(out)
  91.  
  92. ## Number of hits per day
  93.  
  94. access_log = "access.log"
  95. logfile = open(access_log,"r")
  96. datecounter = {}
  97.  
  98. for line in logfile:
  99. date = line.split()[3][1:12]
  100. if date not in datecounter:
  101. datecounter[date] = 1
  102. else:
  103. datecounter[date] += 1
  104. print(datecounter)
  105.  
  106. ## Logpraser
  107.  
  108. import logparser
  109. access_log = "access.log"
  110. logfile = open(access_log,"r")
  111.  
  112. for line in logfile:
  113. out = logparser.parser(line)
  114. print(line)
  115. print('---------------------------')
  116. print(line.split())
  117. print('---------------------------')
  118. print(out)
  119. break
  120.  
  121. ##
  122. import logparser
  123. access_log = "access.log"
  124. logfile = open(access_log,"r")
  125. ipcounter = {}
  126.  
  127. for line in logfile:
  128. logParts = logparser.parser(line)
  129. host = logParts['host']
  130. status = logParts['status']
  131. if status == "404":
  132. if host not in ipcounter:
  133. ipcounter[host] = 1
  134. else:
  135. ipcounter[host] = ipcounter[host] + 1
  136.  
  137. for ip in ipcounter:
  138. freq = ipcounter[ip]
  139. if freq >= 50:
  140. out = "{:20} - {} ".format(ip,freq)
  141. print(out)
  142.  
  143. ## Writing it to a file
  144.  
  145. import logparser
  146. access_log = "access.log"
  147. logfile = open(access_log,"r")
  148. ipcounter = {}
  149.  
  150. for line in logfile:
  151. logParts = logparser.parser(line)
  152. host = logParts['host']
  153. status = logParts['status']
  154. if status == "404":
  155. if host not in ipcounter:
  156. ipcounter[host] = 1
  157. else:
  158. ipcounter[host] = ipcounter[host] + 1
  159.  
  160. outfile = open('ipresult.txt','w')
  161. for ip in ipcounter:
  162. freq = ipcounter[ip]
  163. if freq >= 50:
  164. out = "{:20} - {}\n".format(ip,freq)
  165. outfile.write(out)
  166. outfile.close()
  167.  
  168. ! cat ipresult.txt
  169.  
  170. ##
  171. import logparser
  172. access_log = "access.log"
  173. logfile = open(access_log,"r")
  174. ipcounter = {}
  175.  
  176. for line in logfile:
  177. logParts = logparser.parser(line)
  178. host = logParts['host']
  179. date = logParts['time'][:11]
  180. if date not in ipcounter:
  181. ipcounter[date] = {}
  182. ipcounter[date][host] = 1
  183. else:
  184. if host not in ipcounter[date]:
  185. ipcounter[date][host] = 1
  186. else:
  187. ipcounter[date][host] += 1
  188. print(ipcounter)
  189.  
  190. ##
  191. import logparser
  192. access_log = "access.log"
  193. logfile = open(access_log,"r")
  194. ipcounter = {}
  195.  
  196. for line in logfile:
  197. logParts = logparser.parser(line)
  198. host = logParts['host']
  199. date = logParts['time'][:11]
  200. if date not in ipcounter:
  201. ipcounter[date] = {}
  202. ipcounter[date][host] = 1
  203. else:
  204. if host not in ipcounter[date]:
  205. ipcounter[date][host] = 1
  206. else:
  207. ipcounter[date][host] += 1
  208. print(ipcounter['20/Dec/2020'])
  209.  
  210. ! grep "20/Dec/2020" access.log | grep "45.144.0.179" | wc -l
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
Advertisement
Add Comment
Please, Sign In to add comment