diff --git a/README.md b/README.md
index 3a9d6aa..30bfcb1 100644
--- a/README.md
+++ b/README.md
@@ -1,11 +1,14 @@
-###运维通用python脚本框架
+### 运维通用python脚本框架
* 日常运维:
[*opsmod.py*](https://github.com/honglongwei/python-scripts/blob/master/opsmod.py)
+
+* python操作mysql的原生sql:
+ [*dodb.py*](https://github.com/honglongwei/python-scripts/blob/master/dodb.py)
* zabbix修改可见名称API:
- [*zabbix_update_pj-api*](https://github.com/honglongwei/python-scripts/blob/master/zabbix_update_pj-api.py)
+ [*zabbix_update_pj-api.py*](https://github.com/honglongwei/python-scripts/blob/master/zabbix_update_pj-api.py)
* zabbix通用API:
[*zabbix-api.py*](https://github.com/honglongwei/python-scripts/blob/master/zabbix-api.py)
@@ -27,3 +30,18 @@
* 游戏日志监控脚本:
[*monitor_log.py*](https://github.com/honglongwei/python-scripts/blob/master/monitor_log.py)
+
+* 扫描服务器生成Excel报表:
+ [*ssh_excel.py*](https://github.com/honglongwei/python-scripts/blob/master/ssh_excel.py)
+
+* Web扫描:
+ [*scan_web_banner.py*](https://github.com/honglongwei/python-scripts/blob/master/scan_web_banner.py)
+
+* 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)
+
+* 获取本机公网出口IP:
+ [*get_local_wip.py*](https://github.com/honglongwei/python-scripts/blob/master/get_local_wip.py)
diff --git a/dodb.py b/dodb.py
new file mode 100644
index 0000000..eaae815
--- /dev/null
+++ b/dodb.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+#_*_ coding:utf-8 _*_
+
+import MySQLdb
+
+
+with open('/var/logs/install.log') as f:
+ a = f.read().strip().split('\n')
+ hostl = []
+ for i in a:
+ b = i.split('\t')
+ if b[3] == 'stop':
+ hostl.append(b[1])
+ else:
+ pass
+ for host in list(set(hostl)):
+ conn= MySQLdb.connect(
+ host='localhost',
+ port = 3306,
+ user='root',
+ passwd='111111',
+ db ='pj_install',
+ )
+ try:
+ #select
+ sql = "select * from automsg where HostName='{0}'".format(host)
+ cur = conn.cursor()
+ cur.execute(sql)
+ ret = cur.fetchone()
+
+ #update
+ update_sql = "update installretmsg set status=2, msg=u'安装成功!' where id={0}".format(int(ret[0]))
+ cur.execute(update_sql)
+
+ cur.close()
+ conn.commit()
+ conn.close()
+ except:
+ pass
diff --git a/get_local_wip.py b/get_local_wip.py
new file mode 100644
index 0000000..7efb10d
--- /dev/null
+++ b/get_local_wip.py
@@ -0,0 +1,69 @@
+#coding:utf-8
+
+import os
+import Queue
+import codecs
+import requests
+import threading, subprocess
+from time import ctime, sleep, time
+from bs4 import BeautifulSoup
+
+def get_out_ip(url):
+ 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)
+ txt = r.text
+ 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())
+ 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
diff --git a/ldap.py b/ldap.py
new file mode 100644
index 0000000..c74e892
--- /dev/null
+++ b/ldap.py
@@ -0,0 +1,90 @@
+#!usr/bin/env python
+#coding: utf-8
+
+import os
+import sys
+import ldap
+
+def login_ldap(username, password):
+ try:
+ # print("开始执行")
+ Server = "ldap://8.8.8.8: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['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]
+
+ # 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)
diff --git a/scan_web_banner.py b/scan_web_banner.py
new file mode 100644
index 0000000..7bb0ea5
--- /dev/null
+++ b/scan_web_banner.py
@@ -0,0 +1,112 @@
+#/usr/bin/env python
+#-*-coding=utf-8-*-
+
+# __author__ = 'Zline'
+
+import requests
+import re
+from threading import Thread,Lock
+import time
+import sys
+import chardet
+import netaddr
+import struct
+import socket
+
+lock = Lock()
+
+def ip2int(addr):
+ return struct.unpack("!I", socket.inet_aton(addr))[0]
+def int2ip(addr):
+ return socket.inet_ntoa(struct.pack("!I", addr))
+def int_dec(pagehtml):
+
+ charset = None
+ if pagehtml != '':
+ # print 'use charset dect'
+ enc = chardet.detect(pagehtml)
+ # print 'enc= ', enc
+ if enc['encoding'] and enc['confidence'] > 0.9:
+ charset = enc['encoding']
+
+ if charset == None:
+ charset_re = re.compile("((^|;)\s*charset\s*=)([^\"']*)", re.M)
+ charset=charset_re.search(pagehtml[:1000])
+ charset=charset and charset.group(3) or None
+
+ # test charset
+ try:
+ if charset:
+ unicode('test',charset,errors='replace')
+ except Exception,e:
+ print 'Exception',e
+ charset = None
+ # print 'charset=', charset
+ return charset
+
+
+def http_banner(url):
+ ip=url
+ try:
+ url=requests.get(url,timeout=2)
+
+ body = url.content
+
+ charset = None
+ if body != '':
+ charset = int_dec(body)
+
+ if charset == None or charset == 'ascii':
+ charset = 'ISO-8859-1'
+
+ if charset and charset != 'ascii' and charset != 'unicode':
+ try:
+ body = unicode(body,charset,errors='replace')
+ except Exception, e:
+ body = ''
+ Struts=url.status_code
+ Server=url.headers['server'][0:13]
+ if Struts==200 or Struts==403 or Struts==401:
+ title=re.findall(r"(.*)<\/title>",body)
+ if len(title):
+ title = title[0].strip()
+ else:
+ title = ''
+ lock.acquire()
+ print ('%s\t%d\t%-10s\t%s'%(ip.lstrip('http://'),Struts,Server,title))
+ lock.release()
+ except (requests.HTTPError,requests.RequestException,AttributeError,KeyError),e:
+ pass
+
+
+
+if __name__ == '__main__':
+ if len(sys.argv) >= 2:
+ ips = sys.argv[1]
+ else:
+ print 'usage: python http_banner.py 192.168.1./24 '
+ print 'usage: python http_banner.py 192.168.1.1-192.168.1.254 '
+ print 'usage: python http_banner.py 192.168.1./24 8080'
+ print 'usage: python http_banner.py 192.168.1.1-192.168.1.254 8080'
+ sys.exit()
+ port = '80'
+ if len(sys.argv) == 3:
+ port = sys.argv[2]
+
+ if '-' in ips:
+ start, end = ips.split('-')
+ startlong = ip2int(start)
+ endlong = ip2int(end)
+ ips = netaddr.IPRange(start,end)
+ for ip in list(ips):
+ url='http://%s:%s'%(ip,port)
+ t = Thread(target=http_banner,args=(url,))
+ t.daemon=False
+ t.start()
+ elif '/' in ips:
+ ips = netaddr.IPNetwork(ips)
+ for ip in list(ips):
+ url='http://%s:%s'%(ip,port)
+ t = Thread(target=http_banner,args=(url,))
+ t.daemon=False
+ t.start()
diff --git a/ssh_excel.py b/ssh_excel.py
new file mode 100644
index 0000000..e95960e
--- /dev/null
+++ b/ssh_excel.py
@@ -0,0 +1,103 @@
+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+import os
+import sys
+import xlwt
+import time
+import json
+import urllib
+import urllib2
+import datetime
+import paramiko
+import commands
+
+
+reload(sys)
+sys.setdefaultencoding("utf-8")
+
+username = 'root'
+password = '123456'
+
+url_idc = 'http://www.baidu.com/assetInfo'
+url_clound = 'http://www.tengxun.com/cloudAsset'
+
+def GetAllSeal(url):
+ user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
+ if url == url_clound:
+ values = {'test': 'demo'}
+ elif url == url_idc:
+ values = {'product': u'运维'}
+ else:
+ return 'URL is Error !!!'
+ headers = {'User-Agent': user_agent}
+ data = urllib.urlencode(values)
+ req = urllib2.Request(url, data, headers)
+ response = urllib2.urlopen(req)
+ res = response.read()
+ a = json.loads(res)
+ return a
+
+
+def set_style(name, height, bold=False):
+ style = xlwt.XFStyle()
+
+ font = xlwt.Font()
+ font.name = name
+ font.bold = bold
+ font.color_index = 4
+ font.height = height
+
+ style.font = font
+ return style
+
+def WriteDateExcel():
+ Tm = datetime.datetime.now().strftime('%Y-%m-%d')
+ urls = [url_clound, url_idc]
+ wbk = xlwt.Workbook(encoding='utf-8')
+ for url in urls:
+ result = GetAllSeal(url)
+ if url == url_clound:
+ sheet1 = wbk.add_sheet(u'云资产扫描结果', cell_overwrite_ok=True)
+ row0 = [u'业务', u'负责人', u'外网IP', u'内网IP', u'操作系统', u'扫描结果']
+ for x in xrange(len(row0)):
+ sheet1.write(0, x, row0[x], set_style('Times New Roman',220,True))
+ for i in xrange(len(result)):
+ conn = paramiko.SSHClient()
+ conn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
+ try:
+ conn.connect(hostname = result[i]['WIp'], username = 'root', timeout = 1)
+ stdin, stdout, stderr = conn.exec_command("iptables -nvL|grep -E '10.0.0.0/8|10.12.0.0/16'")
+ cdt = stdout.readlines()
+ ret = ''.join(cdt)
+ except:
+ ret = u'无法连接'
+ contl = [result[i]['Name'], result[i]['Responser'], result[i]['WIp'], result[i]['LIp'], result[i]['os'], ret]
+ for j in xrange(len(contl)):
+ sheet1.write(i+1, j, contl[j])
+ elif url == url_idc:
+ sheet2 = wbk.add_sheet(u'物理机扫描结果', cell_overwrite_ok=True)
+ row0 = [u'项目', u'负责人', u'外网IP', u'内网IP', u'操作系统', u'扫描结果']
+ for x in xrange(len(row0)):
+ sheet2.write(0, x, row0[x], set_style('Times New Roman',220,True))
+ for i in xrange(len(result)):
+ conn = paramiko.SSHClient()
+ conn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
+ try:
+ conn.connect(hostname = result[i]['wIp'], username = 'root', timeout = 1)
+ stdin, stdout, stderr = conn.exec_command("iptables -nvL|grep -E '10.0.0.0/8|10.12.0.0/16'")
+ cdt = stdout.readlines()
+ ret = ''.join(cdt)
+ except:
+ ret = u'无法连接'
+ contl = [result[i]['Name'], result[i]['Owner'], result[i]['wIp'], result[i]['lIp'], result[i]['os'], ret]
+ for j in xrange(len(contl)):
+ sheet2.write(i+1, j, contl[j])
+ else:
+ return 'URL is Error !!!'
+ wbk.save('checkseal_{0}.xls'.format(Tm))
+
+
+if __name__ == "__main__":
+ WriteDateExcel()
diff --git a/unix_time_format.py b/unix_time_format.py
new file mode 100644
index 0000000..fd5cec4
--- /dev/null
+++ b/unix_time_format.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import time
+
+def timestamp_datetime(value):
+ format = '%Y-%m-%d %H:%M:%S'
+ # value为传入的值为时间戳(整形),如:1332888820
+ value = time.localtime(value)
+ ## 经过localtime转换后变成
+ ## time.struct_time(tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, tm_wday=2, tm_yday=88, tm_isdst=0)
+ # 最后再经过strftime函数转换为正常日期格式。
+ dt = time.strftime(format, value)
+ return dt
+
+def datetime_timestamp(dt):
+ #dt为字符串
+ #中间过程,一般都需要将字符串转化为时间数组
+ time.strptime(dt, '%Y-%m-%d %H:%M:%S')
+ ## time.struct_time(tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, tm_wday=2, tm_yday=88, tm_isdst=-1)
+ #将"2012-03-28 06:53:40"转化为时间戳
+ s = time.mktime(time.strptime(dt, '%Y-%m-%d %H:%M:%S'))
+ return int(s)
+
+if __name__ == '__main__':
+ d = datetime_timestamp('2015-07-28 21:53:40')
+ print d
+ s = timestamp_datetime(1352889820)
+ print s