zoukankan      html  css  js  c++  java
  • python编写脚本,登录Github通过指定仓库指定敏感关键字搜索自动化截图生成文件【完美截图】

    前言:为了避免开发人员将敏感信息写入文件传到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
    

     

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

    自动化生成的文件如下:

    指定的仓库

    指定仓库搜索的关键字

     自动截图

     

  • 相关阅读:
    影响stm32仿真的因素
    DMA 如何查看它有没有传输完成 传输完成再开启
    keil出错总结
    一个例子讲解wav头文件 stm32声音程序 录音和播放 wav
    前期绑定 vs 后期绑定
    抽象类 VS 接口 (3)
    抽象类 VS 接口(1)
    封装之--JAVA中的访问修饰符(区别于.NET)
    【DP专题】——洛谷P1156垃圾陷阱
    MySql
  • 原文地址:https://www.cnblogs.com/Jack-cx/p/9429422.html
Copyright © 2011-2022 走看看