From 8e184ee6ae0d90ee19bdc8ce6c3cbd525c217be6 Mon Sep 17 00:00:00 2001 From: tes Date: Thu, 11 Jan 2018 20:11:14 +0800 Subject: [PATCH 1/5] ldap --- README.md | 5 +++- ldap.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 ldap.py diff --git a/README.md b/README.md index b6d8aed..95349ad 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,9 @@ * Web扫描:  [*scan_web_banner.py*](https://github.com/honglongwei/python-scripts/blob/master/scan_web_banner.py) - * Unix时间戳转换: +* Unix时间戳转换:  [*unix_time_format.py*](https://github.com/honglongwei/python-scripts/blob/master/unix_time_format.py) +* LDAP认证: +  [*ldap.py*](https://github.com/honglongwei/python-scripts/blob/master/ldap.py) + diff --git a/ldap.py b/ldap.py new file mode 100644 index 0000000..c643aa7 --- /dev/null +++ b/ldap.py @@ -0,0 +1,87 @@ +#!usr/bin/env python +#coding: utf-8 + +import os +import sys +import ldap + +def login_ldap(username, password): + try: + # print("开始执行") + Server = "ldap://127.0.0.1:389" + baseDN = "dc=baidu,dc=com" + searchScope = ldap.SCOPE_SUBTREE + # 设置过滤属性,这里只显示cn=test的信息 + searchFilter = "sAMAccountName=" + username + # 为用户名加上域名 + username = 'baidu\\' + username + + + # None表示搜索所有属性,['cn']表示只搜索cn属性 + retrieveAttributes = None + + conn = ldap.initialize(Server) + #非常重要 + conn.set_option(ldap.OPT_REFERRALS, 0) + conn.protocol_version = ldap.VERSION3 + # 这里用户名是域账号的全名例如domain/name + #print conn.simple_bind_s(username, password) + conn.simple_bind_s(username, password) + # print 'ldap connect successfully' + + + #调用search方法返回结果id + ldap_result_id = conn.search(baseDN, searchScope, searchFilter, retrieveAttributes) + result_set = [] + #print ldap_result_id + + #print("****************") + while 1: + result_type, result_data = conn.result(ldap_result_id, 0) + if(result_data == []): + break + else: + if result_type == ldap.RES_SEARCH_ENTRY: + result_set.append(result_data) + + #print result_set + Name,Attrs = result_set[0][0] + if hasattr(Attrs, 'has_key') and Attrs.has_key('name'): + ret = {} + #distinguishedName = Attrs['mail'][0] + #distinguishedName = Attrs['name'][0] + #distinguishedName = Attrs['displayName'][0] + #distinguishedName = Attrs['mail'][0] + #distinguishedName = Attrs['memberOf'][0] + #distinguishedName = Attrs['mailNickname'][0] + #distinguishedName = Attrs['sAMAccountName'][0] + #distinguishedName = Attrs['distinguishedName'][0] + #distinguishedName = Attrs['title'][0] + #distinguishedName = Attrs['department'][0] + #distinguishedName = Attrs['manager'][0] + # print "Login Info for user : %s" % distinguishedName + + ret['mail'] = Attrs['mail'][0] + ret['username'] = Attrs['name'][0] + ret['nickname'] = Attrs['displayName'][0] + ret['code'] = 200010 + # print Attrs['memberOf'][0] + #print Attrs['sAMAccountName'][0] + + # print Attrs['title'][0] + # print Attrs['department'][0] + + + + return ret + + else: + return {'code': 400011, 'msg': u'认证失败'} + except ldap.LDAPError, e: + return {'code': 400012, 'msg': u'认证失败'} + +if __name__ == "__main__": + username = "" # ldap中用户名 + password = "" # ldap中密码 + + print login_ldap(username, password) From b9dcee6b0427e21b13d6bb969985493c702203f6 Mon Sep 17 00:00:00 2001 From: Zline Date: Fri, 9 Mar 2018 10:28:41 +0800 Subject: [PATCH 2/5] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 95349ad..30bfcb1 100644 --- a/README.md +++ b/README.md @@ -43,3 +43,5 @@ * LDAP认证:  [*ldap.py*](https://github.com/honglongwei/python-scripts/blob/master/ldap.py) +* 获取本机公网出口IP: +  [*get_local_wip.py*](https://github.com/honglongwei/python-scripts/blob/master/get_local_wip.py) From 12db3bfa68c1c0ad8843d726179e900630154328 Mon Sep 17 00:00:00 2001 From: Zline Date: Fri, 9 Mar 2018 10:31:02 +0800 Subject: [PATCH 3/5] Create get_local_wip.py --- get_local_wip.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 get_local_wip.py diff --git a/get_local_wip.py b/get_local_wip.py new file mode 100644 index 0000000..bd99822 --- /dev/null +++ b/get_local_wip.py @@ -0,0 +1,28 @@ +#encoding: utf-8 +import requests +from bs4 import BeautifulSoup + + +# 获取公网出口IP +def get_out_ip(url): + r = requests.get(url) + txt = r.text + ip = txt[txt.find("[") + 1: txt.find("]")] + print('ip:' + ip) + return ip + + +def get_real_url(url=r'http://www.ip138.com/'): + r = requests.get(url) + txt = r.text + soup = BeautifulSoup(txt,"html.parser").iframe + return soup["src"] + + +if __name__ == '__main__': + ip = get_out_ip(get_real_url()) + with open("result.log", "a") as f: + print >>f, ip + +#pip install pyinstaller +#pyinstaller -F get_local_wip.py >> dist/get_local_wip.exe From 7e965f9bfa56de491184248e17f373caa27c07e3 Mon Sep 17 00:00:00 2001 From: Zline Date: Tue, 20 Mar 2018 14:22:35 +0800 Subject: [PATCH 4/5] Update get_local_wip.py --- get_local_wip.py | 61 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/get_local_wip.py b/get_local_wip.py index bd99822..7efb10d 100644 --- a/get_local_wip.py +++ b/get_local_wip.py @@ -1,16 +1,26 @@ -#encoding: utf-8 +#coding:utf-8 + +import os +import Queue +import codecs import requests +import threading, subprocess +from time import ctime, sleep, time from bs4 import BeautifulSoup - -# 获取公网出口IP def get_out_ip(url): - r = requests.get(url) - txt = r.text - ip = txt[txt.find("[") + 1: txt.find("]")] - print('ip:' + ip) - return ip + req = requests.get(url) + if req.encoding == 'ISO-8859-1': + encodings = requests.utils.get_encodings_from_content(req.text) + if encodings: + encoding = encodings[0] + else: + encoding = req.apparent_encoding + global encode_content + encode_content = req.content.decode(encoding, 'replace') + + return encode_content[encode_content.find("
") + 1: encode_content.find("
")].replace("center>", "") def get_real_url(url=r'http://www.ip138.com/'): r = requests.get(url) @@ -18,11 +28,42 @@ def get_real_url(url=r'http://www.ip138.com/'): soup = BeautifulSoup(txt,"html.parser").iframe return soup["src"] +class ThreadUrl(threading.Thread): + def __init__(self,queue): + threading.Thread.__init__(self) + self.queue = queue + + def run(self): + while True: + host = self.queue.get() + ret = subprocess.Popen('ping -n 600 '+ host, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + with open('iflytek/{0}.log'.format(host), 'a') as f: + print >>f, ret.stdout.read() + self.queue.task_done() + +def startPing(): + for i in range(100): + t=ThreadUrl(queue) + t.setDaemon(True) + t.start() + for host in ['140.143.0.175', '39.107.78.159', '120.92.31.91', '117.121.21.146', '117.121.21.146', '42.62.116.34', '42.62.42.10', '121.201.83.170', '59.107.24.5']: + queue.put(host) + queue.join() if __name__ == '__main__': + print '*'*40 + print '*' + u' 欢迎使用网络质量检测工具 ' + '*' + print '*' + ' ' + '*' + print '*' + u' 程序运行过程中,请不要关闭程序窗口! ' + '*' + print '*'*40 + if not os.path.exists('iflytek'): + os.makedirs('iflytek') ip = get_out_ip(get_real_url()) - with open("result.log", "a") as f: - print >>f, ip + f = codecs.open('iflytek/ISP.log', 'w', 'utf-8') + f.write(ip) + queue = Queue.Queue() + ret = startPing() + #pip install pyinstaller #pyinstaller -F get_local_wip.py >> dist/get_local_wip.exe From affcea69d7edd539e81f2ab0bde429986a047041 Mon Sep 17 00:00:00 2001 From: Zline Date: Wed, 28 Mar 2018 10:00:22 +0800 Subject: [PATCH 5/5] Update ldap.py --- ldap.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ldap.py b/ldap.py index c643aa7..c74e892 100644 --- a/ldap.py +++ b/ldap.py @@ -8,7 +8,7 @@ def login_ldap(username, password): try: # print("开始执行") - Server = "ldap://127.0.0.1:389" + Server = "ldap://8.8.8.8:389" baseDN = "dc=baidu,dc=com" searchScope = ldap.SCOPE_SUBTREE # 设置过滤属性,这里只显示cn=test的信息 @@ -64,6 +64,9 @@ def login_ldap(username, password): ret['mail'] = Attrs['mail'][0] ret['username'] = Attrs['name'][0] ret['nickname'] = Attrs['displayName'][0] + ret['sAMAccountName'] = Attrs['sAMAccountName'][0] + ret['memberOf'] = Attrs['memberOf'][0] + ret['distinguishedName'] = Attrs['distinguishedName'][0] ret['code'] = 200010 # print Attrs['memberOf'][0] #print Attrs['sAMAccountName'][0] @@ -84,4 +87,4 @@ def login_ldap(username, password): username = "" # ldap中用户名 password = "" # ldap中密码 - print login_ldap(username, password) + print login_ldap(username, password)