chrisluiz16

Untitled

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