From b820b16aa8faacfe5039b85fe9f04d87bee42fd5 Mon Sep 17 00:00:00 2001 From: honglongwei Date: Mon, 9 May 2016 09:09:13 +0800 Subject: [PATCH 01/23] add ssh_cmd.py --- README.md | 3 + ssh_cmd.py | 181 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 184 insertions(+) create mode 100644 ssh_cmd.py diff --git a/README.md b/README.md index 7d088f0..807ec30 100644 --- a/README.md +++ b/README.md @@ -18,3 +18,6 @@ * Python八大排序算法的实现: [*psf.py*](https://github.com/honglongwei/python-scripts/blob/master/psf.py) + +* 远程执行命令、远程添加信任、远程自动分区挂盘: + [*ssh_cmd.py*](https://github.com/honglongwei/python-scripts/blob/master/ssh_cmd.py) diff --git a/ssh_cmd.py b/ssh_cmd.py new file mode 100644 index 0000000..e129e0f --- /dev/null +++ b/ssh_cmd.py @@ -0,0 +1,181 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- + +import os +import sys +import time +import argparse +import datetime +import paramiko + +reload(sys) +sys.setdefaultencoding("utf-8") + +username = 'root' +password = '123456' + +rsa = [ +'ssh-rsa1', +'ssh-rsa2' +] + + +#change return color +def G(s): + return "%s[32;2m%s%s[0m"%(chr(27), s, chr(27)) +def R(s): + return "%s[31;2m%s%s[0m"%(chr(27), s, chr(27)) + + +def cmd_exc(ip, username, password): + conn = paramiko.SSHClient() + conn.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + try: + conn.connect(hostname = ip, username = username, password = password, timeout = 5) + stdin, stdout, stderr = conn.exec_command(cmd) + result = stdout.readlines() + ret = ''.join(result) + except: + print R("无法连接") + conn.close() + try: + return G(ret) + except UnboundLocalError: + pass + + +def copy_rsa(ip, username, password): + conn = paramiko.SSHClient() + conn.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + try: + conn.connect(hostname = ip, username = username, password = password, timeout = 5) + stdin, stdout, stderr = conn.exec_command("echo '{0}'>>/root/.ssh/authorized_keys;echo '{1}'>>/root/.ssh/authorized_keys".format(rsa[0], rsa[1])) + result = stdout.readlines() + ret = ''.join(result) + except: + print R("无法连接") + conn.close() + try: + return G(ret) + except UnboundLocalError: + pass + + +def auto_disk(Disk): + if os.path.exists('./auto_disk.sh'): + os.remove('./auto_disk.sh') + with open('auto_disk.sh', 'a') as f: + print>>f, '#!/bin/bash' + print>>f, 'rpm -aq|grep expect' + print>>f, 'if [ $? != 0 ];then' + print>>f, ' yum install -y expect' + print>>f, 'fi' + print>>f, '/usr/bin/expect -c"' + print>>f, 'set timeout -1' + print>>f, 'spawn /sbin/fdisk /dev/{0}'.format(Disk) + print>>f, 'expect \"*m for help*:\"' + print>>f, 'send -- \"n\r\"' + print>>f, 'expect \"*p*\n\"' + print>>f, 'send -- \"p\r\"' + print>>f, 'expect \"*number (1-4):\"' + print>>f, 'send -- \"1\r\"' + print>>f, 'expect \"*default 1*:\"' + print>>f, 'send -- \"\r\"' + print>>f, 'expect \"*default*:\"' + print>>f, 'send -- \"\r\"' + print>>f, 'expect \"*m for help*:\"' + print>>f, 'send -- \"w\r\"' + print>>f, 'expect eof' + print>>f, '"' + print>>f, 'mkfs.ext4 /dev/{0}1'.format(Disk) + print>>f, 'echo "/dev/{0}1 /home/ ext4 defaults 0 0">> /etc/fstab'.format(Disk) + print>>f, 'mount /dev/{0}1 /home/'.format(Disk) + + +def sftp_auto(ip, username, password): + t = paramiko.Transport((ip,22)) + t.connect(username = username, password = password) + sftp = paramiko.SFTPClient.from_transport(t) + sftp.put('./auto_disk.sh','/tmp/auto_disk.sh') + t.close() + conn = paramiko.SSHClient() + conn.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + try: + conn.connect(hostname = ip, username = username, password = password, timeout = 5) + stdin, stdout, stderr = conn.exec_command('sh /tmp/auto_disk.sh') + result = stdout.readlines() + ret = ''.join(result) + except: + print R("无法连接") + conn.close() + try: + return G(ret) + except UnboundLocalError: + pass + + + +if __name__ == "__main__": + parser=argparse.ArgumentParser(description='ssh_cmd', usage='%(prog)s [options]') + parser.add_argument('-H','--host', nargs='?', dest='listhost', help='主机/多个主机用","分割') + parser.add_argument('-f','--file', nargs='?', dest='filehost', help='主机列表文件') + parser.add_argument('-m','--command', nargs='?', dest='command', help='执行命令') + parser.add_argument('-I','--init', nargs='?', dest='init', help='自动分区挂盘') + parser.add_argument('-A','--add', nargs='?', dest='add_rsa', help='添加信任') + if len(sys.argv)==1: + parser.print_help() + else: + args=parser.parse_args() + cmd = args.command + if args.listhost is not None and args.filehost is None: + if args.command is not None: + for ip in args.listhost.split(','): + print G(ip) + print G('-'*80) + print cmd_exc(ip, username, password) + print + elif args.init is not None: + auto_disk(args.init) + for ip in args.listhost.split(','): + print G(ip) + print G('-'*80) + print sftp_auto(ip, username, password) + print + elif args.add_rsa == 'root': + for ip in args.listhost.split(','): + print G(ip) + print G('-'*80) + print copy_rsa(ip, username, password) + print + else: + print R('功能选项为空') + elif args.listhost is None and args.filehost is not None: + if args.command is not None: + try: + with open(args.filehost) as f: + for ips in f.readlines(): + ip = ips.replace('\n', '') + print G(ip) + print G('-'*80) + print cmd_exc(ip, username, password) + print + except IOError: + print R('主机列表文件不存在') + elif args.init is not None: + auto_disk(args.init) + for ip in args.listhost.split(','): + print G(ip) + print G('-'*80) + print sftp_auto(ip, username, password) + print + elif args.add_rsa == 'root': + for ip in args.listhost.split(','): + print G(ip) + print G('-'*80) + print copy_rsa(ip, username, password) + print + else: + print R('功能选项为空') + else: + print R('主机或命令不能为空') + From 02c0e8c607efc931f45d326eca04169947abb5a2 Mon Sep 17 00:00:00 2001 From: honglongwei Date: Wed, 8 Jun 2016 13:19:38 +0800 Subject: [PATCH 02/23] add gameops.py --- README.md | 3 ++ gameops.py | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 gameops.py diff --git a/README.md b/README.md index 807ec30..89e5245 100644 --- a/README.md +++ b/README.md @@ -21,3 +21,6 @@ * 远程执行命令、远程添加信任、远程自动分区挂盘: [*ssh_cmd.py*](https://github.com/honglongwei/python-scripts/blob/master/ssh_cmd.py) + +* 基于saltstack的nested输出的运维脚本: + [*gameops.py*](https://github.com/honglongwei/python-scripts/blob/master/gameops.py) diff --git a/gameops.py b/gameops.py new file mode 100644 index 0000000..a46cb3b --- /dev/null +++ b/gameops.py @@ -0,0 +1,146 @@ +#!/usr/bin/env python +# -*- coding: utf8 -*- + +__author__ = 'honglongwei' + +import os +import sys +import time +import zipfile +import datetime +import subprocess +import salt.client +from salt.output.nested import NestDisplay +from salt.utils import get_colors + +#script path +sct_lt = {'start': '/home/game/startup.sh', + 'stop': '/home/game/shutdown.sh' + } +#pid path +ser_pid = '/home/game/game.pid' +#src update package path +src_path = '/home/update' +#des update package path +des_path = '/home' +#src backup package path +bsrc_path_lt = ['/home/game/config', + '/home/game/data', + '/home/game/hibernate'] +#des backup package path +bdes_path = '/home/backup' + + +# call salt output class +class NestPut(NestDisplay): + def __init__(self): + self.colors = get_colors(True) + self.__dict__.update(get_colors(True)) + self.strip_colors = True + +def Prest(data): + ''' + Display ret data + ''' + nest = NestPut() + print '\n'.join(nest.display(data, 0, '', [])) + + +def start(): + ''' + Define func start server + ''' + if os.path.exists(ser_pid): + return Prest('GameServer is already running !!!') + else: + cmd = sct_lt['start'] + proc = subprocess.Popen(cmd, shell=True) + return Prest('Start GameServer is successful !!!') + + +def stop(): + ''' + Define func stop server + ''' + if os.path.exists(ser_pid): + cmd = sct_lt['stop'] + proc = subprocess.Popen(cmd, shell=True) + return Prest('Stop GameServer is successful !!!') + else: + return Prest('GameServer is already stopped !!!') + + +def status(): + ''' + Define func status server + ''' + if os.path.exists(ser_pid): + return Prest('GameServer is not running !!!') + else: + cmd = 'ps -ef|grep \'{0}\'|grep -v grep'.format('server') + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) + ret = proc.stdout.read() + return Prest(ret) + + +def backup(): + ''' + Define func backup server + ''' + bakname = 'gs_' + datetime.datetime.now().strftime('%Y%m%d%H%M%S') + '.zip' + zipname = os.path.join(bdes_path, bakname) + f = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) + for bsrc_path in bsrc_path_lt: + bac_path = os.path.dirname(bsrc_path) + ls_path = bac_path + '/' + zg_path = bsrc_path.split(ls_path)[1] + os.chdir(bac_path) + for dirpath, dirnames, filenames in os.walk(zg_path): + for filename in filenames: + f.write(os.path.join(dirpath, filename)) + f.close() + return 'Backup is successful !' + + +def update(pkg): + ''' + Define func update server + ''' + if pkg: + fl = os.path.join(src_path, pkg) + try: + zfile = zipfile.ZipFile(fl,'r') + for filename in zfile.namelist(): + zfile.extract(filename, des_path) + return 'Update is successful !' + except IOError: + return 'The package is invalid !!!' + else: + return 'The package is invalid !!!' + + + +if __name__== "__main__": + # check arguments + opts = sys.argv + if len(opts) < 2: + print 'start|stop|status|update' + sys.exit(0) + elif len(opts) == 2: + if opts[1]=='start': + start() + elif opts[1]=='stop': + stop() + elif opts[1]=='status': + status() + elif opts[1]=='backup': + backup() + else: + print 'Script Parameter Error !!!' + elif len(opts) == 3: + if opts[1]=='update': + update(opts[2]) + else: + print 'Script Parameter Error !!!' + else: + print 'Script Parameter Error !!!' From 11db43910eb0e3f43cad99016d4e692981bb296c Mon Sep 17 00:00:00 2001 From: honglongwei Date: 2016年7月27日 13:55:57 +0800 Subject: [PATCH 03/23] Update gameops.py --- gameops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gameops.py b/gameops.py index a46cb3b..27fbf71 100644 --- a/gameops.py +++ b/gameops.py @@ -124,7 +124,7 @@ def update(pkg): # check arguments opts = sys.argv if len(opts) < 2: - print 'start|stop|status|update' + print 'start|stop|status|backup|update' sys.exit(0) elif len(opts) == 2: if opts[1]=='start': From 1b2cd77d90a77fe1646171ef4e71ddbb9ddade1c Mon Sep 17 00:00:00 2001 From: root Date: 2016年8月19日 16:35:02 +0800 Subject: [PATCH 04/23] add monitor_log --- README.md | 3 +++ monitor_log.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 monitor_log.py diff --git a/README.md b/README.md index 89e5245..3a9d6aa 100644 --- a/README.md +++ b/README.md @@ -24,3 +24,6 @@ * 基于saltstack的nested输出的运维脚本: [*gameops.py*](https://github.com/honglongwei/python-scripts/blob/master/gameops.py) + +* 游戏日志监控脚本: + [*monitor_log.py*](https://github.com/honglongwei/python-scripts/blob/master/monitor_log.py) diff --git a/monitor_log.py b/monitor_log.py new file mode 100644 index 0000000..450e932 --- /dev/null +++ b/monitor_log.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +# -*- coding: utf8 -*- + +import os +import time +import datetime + + +def GetLog(num): + fls = [] + for dirpath, dirnames, filenames in os.walk('/opt/logbak'): + for filename in filenames: + fn = os.path.join(dirpath, filename) + now_time = datetime.datetime.now() + last_time = now_time + datetime.timedelta(days=-num) + tg_time = last_time.strftime('%Y-%m-%d') + endf = tg_time + '.tgz' + if filename.startswith('xxx_') and filename.endswith(endf): + if os.path.exists(fn): + fls.append(fn) + else: + pass + else: + pass + + xlog = [] + for a in fls: + xlog.append(a[13:17]) + xlog.sort() + return xlog + + +blog = GetLog(2) +alog = GetLog(1) +if len(blog)> len(alog): + print '-' + ' ' + ','.join(list(set(blog).difference(set(alog)))) #交集:list(set(a).intersection(set(b))) 并集:list(set(a).union(set(b))) 差集:list(set(b).difference(set(a))) # b中有而a中没有的 +elif len(blog) < len(alog): + print '+' + ' ' + ','.join(list(set(alog).difference(set(blog)))) +else: + print 0 + + From f940bd167cda59023923e820ac03c936bb749d6b Mon Sep 17 00:00:00 2001 From: root Date: 2016年9月28日 14:34:18 +0800 Subject: [PATCH 05/23] update monitor_log --- monitor_log.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/monitor_log.py b/monitor_log.py index 450e932..10845dc 100644 --- a/monitor_log.py +++ b/monitor_log.py @@ -39,4 +39,16 @@ def GetLog(num): else: print 0 - +''' +jdata = set(blog).intersection(set(alog)) //交集 +mor = list(set(alog).difference(jdata)) //新增 +les = list(set(blog).difference(jdata)) //减少 +if len(mor) == 0 and len(les) == 0: + print 0 +elif len(mor) == 0 and len(les) != 0: + print '-: {0}'.format(','.join(les)) +elif len(mor) != 0 and len(les) == 0: + print '+: {0}'.format(','.join(mor)) +else: + print '+: {jia}\n-: {shao}'.format(jia=','.join(mor), shao=','.join(les)) +''' From 7856ff6ab338880408f355e76b327a1ae4715069 Mon Sep 17 00:00:00 2001 From: root Date: 2016年9月28日 14:38:30 +0800 Subject: [PATCH 06/23] update log --- monitor_log.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/monitor_log.py b/monitor_log.py index 10845dc..10eeed7 100644 --- a/monitor_log.py +++ b/monitor_log.py @@ -32,17 +32,14 @@ def GetLog(num): blog = GetLog(2) alog = GetLog(1) -if len(blog)> len(alog): - print '-' + ' ' + ','.join(list(set(blog).difference(set(alog)))) #交集:list(set(a).intersection(set(b))) 并集:list(set(a).union(set(b))) 差集:list(set(b).difference(set(a))) # b中有而a中没有的 -elif len(blog) < len(alog): - print '+' + ' ' + ','.join(list(set(alog).difference(set(blog)))) -else: - print 0 - ''' -jdata = set(blog).intersection(set(alog)) //交集 -mor = list(set(alog).difference(jdata)) //新增 -les = list(set(blog).difference(jdata)) //减少 + 交集:list(set(a).intersection(set(b))) + 并集:list(set(a).union(set(b))) + 差集:list(set(b).difference(set(a))) // b中有而a中没有的 +''' +jdata = set(blog).intersection(set(alog)) #交集 +mor = list(set(alog).difference(jdata)) #新增 +les = list(set(blog).difference(jdata)) #减少 if len(mor) == 0 and len(les) == 0: print 0 elif len(mor) == 0 and len(les) != 0: @@ -51,4 +48,3 @@ def GetLog(num): print '+: {0}'.format(','.join(mor)) else: print '+: {jia}\n-: {shao}'.format(jia=','.join(mor), shao=','.join(les)) -''' From c44a807f716786ff25eb538f75fed366227f2500 Mon Sep 17 00:00:00 2001 From: Zline Date: 2017年3月15日 14:49:26 +0800 Subject: [PATCH 07/23] Create ssh_excel.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 扫描服务器生成excel报表 --- ssh_excel.py | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 ssh_excel.py 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() From ed7f71ef65f2e06d57cb71838d72bf452255e8e8 Mon Sep 17 00:00:00 2001 From: Zline Date: 2017年3月15日 14:51:02 +0800 Subject: [PATCH 08/23] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 3a9d6aa..b6a898f 100644 --- a/README.md +++ b/README.md @@ -27,3 +27,6 @@ * 游戏日志监控脚本: [*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) From 25e2940ba0f5033937c5890f2bd8b42ff992f3c5 Mon Sep 17 00:00:00 2001 From: Zline Date: 2017年3月15日 14:51:51 +0800 Subject: [PATCH 09/23] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b6a898f..2fadb70 100644 --- a/README.md +++ b/README.md @@ -28,5 +28,5 @@ * 游戏日志监控脚本: [*monitor_log.py*](https://github.com/honglongwei/python-scripts/blob/master/monitor_log.py) -* 扫描服务器生产Excel报表: +* 扫描服务器生成Excel报表: [*ssh_excel.py*](https://github.com/honglongwei/python-scripts/blob/master/ssh_excel.py) From f5adb01450ed400a34f70869648af754028d7ff3 Mon Sep 17 00:00:00 2001 From: Zline Date: 2017年4月14日 09:34:16 +0800 Subject: [PATCH 10/23] Create scan_web_banner.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 扫描web并获取版本 --- scan_web_banner.py | 112 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 scan_web_banner.py 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() From 09c7d1d5bc52a5a195094247ba27ffb32e246aaa Mon Sep 17 00:00:00 2001 From: Zline <honglongwei@users.noreply.github.com> Date: 2017年4月14日 09:35:29 +0800 Subject: [PATCH 11/23] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 2fadb70..eb0cd22 100644 --- a/README.md +++ b/README.md @@ -30,3 +30,6 @@ * 扫描服务器生成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) From 141dd961b1b48f42dc790287a5a7f0395651b4b3 Mon Sep 17 00:00:00 2001 From: Zline <honglongwei@users.noreply.github.com> Date: 2017年4月14日 09:37:20 +0800 Subject: [PATCH 12/23] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eb0cd22..d8c0a87 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -###运维通用python脚本框架 +### 运维通用python脚本框架 * 日常运维: From cf9f41df6eda91ed388ccc75665c7dc74eaab355 Mon Sep 17 00:00:00 2001 From: Zline <honglongwei@users.noreply.github.com> Date: 2017年5月31日 11:22:30 +0800 Subject: [PATCH 13/23] Create dodb.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit python操作db的原生sql --- dodb.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 dodb.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 From b800d1bef53a19bb73201b9d3823c01b86e890cd Mon Sep 17 00:00:00 2001 From: Zline <honglongwei@users.noreply.github.com> Date: 2017年5月31日 11:24:31 +0800 Subject: [PATCH 14/23] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index d8c0a87..20e727f 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,9 @@ * 日常运维: [*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) From 98c4dabc045482406184b31cb00eb37896d99fd2 Mon Sep 17 00:00:00 2001 From: Zline <honglongwei@users.noreply.github.com> Date: Wed, 5 Jul 2017 12:47:40 +0800 Subject: [PATCH 15/23] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 20e727f..589395a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ [*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) From d829c91f23ad540ff3cbdd77fec3fc3c6133a50c Mon Sep 17 00:00:00 2001 From: Zline <honglongwei@users.noreply.github.com> Date: 2017年8月15日 20:14:31 +0800 Subject: [PATCH 16/23] Create unixtimeformat.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Unix时间戳转换 --- unixtimeformat.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 unixtimeformat.py diff --git a/unixtimeformat.py b/unixtimeformat.py new file mode 100644 index 0000000..fd5cec4 --- /dev/null +++ b/unixtimeformat.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 From 560788c3d95321c40b62d928e4e9ac10b7fd89ed Mon Sep 17 00:00:00 2001 From: Zline <honglongwei@users.noreply.github.com> Date: 2017年8月15日 20:15:06 +0800 Subject: [PATCH 17/23] Rename unixtimeformat.py to unix_time_format.py --- unixtimeformat.py => unix_time_format.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename unixtimeformat.py => unix_time_format.py (100%) diff --git a/unixtimeformat.py b/unix_time_format.py similarity index 100% rename from unixtimeformat.py rename to unix_time_format.py From ce6859dadb4d22d34d55b3b75b40aa18125ff9a0 Mon Sep 17 00:00:00 2001 From: Zline <honglongwei@users.noreply.github.com> Date: 2017年8月15日 20:16:51 +0800 Subject: [PATCH 18/23] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 589395a..b6d8aed 100644 --- a/README.md +++ b/README.md @@ -36,3 +36,7 @@ * 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) + From 8e184ee6ae0d90ee19bdc8ce6c3cbd525c217be6 Mon Sep 17 00:00:00 2001 From: tes <tes@163.com> Date: 2018年1月11日 20:11:14 +0800 Subject: [PATCH 19/23] 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 <honglongwei@users.noreply.github.com> Date: Fri, 9 Mar 2018 10:28:41 +0800 Subject: [PATCH 20/23] 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 <honglongwei@users.noreply.github.com> Date: Fri, 9 Mar 2018 10:31:02 +0800 Subject: [PATCH 21/23] 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 <honglongwei@users.noreply.github.com> Date: 2018年3月20日 14:22:35 +0800 Subject: [PATCH 22/23] 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("<div>") + 1: encode_content.find("</div>")].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 <honglongwei@users.noreply.github.com> Date: 2018年3月28日 10:00:22 +0800 Subject: [PATCH 23/23] 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) </div><div class="naked_ctrl"> <form action="/index.cgi/contrast" method="get" name="gate"> <p><a href="http://altstyle.alfasado.net">AltStyle</a> によって変換されたページ <a href="https://github.com/chenpiao/python-scripts/compare/master...honglongwei:python-scripts:master.patch">(->オリジナル)</a> / <label>アドレス: <input type="text" name="naked_post_url" value="https://github.com/chenpiao/python-scripts/compare/master...honglongwei:python-scripts:master.patch" size="22" /></label> <label>モード: <select name="naked_post_mode"> <option value="default">デフォルト</option> <option value="speech">音声ブラウザ</option> <option value="ruby">ルビ付き</option> <option value="contrast" selected="selected">配色反転</option> <option value="larger-text">文字拡大</option> <option value="mobile">モバイル</option> </select> <input type="submit" value="表示" /> </p> </form> </div>