51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2054|回复: 3
打印 上一主题 下一主题

[原创] Python连接SSH的相关操作,当Web测试存在图片验证时怎么办?

[复制链接]
  • TA的每日心情
    慵懒
    2018-1-3 14:03
  • 签到天数: 5 天

    连续签到: 1 天

    [LV.2]测试排长

    跳转到指定楼层
    1#
    发表于 2017-12-21 13:14:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    我们经常做Web自动化测试时,会遇到图片验证登录的尴尬情形,这里提供一种解决思路和方法,对于我们自己公司的产品来说,是可以通过修改配置文件的方式将图片识别功能干掉的,所以我想到了写一个Python脚本,用以实现对服务器(Linux)中的配置文件的相关内容删除,然后进行命令重启的操作,进而实现将图片验证的功能干掉,下面我贴出我的代码,并作出解释


    # coding = utf-8
    import sys,os
    curPath = os.path.abspath(os.path.dirname(__file__))
    rootPath = os.path.split(curPath)[0]
    sys.path.append(rootPath)
    print(sys.path)

    #上传批量文件到远程主机
    import paramiko       #这句话是关键,引入SSH操作类
    import os,re,time
    # 从配置文件读取配置信息
    import configparser
    config = configparser.ConfigParser()
    file_path = os.path.dirname(os.path.abspath('.')) + '/config/config.ini'
    config.read(file_path)

    #从配置文件获取网站地址
    hostname = config.get("testServer", "hostname")
    username = 'root'
    password = '123456'
    port = 22
    local_path = 'F:\jiagou'
    dir_path = '/opt/local/bin/VOS/cur/INM-8086/webapps/INM/WEB-INF/'  #这句话是我们将要操作的远端服务器相关文件所在的路径
    execmd = ["cd /opt/local/bin/VOS/cur/INM-8086/bin/\n","ls\n","./shutdown.sh\n","./startup.sh\n"]    #这个是替换文件后我们将要执行的相关命令,用来重启服务
    # local_dir = '/tmp/'
    # remote_dir = '/tmp/test/'


    class SSH_Connect(object):
        """将文件保存到本地"""
        def save_file(self):
            t = paramiko.Transport((hostname, port))
            t.connect(username=username, password=password)
            sftp = paramiko.SFTPClient.from_transport(t)
            #将SSH路径下的指定文件"web.xml"下载到本地指定路径下
            sftp.get(os.path.join(dir_path,"web.xml"),os.path.join(local_path,"web.xml"))
            new_txt = []
            regex1="<!-- JCaptcha`s filter -->"
           regex2="</web-app>"
           file_object=sftp.open(dir_path+"web.xml",'r+')
            all_the_text = file_object.readlines( )
            for line in all_the_text:                          #此处的循环是为了删除配置文件中的相关内容,搞程序的研究研究就能看懂了,这里不多解释
                if re.search(regex1, line):                    #这里的re.search是一个正则表达式的用法,逐行搜索regex1的内容,找到后返回一个布尔值,这里纯在一个重写文件的过程,重写内容保存在new_txt中
                    new_txt.append("</web-app>")
                    break
                else:
                    new_txt.append(line)
            file_object.seek(0)
            file_object.truncate(0)
            file_object.writelines(new_txt)                   #将new_txt写入新的配置文件
            file_object.close()
            t.close()
        """发送命令重新启动服务"""
        def sshclient_execmd(self):#hostname, port, username, password, execmd):
            s = paramiko.SSHClient()
            s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            s.connect(hostname=hostname, port=port, username=username, password=password)
            ssh=s.invoke_shell()
            for commend in execmd:
                ssh.send(commend)
                out = ssh.recv(1024)
                print(out)
                time.sleep(2)
            ssh.close()
            s.close()
    """后面这部分#sftp.put(os.path.join(local_dir, f), os.path.join(remote_dir, f))这一句可以用来将本地文件上传到远端指定路径下,注意for循环,原本的程序可以将指定路径的所有文件上传到指定路径下的,f即是文件名"""    #        files=sftp.listdir(dir_path)
        #files = t.listdir(dir_path)
        # for f in files:
        #     '#########################################'
        #     'Beginning to upload file %s ' % datetime.datetime.now()
        #     'Uploading file:', os.path.join(local_path, f)

            #sftp.put(os.path.join(local_dir, f), os.path.join(remote_dir, f))
            # 'Upload file success %s ' % datetime.datetime.now()
        # t.close()
    # SSH=SSH_Connect()
    # SSH.save_file()
    # SSH.sshclient_execmd()

    评分

    参与人数 1测试积点 +10 收起 理由
    lsekfe + 10 赞一个!

    查看全部评分

    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-9-21 05:41 , Processed in 0.066702 second(s), 25 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表