Created
August 1, 2014 13:52
-
-
Save mengzhuo/ae81e8e7a43897ded48c to your computer and use it in GitHub Desktop.
Check STARTTLS status of given domain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
The MIT License (MIT) | |
Copyright (c) <2014> <Meng Zhou [email protected]> | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
""" | |
import datetime | |
import csv | |
import requests | |
from lxml import html | |
API = 'http://www.checktls.com/perl/TestReceiver.pl' | |
def check_tls(domain): | |
"""Check given domain STARTTLS status | |
:params domain: This domain you wish to check | |
""" | |
resp = requests.post(API, | |
{'LEVEL':'4', | |
'EMAIL':'admin@%s' % domain, | |
'TIMEOUT':"60"}) | |
parsed = html.fromstring(resp.text) | |
result_list = [(lambda x: x and x[:-1].isdigit() and int(x[:-1]))(x.text) | |
for x in parsed.get_element_by_id('Matrix')\ | |
.find_class('totalrow')[0].findall('td')] | |
return dict(zip(['can_use', 'tls_adv', 'cert_ok', 'tls_neg'], result_list[4:])) | |
def check_multi_domains(domains_file): | |
log = open("%s.log" % datetime.datetime.now(), 'wb+') | |
writer = csv.writer(log) | |
with open(domains_file) as f: | |
for domain in f.readlines(): | |
domain = domain.strip() | |
print 'checking.....'+domain | |
ch = check_tls(domain) | |
if ch['cert_ok'] == 100: | |
result = u'通过' | |
elif ch['cert_ok'] < 100 and ch['tls_neg'] > 0: | |
result = u'证书不安全' | |
else: | |
result = u'无证书' | |
print "%s %s" % (domain, result) | |
writer.writerow((domain.encode('utf8'), result.encode('utf8'))) | |
log.flush() | |
log.close() | |
if __name__ == '__main__': | |
import sys | |
method = sys.argv[1] | |
if method == '-d': | |
print check_tls(sys.argv[2]) | |
elif method == '-f': | |
check_multi_domains(sys.argv[2]) | |
else: | |
print "-d domain or -f file check list" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment