-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcheckNginxVersion.py
executable file
·86 lines (66 loc) · 2.61 KB
/
checkNginxVersion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##
# Tart Monitoring
#
# Copyright (c) 2013, Tart İnternet Teknolojileri Ticaret AŞ
#
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby
# granted, provided that the above copyright notice and this permission notice appear in all copies.
#
# The software is provided "as is" and the author disclaims all warranties with regard to the software including all
# implied warranties of merchantability and fitness. In no event shall the author be liable for any special, direct,
# indirect, or consequential damages or any damages whatsoever resulting from loss of use, data or profits, whether
# in an action of contract, negligence or other tortious action, arising out of or in connection with the use or
# performance of this software.
##
import sys
import urllib2
import re
def main():
if len(sys.argv) < 2:
raise Exception('No argument.')
if len(sys.argv) > 2:
raise Exception('Too many arguments.')
try:
serverVersion = getServerVersion(sys.argv[1])
except Exception as exception:
print('Cannot get the server version: ' + str(exception))
sys.exit(3)
majorVersion = serverVersion.rsplit('.', 1)[0]
try:
publishedVersion = getPublishedVersion('http://nginx.org/en/download.html', majorVersion)
except Exception as exception:
print('Cannot get the published version: ' + str(exception))
sys.exit(3)
print('Server version: ' + serverVersion + ' published version: ' + publishedVersion)
if serverVersion != publishedVersion:
sys.exit(1)
sys.exit(0)
def getServerVersion(address):
response = None
try:
response = urllib2.urlopen(address)
except urllib2.HTTPError as error:
response = error
server = selectHeader(response.info().headers, 'Server')
if not server:
raise Exception('No server header.')
split = server.strip().split('/')
if split[0] != 'nginx':
raise Exception('Server is not Nginx.')
if len(split) < 2:
raise Exception('No version on the server header.')
return split[1]
def selectHeader(headers, key):
for header in headers:
if header.startswith(key + ':'):
return header[(len(key) + 1):]
def getPublishedVersion(address, majorVersion):
response = urllib2.urlopen(address)
versions = re.findall('nginx-' + majorVersion + '.[0-99]', response.read())
if not versions:
raise Exception('Page does not include the major version.')
return versions[0].split('-')[1]
if __name__ == '__main__':
main()