前言:为了避免开发人员将敏感信息写入文件传到github,所以测试人员需要检查每个仓库是否有写入,人工搜索审核比较繁琐,所以写一个脚本通过配置 配置文件,指定需要搜索的仓库和每个仓库需要搜索的关键字,生成文件

版本声明

python2.7.8

selenium 2.48.0

火狐浏览器30版本

github.ini

#登录github用户名密码
[login]
usr=111
pwd=222
#定义需要搜索的组织下哪个仓库
[Warehouse]
Warehouse_list=[test1/projectA,test2/projectB] #定义敏感关键字
[SensitiveKey]
keyWord_list=[smtp,password,host] [email]
from_email=[]
to_email=[]

上脚本

[RunScripts.py]

# coding:utf-8
from selenium import webdriver
import time
import os
import datetime
import smtplib
from email.mime.text import MIMEText
import ConfigParser
import zipfile
from email.mime.application import MIMEApplication
import email.mime.multipart
import email.mime.text class conf_git(): def ini_list(self,value):
w_len = len(value)
str_o = w_len - 1
value_str = value.replace(value[0], '')
value = value_str.replace(value[str_o], '')
value_list = value.split(',')
return value_list def _take_dict(self): cf = ConfigParser.ConfigParser() cf.read('github.ini') save_body={
"name":None,
"pwd":None,
"Warehouse":None,
"SensitiveKey":None,
"from_email":None,
"to_email":None }
try:
login_usr=cf.get("login","usr")
login_pwd = cf.get("login", "pwd")
from_email = cf.get("email", "from_email")
from_email_list=self.ini_list(from_email) to_email = cf.get("email", "to_email")
to_email_list = self.ini_list(to_email) # 读需要搜索的仓库转换list
Warehouse = cf.get("Warehouse", "Warehouse_list")
Warehouse_list=self.ini_list(Warehouse) # 需要搜索的敏感关键字
SensitiveKey = cf.get("SensitiveKey", "keyWord_list")
SensitiveKey_list = self.ini_list(SensitiveKey) save_body['name']=login_usr
save_body['pwd'] = login_pwd
save_body['Warehouse'] = Warehouse_list
save_body['SensitiveKey'] = SensitiveKey_list save_body['from_email']=from_email_list save_body['to_email'] = to_email_list
conf_dic=save_body
except Exception as errorLog:
print("读写错误,文件配置没有找到或没有匹配项")
return False
else: return conf_dic
finally:
pass class _serch_git(conf_git):
def __init__(self):
conf_dic=self._take_dict()
self.Warehouse_list=conf_dic['Warehouse']
self.SensitiveKey_list = conf_dic['SensitiveKey'] self.name=conf_dic['name']
self.pwd = conf_dic['pwd'] self.from_email=conf_dic['from_email']
self.to_email = conf_dic['to_email'] def get_time(self):
nowTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') # 现在
nowTime = str(nowTime)
nowTime_str = nowTime.replace(' ', '')
nowTime = nowTime_str.replace(':', '-')
yymm = nowTime[0:10]
hhmm = nowTime[10:]
file_time=yymm + '-' + hhmm
nowTime='reportIMG'+yymm + '-' + hhmm return nowTime,file_time def mkdir_file(self):
nowTime = self.get_time()[0]
for dir_f in self.Warehouse_list:
for dir_key in self.SensitiveKey_list:
if '/' in dir_f:
dir_f=dir_f.replace('/','$')
else:
pass
pr_files = nowTime + '/' + dir_f + '/' + dir_key
os.makedirs(pr_files) return nowTime def _login_git_serch(self):
file_master=self.mkdir_file()
self.Warehouse_lists=self.Warehouse_list
self.SensitiveKey_lists=self.SensitiveKey_list
driver = webdriver.Firefox()
driver.get('https://github.com/login')
driver.maximize_window()
driver.find_element_by_id('login_field').send_keys(self.name)
driver.find_element_by_id('password').send_keys(self.pwd)
driver.find_element_by_name('commit').click()
time.sleep(3)
_current_file=os.path.split(os.path.realpath(__file__))[0]
file = self.get_time()[1]
for Warehouse in self.Warehouse_lists:
git_url = 'https://github.com/' + Warehouse
for SensitiveKey in self.SensitiveKey_lists:
git_urls = git_url+ '/search?q=' + SensitiveKey + '&unscoped_q=' + SensitiveKey
driver.get(git_urls)
# time.sleep(1)
if '/' in Warehouse:
Warehouse_file=Warehouse.replace('/','$')
else:
Warehouse_file=Warehouse
try:
all_page_ele = driver.find_element_by_xpath('//*[@id="code_search_results"]/div[2]/div/em')
all_page = all_page_ele.get_attribute('data-total-pages') # print(all_page)
except:
# print (unicode('只有一页数据:', 'utf-8'))
# driver.save_screenshot(_current_file + '\\' +file_master+ '\\'+Warehouse_file + '\\'+SensitiveKey +'\\'+ file + '.png')
driver.save_screenshot(_current_file + '\\' + file_master + '\\' + Warehouse_file + '\\' + SensitiveKey + '\\' + Warehouse_file+ SensitiveKey+ '1.png')
# print (unicode('截图完成:', 'utf-8'))
else:
true_ele = str(all_page)
# print(true_ele)
true_ele_int = int(true_ele)
# print(true_ele_int) for pages in range(1,true_ele_int+1):
file = self.get_time()[1]
driver.get('https://github.com/' + Warehouse + '/search?p=' + str(pages) + '&q=' + SensitiveKey + '&type=&utf8=%E2%9C%93')
# driver.save_screenshot(_current_file + '\\' +file_master+ '\\'+Warehouse_file + '\\'+SensitiveKey +'\\'+ file + '.png')
driver.save_screenshot(
_current_file + '\\' + file_master + '\\' + Warehouse_file + '\\' + SensitiveKey + '\\' + Warehouse_file + SensitiveKey +str(pages)+ '.png')
# print(_current_file + '\\' +file_master+ Warehouse_file + '\\'+SensitiveKey +'\\'+ file + '.png')
# print a
finally:
pass
driver.quit()
print (unicode('指定仓库根据指定的关键字已截图完成', 'utf-8'))
return file_master class sendEmail(_serch_git): def send_forUsr(self): file_name=self._login_git_serch()
if "" in self.to_email:
print (unicode('脚本执行完成:', 'utf-8')) else: print (unicode('正在打包发送邮件:', 'utf-8'))
f = zipfile.ZipFile(file_name + '.zip', 'w', zipfile.ZIP_DEFLATED)
startdir = file_name
for dirpath, dirnames, filenames in os.walk(startdir):
for filename in filenames:
f.write(os.path.join(dirpath, filename))
time.sleep(3) # 发送邮件
self.mailserver = 'smtp.mxhichina.com'
self.username_send = self.from_email[0]
self.password = self.from_email[1]
self.user_list = self.to_email
# 主题
subject = 'Github敏感关键字自动化搜索测试报告[附件]'
# 内容
content = '具体截图请查收附件'
for usr in self.user_list:
msg = email.mime.multipart.MIMEMultipart()
msg['from'] = self.username_send
msg['to'] = usr
msg['subject'] = subject
content = content
txt = email.mime.text.MIMEText(content, 'plain', 'utf-8')
msg.attach(txt) # 添加附件
part = MIMEApplication(open(file_name+'.zip', 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename=file_name+'.zip')
msg.attach(part)
time.sleep(3) smtp = smtplib.SMTP()
smtp.connect(self.mailserver, '25')
smtp.login(self.username_send, self.password)
try:
smtp.sendmail(self.username_send, usr, str(msg))
except:
print (unicode('邮件未发送', 'utf-8'))
else:
print (unicode('邮件发送成功', 'utf-8'))
finally:
smtp.quit() if __name__ =="__main__":
sendEmail().send_forUsr()
else:
pass

 

有个问题,邮件发送后,附件经常破损,后面有时间解决

自动化生成的文件如下:

指定的仓库

指定仓库搜索的关键字

自动截图

python编写脚本,登录Github通过指定仓库指定敏感关键字搜索自动化截图生成文件【完美截图】的更多相关文章

  1. 使用 Python 编写脚本并发布

    使用 Python 编写脚本并发布 P1: 脚本 通常在 Linux 服务器上会遇到在命令行中输入命令的操作,而有些操作包含的命令数目较多或者其中的命令包含的参数较多,如果一个一个的敲命令的话就太麻烦 ...

  2. 【MonkeyRunner】[技术博客]用python编写脚本查看设备信息

    [MonkeyRunner]用python编写脚本查看设备信息 原以为是个简单的操作,在实践的时候发现了一些问题. python脚本 test.py: from com.android.monkeyr ...

  3. python编写脚本应用实例

    这里主要记录工作中应用python编写脚本的实例.由于shell脚本操作数据库(增.删.改.查)并不是十分直观方便,故这里采用python监控mysql状态,然后将状态保存到数据库中,供前台页面进行调 ...

  4. Python 通过脚本获取Android的apk的部分属性,再通过加密算法生成秘钥。

    Python 通过脚本获取Android的apk的部分属性,再通过加密算法生成秘钥. #!/usr/bin/env python # -*- coding: utf- -*- import os im ...

  5. 使用python编写量子线路打印的简单项目,并使用Sphinx自动化生成API文档

    技术背景 该文章一方面从量子线路的打印着手,介绍了一个简单的python量子线路工程.同时基于这个简单的小工程,我们顺带的介绍了python的API文档自动化生成工具Sphinx的基本使用方法. 量子 ...

  6. Python爬虫之用脚本登录Github并查看信息

    前言分析目标网站的登录方式 目标地址:https://github.com/login     登录方式做出分析: 第一,用form表单方式提交信息, 第二,有csrf_token, 第三 ,是以po ...

  7. python编写脚本,删除固定用户下的所有表

    脚本如下: [oracle@ycr python]$ more t_del.py #/usr/bin/python#coding:utf8 import sysimport cx_Oracle i=0 ...

  8. 用python编写简易登录接口

    需求: 让用户输入用户名密码 认证成功后显示欢迎信息 输错三次后退出程序 可以支持多个用户登录 用户3次认证失败后,退出程序,再次启动程序尝试登陆时,还是锁定状态 下面是我写的代码,如果有BUG或者不 ...

  9. Zabbix调用外部脚本发送邮件:python编写脚本

    Zabbix调用外部脚本发送邮件的时候,会在命令行传入两个参数,第一个参数就是要发送给哪个邮箱地址,第二个参数就是邮件信息,为了保证可以传入多个参数,所以假设有多个参数传入 #!/usr/bin/en ...

随机推荐

  1. Dapr + .NET Core实战(六)绑定

    什么是绑定 处理外部事件或调用外部接口的功能就是绑定,绑定可以提供以下好处: 避免连接到消息系统 ( 如队列和消息总线 ) 并进行轮询的复杂性 聚焦于业务逻辑,而不是如何与系统交互 使代码不受 SDK ...

  2. 最详细的搭建web自动化测试网站,别再说你没有实战项目(文未有福利)

    一步步教你搭建开源网站 环境准备: Tomcat shopping商城文件 jdk环境 Mysql环境 解压shopping.rar拷贝至tomcat/webapps 在navicat导入数据库db_ ...

  3. dbus中的数据类型

    DBus中也是类似于静态语言,使用了"强类型"数据格式.在DBus上传递的所有数据都需要声明其对应的类型,下面整理了下,DBus中的数据类型,以及在DBus中声明的数据类型是什么意 ...

  4. Unity——可复用背包工具

    Unity可复用背包工具 Demo展示 设计思路 游戏中有非常多的背包样式,比如玩家道具背包,商城,装备栏,技能栏等:每个形式的背包都单独写一份逻辑会非常繁琐,所以需要有一套好用的背包工具: 这些背包 ...

  5. react之组件生命周期

    四个阶段 初始化 运行中 销毁 错误处理(16.3以后) 初始化 constructor static getDerivedStateFromProps() componentWillMount() ...

  6. Jmeter使用问题记录

    Jmeter下载安装,设置中文,返回值乱码处理,下载接口测试 下载地址 解压后,在Jmeter的bin文件夹下启动 修改默认启动为中文简体:打开bin目录下的jmeter.properties文件,在 ...

  7. WEB 标准以及 W3C 的理解与认识

    01.  WEB标准 ①  web标准 简单来说可以分为结构.表现和行为. ②  结构:主要是有HTML标签组成(通俗点说,在页面body里面我们写入的标签都是为了页面的结构)   表现:即指css样 ...

  8. C#开发BIMFACE系列48 Nginx部署并加载离线数据包

    BIMFACE二次开发系列目录     [已更新最新开发文章,点击查看详细] 在前一篇博客<C#开发BIMFACE系列47 IIS部署并加载离线数据包>中详细介绍了IIS部署并访问的完整步 ...

  9. 用NXOpen.CAM.CAMSetup.CopyObjects复制刻字操作

    复制刻字操作 手动时,报粘贴对象失败: 用代码执行,报内部错误: Dim destinationObject As NXOpen.CAM.CAMObject = CType(NXOpen.Utilit ...

  10. PG集群(PostgreSql环境)搭建

    centos PG集群搭建 一.安装PG 1.安装之前首先查看软件是否已经安装 rpm -qa | grep postgresql #若存在,需要卸载使用 yum remove postgresql ...