目的介绍:
用python脚本对nginx的access_log日志分析,获取到每个IP的访问次数,将IP和访问次数构建一个字典,最后对字典排序输入。
#!/usr/bin/env python #-*-coding:utf8-*- """ 分析Nginx等Web应用访问IP信息 并将其访问数量从大到小排序 """ ip_list=[] #定义空列表 所有的访问IP放置在该列表中 ip_count={}#定义一个空字典 将IP和访问的次数放置到该字典中 with open("/home/admin/output/nginx/logs/portal_ssl.access.log","r") as ngfile: #打开日志文件 for line in ngfile: #print(line.split()) 调试信息 ip_list.append(line.split()[0]) #用split分割字符串获取到列表line.split()[0] 每行的IP地址 for count in set(ip_list): #将ip_list去重 ip_number=ip_list.count(count) #统计每个IP出现的次数 ip_count.setdefault(count,ip_number) #将IP以及数量更新到字典 ip_count_new=sorted(ip_count.items(),key=lambda d:d[1],reverse=True) #使用lambda函数 对其重新排序构建新字典 for eachip in ip_count_new: print(eachip)
执行结果
('101.95.165.34', 721745) ('223.71.139.21', 103207) ('36.7.107.196', 58529) ('61.160.72.166', 22186) ('180.169.146.185', 17156) ('157.122.117.223', 16967) ('121.9.226.46', 16359) ('123.59.41.33', 13726) ('116.228.3.66', 12713) ('61.145.213.42', 11231) ('113.12.72.4', 9702) ('101.200.183.32', 8540) ('61.233.16.210', 8332) ('61.50.110.250', 8041) ('124.65.161.34', 7900) ('113.108.156.188', 6732) ('112.26.112.78', 6725) ('123.15.51.94', 6598) ('117.122.213.136', 6555) ('111.196.211.180', 6362) ('111.198.46.221', 4702) ('112.83.122.218', 4675) ('116.231.210.229', 4606) ('223.223.194.233', 4462) ('14.205.4.101', 4320) ('113.107.140.151', 4244) ('123.52.40.48', 3828) ('111.202.248.113', 3823) ('211.159.166.70', 3702) ('113.12.192.108', 3637) ('121.204.79.241', 3601) ('183.234.34.68', 3594) ('114.80.240.184', 3492) ('218.28.79.37', 3205) ('122.4.210.224', 3074) ('218.65.149.119', 3071) ('58.20.235.226', 2985) ('219.145.168.126', 2904) ('124.207.44.178', 2764) ('111.196.213.150', 2552) ('222.168.162.56', 2521) ('123.125.37.233', 2283) ('59.45.63.94', 2160) ('61.136.137.160', 2113) ('124.74.136.198', 2005) ('221.203.58.178', 1906) ('111.200.254.2', 1828) ('112.91.178.78', 1805) ('118.192.104.55', 1803) ('61.48.52.129', 1789) ('111.200.62.30', 1693) ('58.20.26.91', 1633) ('140.207.96.146', 1586) ('123.125.35.2', 1472) ('219.143.154.94', 1460) ('14.215.236.194', 1428) ('112.65.141.22', 1344) ('101.81.60.150', 1306) ('120.234.46.254', 1290) ('111.196.213.37', 1231) ('27.19.41.133', 1184) ('116.228.49.222', 1155) ('10.201.18.123', 1139) ('101.231.75.105', 1121) ('101.230.195.113', 1114) ('171.43.231.234', 1091) ('61.50.123.66', 1065) ('10.201.18.45', 1030) ('116.228.205.130', 1001) ('114.244.6.61', 988) ('124.74.109.254', 945) ('1.192.32.223', 942) ('183.129.211.50', 870) ('116.228.237.158', 862) ('59.48.159.234', 859) ('1.192.31.40', 846) ('1.192.166.78', 843) ('60.191.88.70', 842) ('124.126.131.137', 834) ('183.194.42.234', 822) ('58.132.173.59', 790) ('61.178.41.75', 789) ('180.169.182.89', 779) ('116.25.97.47', 768) ('123.117.69.220', 688) ('119.98.225.97', 687) ('1.192.30.42', 686) ('123.124.213.210', 682) ('10.201.18.160', 681) ('140.206.187.134', 669) ('59.45.63.206', 654) ('218.94.33.218', 654) ('106.39.49.98', 622) ('10.201.18.185', 609) ('171.43.231.155', 605) ('10.201.18.240', 599) ('118.187.58.208', 598) ('10.201.18.215', 579) ('27.19.42.4', 575) ('118.114.245.36', 527) ('1.192.35.119', 522) ('10.201.18.171', 522) ('125.70.231.10', 516) ('10.201.18.72', 515) ('10.201.18.222', 515) ('47.93.126.158', 511) ('1.192.32.232', 505) ('10.201.18.12', 486) ('116.226.79.175', 457) ('10.201.18.40', 453) ('101.254.164.62', 446) ('101.81.62.119', 444) ('114.243.69.81', 440) ('114.243.223.13', 427) ('115.233.220.90', 427) ('113.71.42.174', 427) ('111.204.157.66', 426) ('10.201.18.50', 422) ('10.201.18.245', 410) ('121.60.102.120', 409) ('124.126.148.92', 399) ('10.201.18.100', 387) ('10.201.18.60', 382) ('119.57.32.125', 380) ('180.129.225.138', 380) ('218.75.73.58', 379) ('119.98.226.76', 377) ('221.234.191.97', 370) ('183.28.29.112', 358) ('45.121.65.104', 357) ('60.191.34.20', 356) ('10.201.18.205', 356) ('222.95.165.247', 351) ('112.74.21.128', 350) ('203.187.160.2', 349) ('10.201.18.189', 347) ('10.201.18.57', 345) ('1.192.32.177', 345) ('10.201.18.169', 343) ('118.144.141.146', 343) ('114.242.249.57', 342) ('10.201.18.254', 342) ('10.201.18.75', 331) ('221.233.242.118', 330) ('120.27.140.153', 328) ('10.201.18.90', 321) ('124.126.155.146', 316) ('61.148.62.130', 308) ('121.31.60.150', 306) ('113.235.118.179', 306) ('10.201.18.13', 304) ('116.25.97.207', 298) ('10.201.18.208', 298) ('211.103.133.218', 295) ('10.201.18.41', 293) ('36.106.23.206', 289) ('27.19.42.145', 289) ('10.201.18.56', 286) ('123.118.118.180', 284) ('117.89.72.12', 282) ('10.201.18.139', 272) ('101.81.31.160', 269) ('10.201.18.176', 269) ('10.201.18.73', 267) ('117.36.118.190', 261) ('61.135.20.98', 257) ('112.236.254.232', 255) ('125.84.180.71', 254) ('61.152.70.10', 250) ('117.36.119.106', 249) ('10.201.18.3', 247) ('10.201.18.78', 245) ('10.201.18.59', 241) ('10.201.18.110', 240) ('218.61.142.209', 237) ('116.226.72.5', 235) ('101.229.72.137', 227) ('124.205.209.30', 227) ('223.223.197.143', 226) ('10.201.18.34', 225) ('106.38.42.194', 224) ('10.201.18.32', 223) ('10.201.18.26', 222) ('10.201.18.43', 222) ('10.201.18.91', 218) ('183.0.96.48', 213) ('47.92.72.46', 210) ('1.180.145.150', 207) ('116.228.33.250', 199) ('10.201.18.201', 199) ('10.201.18.52', 198) ('10.201.18.170', 198) ('10.201.18.184', 194) ('116.231.173.238', 192) ('58.242.107.146', 192) ('116.7.221.236', 191) ('10.201.18.64', 189) ('36.57.142.50', 186) ('10.201.18.217', 181) ('183.192.72.252', 181) ('119.98.227.50', 175) ('1.192.33.78', 174) ('10.201.18.204', 173) ('10.201.18.152', 172) ('10.201.18.87', 172) ('61.51.59.48', 172) ('10.201.18.211', 171) ('123.113.105.27', 167) ('36.102.208.109', 166) ('10.201.18.2', 166) ('59.63.248.231', 166) ('10.201.18.53', 165) ('10.201.18.174', 165) ('183.11.202.56', 165) ('10.201.18.24', 164) ('10.201.18.140', 164) ('222.137.180.18', 160) ('10.201.18.118', 158) ('10.201.18.236', 157) ('10.201.18.187', 154) ('36.102.228.89', 154) ('10.201.18.80', 152) ('10.201.18.243', 148) ('10.201.18.16', 148) ('122.240.161.233', 148) ('10.201.18.108', 147) ('120.197.36.221', 147) ('1.192.165.46', 147) ('220.248.77.174', 146) ('180.173.39.1', 146) ('10.201.18.121', 145) ('10.201.18.142', 145) ('222.187.218.162', 144) ('10.201.18.28', 144) ('114.243.213.110', 143) ('10.201.18.77', 142) ('10.201.18.7', 142) ('10.201.18.199', 140) ('10.201.18.23', 138) ('10.201.18.82', 136) ('111.202.154.136', 135) ('10.201.18.65', 134) ('114.85.107.18', 134) ('116.231.60.136', 132) ('116.231.159.125', 128) ('10.201.18.214', 128) ('10.201.18.241', 126) ('10.201.18.69', 126) ('113.132.9.215', 126) ('10.201.18.156', 124) ('10.201.18.9', 124) ('10.201.18.119', 121) ('10.201.18.8', 121) ('10.201.18.157', 120) ('114.222.199.23', 116) ('10.201.18.235', 115) ('111.200.206.11', 113) ('10.1.4.159', 113) ('180.110.215.147', 113) ('10.201.18.249', 112) ('10.201.18.55', 111) ('10.201.18.209', 111) ('10.201.18.62', 110) ('10.201.18.181', 110) ('140.207.74.42', 109) ('49.74.195.95', 108) ('10.201.18.31', 107) ('10.201.18.18', 107) ('27.211.176.117', 107) ('49.76.220.252', 106) ('10.201.18.135', 106) ('10.201.18.36', 104) ('10.201.18.20', 104) ('101.231.82.214', 104) ('183.194.73.154', 104) ('10.201.18.188', 103) ('10.201.18.129', 103) ('10.201.18.84', 102) ('10.201.18.130', 102) ('10.201.18.106', 101) ('10.201.18.178', 99) ('1.27.88.231', 98) ('10.201.18.158', 98) ('10.201.18.29', 98) ('10.201.18.138', 98) ('10.201.18.103', 98) ('110.228.190.109', 97) ('10.201.18.172', 97) ('10.201.18.179', 97) ('114.242.17.105', 96) ('10.201.18.83', 96) ('10.201.18.42', 96) ('10.201.18.150', 95) ('222.94.77.132', 95) ('10.201.18.111', 94) ('10.201.18.114', 92) ('10.201.18.120', 92) ('10.201.18.19', 91) ('10.201.18.11', 90) ('218.17.160.144', 89) ('10.201.18.148', 85) ('10.201.18.67', 84) ('117.36.116.123', 84) ('10.201.18.89', 83) ('10.201.18.193', 83) ('1.119.141.197', 82) ('58.214.189.58', 82) ('10.201.18.37', 81) ('1.27.89.66', 81) ('10.201.60.10', 80) ('1.68.60.180', 80) ('221.216.72.184', 79) ('118.194.48.15', 78) ('124.16.0.10', 78) ('10.201.18.98', 77) ('10.201.18.14', 77) ('220.184.201.244', 74) ('10.201.18.162', 74) ('183.129.232.234', 74) ('10.201.18.132', 73) ('171.43.231.22', 72) ('115.215.127.57', 72) ('1.68.57.80', 71) ('180.109.15.92', 71) ('10.201.18.86', 71) ('10.201.18.81', 71) ('122.245.53.87', 71) ('58.246.31.196', 70) ('114.241.208.174', 69) ('125.84.176.239', 69) ('10.201.18.168', 69) ('10.201.18.136', 69) ('219.143.155.51', 68) ('183.14.134.8', 67) ('10.201.18.166', 67) ('10.201.18.180', 67) ('10.201.18.115', 66) ('10.1.4.2', 66) ('10.201.18.202', 66) ('10.201.18.44', 65) ('125.47.71.158', 63) ('10.201.18.137', 63) ('10.201.18.71', 61) ('116.236.162.245', 60) ('10.201.18.237', 59) ('183.14.132.113', 59) ('10.201.18.192', 59) ('222.212.28.6', 57) ('10.201.18.246', 57) ('10.201.18.70', 57) ('183.39.87.104', 56) ('10.201.18.216', 56) ('10.201.18.48', 56) ('10.201.18.51', 55) ('10.201.18.54', 55) ('120.204.17.67', 55) ('42.156.250.242', 54) ('10.201.18.247', 54) ('10.201.18.5', 54) ('42.156.251.194', 54) ('10.201.18.218', 54) ('183.15.178.141', 53) ('10.201.18.194', 53) ('10.201.18.195', 53) ('10.201.18.191', 53) ('221.222.136.153', 53) ('10.201.18.94', 52) ('140.206.88.75', 52) ('115.213.89.152', 51) ('10.201.18.17', 51) ('10.201.18.122', 50) ('10.201.18.104', 50) ('106.6.180.255', 50) ('10.201.18.159', 49) ('10.201.18.173', 49) ('10.201.18.15', 48) ('10.201.18.210', 48) ('10.201.18.196', 48) ('10.201.18.153', 47) ('183.131.243.186', 46) ('58.101.84.178', 46) ('10.201.18.175', 44) ('10.201.18.252', 44) ('180.173.36.89', 43) ('10.201.18.239', 42) ('10.201.18.58', 42) ('10.201.18.101', 42) ('221.217.169.143', 42) ('101.226.65.107', 41) ('10.201.18.190', 41) ('10.201.18.10', 40) ('10.201.18.131', 40) ('1.180.206.200', 40) ('10.201.18.141', 39) ('10.201.18.124', 39) ('101.199.112.50', 39) ('10.201.18.177', 39) ('10.201.18.49', 39) ('114.89.36.237', 38) ('220.181.132.200', 38) ('10.201.18.117', 37) ('125.120.159.247', 37) ('10.201.18.68', 37) ('101.226.33.239', 37) ('10.201.18.97', 37) ('1.192.166.180', 37) ('10.201.18.161', 36) ('180.163.2.88', 36) ('42.156.251.208', 36) ('42.156.251.201', 36) ('42.156.251.205', 36) ('123.234.117.193', 36) ('10.201.18.233', 35) ('10.201.18.21', 35) ('180.172.29.189', 35) ('111.127.235.192', 35) ('10.201.18.155', 34) ('10.201.18.88', 34) ('116.232.148.71', 34) ('117.36.118.41', 34) ('120.204.17.72', 33) ('120.204.17.69', 33) ('1.31.216.160', 33) ('10.201.18.227', 33) ('10.201.18.102', 33) ('115.213.92.56', 33) ('10.201.18.112', 32) ('123.116.254.203', 31) ('120.194.143.70', 31) ('10.201.18.39', 30) ('10.201.18.76', 30) ('10.201.18.213', 30) ('1.27.89.245', 30) ('10.201.18.134', 30) ('10.201.18.79', 29) ('10.201.18.238', 29) ('10.201.18.6', 29) ('153.119.232.229', 29) ('175.174.58.158', 29) ('10.201.18.203', 29) ('10.201.18.27', 28) ('10.201.18.165', 28) ('10.201.18.163', 28) ('10.201.18.85', 28) ('10.201.18.244', 27) ('10.201.18.186', 27) ('58.214.190.174', 27) ('106.38.108.42', 27) ('1.30.6.181', 26) ('45.55.94.56', 26) ('10.201.18.251', 26) ('1.192.165.144', 25) ('10.201.18.133', 25) ('10.201.18.234', 25) ('10.201.18.143', 25) ('10.201.18.46', 25) ('101.226.33.228', 24) ('180.212.38.114', 24) ('1.27.91.191', 24) ('113.71.43.244', 23) ('140.205.201.7', 23) ('10.201.18.35', 23) ('10.201.18.38', 23) ('183.128.137.149', 23) ('223.104.212.241', 23) ('210.12.65.130', 23) ('10.201.18.147', 23) ('140.205.201.18', 23) ('121.57.132.35', 23) ('61.129.8.179', 23) ('117.136.3.69', 23) ('113.129.102.224', 23) ('106.120.162.93', 23) ('10.201.18.113', 22) ('10.201.18.223', 22) ('101.226.66.177', 22) ('101.226.33.240', 22) ('101.226.66.193', 22) ('61.129.6.151', 22) ('10.201.18.151', 21) ('183.13.4.178', 21) ('114.249.118.53', 21) ('117.136.90.154', 21) ('112.65.189.237', 21) ('103.19.65.210', 20) ('101.199.112.55', 20) ('10.201.18.250', 20) ('10.201.18.154', 19) ('10.201.18.30', 19) ('113.129.218.56', 19) ('101.199.108.56', 19) ('101.199.108.52', 19) ('101.199.108.118', 19) ('220.181.132.197', 19) ('220.181.132.192', 19) ('10.201.18.109', 19) ('42.156.250.243', 18) ('101.199.108.58', 18) ('101.199.108.57', 18) ('10.201.18.95', 18) ('101.199.112.51', 18) ('42.156.251.198', 18) ('42.156.251.195', 18) ('42.156.251.191', 18) ('42.156.251.206', 18) ('42.156.251.189', 18) ('10.201.18.74', 16) ('123.116.248.239', 16) ('10.201.18.182', 15) ('220.181.132.179', 15) ('106.120.160.109', 14) ('10.201.18.93', 14) ('10.201.18.33', 14) ('120.204.17.73', 13) ('101.226.33.220', 13) ('10.201.18.144', 13) ('10.201.18.63', 13) ('60.191.124.180', 13) ('61.151.218.118', 12) ('10.201.18.25', 12) ('101.226.66.192', 12) ('123.59.42.78', 12) ('10.201.18.149', 12) ('101.226.33.221', 11) ('101.226.33.223', 11) ('101.226.33.225', 11) ('101.226.89.121', 11) ('58.247.212.98', 11) ('120.204.17.68', 11) ('101.226.33.203', 11) ('101.226.33.201', 11) ('101.226.225.85', 11) ('112.65.193.15', 11) ('101.226.66.179', 11) ('106.120.161.68', 11) ('101.226.33.217', 11) ('61.151.179.83', 11) ('101.226.89.119', 11) ('140.206.161.160', 11) ('140.207.128.193', 11) ('120.204.17.46', 11) ('61.151.179.27', 11) ('61.151.178.179', 11) ('61.151.178.172', 11) ('61.151.178.174', 11) ('101.226.64.177', 11) ('10.201.18.126', 10) ('10.201.18.107', 10) ('10.201.18.164', 9) ('101.226.66.181', 9) ('101.226.33.206', 8) ('101.226.33.200', 8) ('1.192.33.58', 8) ('10.201.18.61', 7) ('61.140.204.101', 7) ('101.226.89.116', 7) ('113.108.67.24', 7) ('120.55.90.223', 7) ('10.201.18.230', 6) ('114.248.140.250', 6) ('120.194.143.51', 6) ('120.194.143.67', 6) ('10.201.18.116', 5) ('10.201.18.127', 5) ('10.201.18.128', 5) ('120.194.143.50', 5) ('120.92.32.247', 5) ('10.201.18.145', 5) ('120.92.32.214', 5) ('183.14.28.8', 4) ('10.201.18.125', 4) ('1.192.167.1', 4) ('14.23.190.150', 4) ('182.84.158.151', 4) ('221.222.138.165', 3) ('14.17.3.65', 3) ('106.6.143.192', 3) ('120.92.11.92', 3) ('103.47.137.226', 3) ('66.102.6.153', 3) ('182.101.144.240', 3) ('183.57.53.177', 3) ('1.192.166.242', 2) ('106.11.227.165', 2) ('106.11.225.111', 2) ('58.217.192.54', 2) ('42.84.43.145', 2) ('119.103.186.218', 2) ('1.192.164.38', 2) ('180.212.200.51', 2) ('114.248.159.244', 2) ('10.201.18.105', 2) ('182.101.145.162', 2) ('106.11.226.231', 2) ('114.242.249.182', 2) ('49.65.251.215', 1) ('101.226.33.227', 1) ('101.226.89.122', 1) ('182.101.145.34', 1) ('117.136.88.204', 1) ('120.92.11.176', 1) ('106.37.89.170', 1) ('58.222.50.121', 1) ('106.11.226.56', 1) ('106.11.225.95', 1) ('106.11.227.105', 1) ('123.125.67.228', 1) ('123.125.67.164', 1) ('169.54.233.121', 1) ('42.84.32.170', 1) ('111.198.74.118', 1) ('61.129.6.148', 1) ('106.7.90.101', 1) ('183.11.203.85', 1) ('221.0.21.182', 1) ('106.7.91.111', 1) ('113.109.76.24', 1) ('120.92.11.56', 1) ('120.92.11.205', 1) ('64.233.172.184', 1) ('64.233.172.182', 1) ('120.92.11.91', 1) ('66.102.6.152', 1) ('116.25.99.211', 1) ('61.151.178.170', 1) ('106.7.91.69', 1) ('120.92.11.27', 1)