模拟登录GAP和FGAP获取cookie

Zss 发表于:

在之后的一些测试中需要使用到脚本来提交一些配置的数据,在提交配置数据前,需要进行登录的操作

所以首先得第一步就是需要模拟登录上设备,登陆成功后就可以获取到合法的cookie,之后使用这个cookie来进行一些合法的操作

两种设备的登录有一点区别,所以定义的一个类,将这两种获取的方式的方法写成静态方法放在类中,每次调用时成功获取到设备的型号后返回cookie值,说明成功登陆了

静态方法在方法定义前加入@staticmethod,使其不需要实例化这个类就可一直使用类里面的这个方法

#coding:utf-8
import requests
import re,sys
reload(sys)
sys.setdefaultencoding('utf8')
requests.packages.urllib3.disable_warnings()

class Get_Cookie():
    #获取设备型号,返回cookie值
    @staticmethod
    def get_gap_cookie(ip):
        try:
            headers1 = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0', }
            url_null = 'https://%s/entrance' % ip
            rsp_null = requests.get(url=url_null, headers=headers1, verify=False)
            mainbody_cookie = rsp_null.headers['Set-Cookie'].strip('; path=/')
            url_mainbody = 'https://%s/login/mainbody' % ip
            headers2 = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0',
                        'cookie': '%s' % mainbody_cookie}
            rsp = requests.get(url_mainbody, headers=headers2, verify=False)
            mytoken = re.findall('\w{32}', rsp.text)
            mytoken = mytoken[0].encode('utf-8')
            login_cookie = rsp.headers['Set-Cookie'].split('; path=/')[1].strip(', ')
            headers3 = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0',
                        'Cookie': login_cookie}
            url_login = 'https://%s/login/login_confirm' % ip
            file_list = {"mytoken": (None, mytoken), "user": (None, 'superman'), "testname": (None, ''),
                         "passwd": (None, '88627d1fe4d5ef9e8b341f0dbf0370b5'),
                         "cerfile": ('', '', 'application/octet-stream')}
            login_rsp = requests.post(url=url_login, headers=headers3, files=file_list, verify=False, allow_redirects=False)
            cookie = re.findall('(?:su_gap_session=).*?(?:;)', login_rsp.headers['Set-Cookie'])[1].strip(';')
        except Exception as e:
            print 'error.msg:%s' % e
        try:
            headers_test = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0','Cookie': cookie}
            rsp = requests.get(url='https://%s/status'%ip, headers=headers_test, verify=False)
            data = re.findall('(?:</p></td>\s*<td>\s*)(.*[^\s])(?:\s*</td>)', rsp.text)
            if 'GAP' in "".join(data):
                print('获取Cookie成功,设备型号为:%s' % data[0])
                return cookie
            else:
                print('获取Cookie失败!!!')
        except Exception as e:
            print 'error.msg:%s' % e
            print('获取Cookie失败!!!')

    @staticmethod
    def get_fgap_cookie(ip):
        try:
            url_null = 'https://%s/' % ip
            rsp_null = requests.get(url=url_null, verify=False)
            mainbody_cookie = rsp_null.headers['Set-Cookie'].strip('; path=/')
            url_mainbody = 'https://%s/login/mainbody' % ip
            headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1',
                       'cookie': '%s' % mainbody_cookie}
            rsp = requests.get(url_mainbody, headers=headers, verify=False)
            mytoken = re.findall('\w{32}', rsp.text)
            mytoken = mytoken[0].encode('utf-8')
            login_cookie = rsp.headers['Set-Cookie'].split('; path=/')[2].strip(', ')
            headers2 = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
                'Cookie': login_cookie
                }
            url_login = 'https://%s/login/login_confirm' % ip
            file_list = {"mytoken": (None, mytoken), "user": (None, 'superman'), "testname": (None, ''),
                         "passwd": (None, '88627d1fe4d5ef9e8b341f0dbf0370b5'),
                         "cerfile": ('', '', 'application/octet-stream')}
            login_rsp = requests.post(url=url_login, headers=headers2, files=file_list, verify=False, allow_redirects=False)
            cookie = 'sufgap_dxsess=%s' % (login_rsp.headers['Set-Cookie'].split('sufgap_dxsess=')[4].strip('; path=/'))
        except Exception as e:
            print 'error.msg:%s' % e
        try:
            headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36','Cookie': cookie}
            rsp = requests.get(url='https://192.168.1.254/status', headers=headers, verify=False)
            data = re.findall('(?:"left">\s*)([^\s]*)(?:\s*</td>)', rsp.text)
            if 'FGAP' in "".join(data):
                print('获取Cookie成功,设备型号为:%s' % data[1])
                return cookie
            else:
                print('获取Cookie失败!!!')
        except Exception as e:
            print 'error.msg:%s' % e
            print('获取Cookie失败!!!')

if __name__ == '__main__':
    cookie = Get_Cookie.get_gap_cookie('192.168.1.254')