2022年 11月 9日

python模拟登录统一认证平台代码实例

import requests
import urllib3
import re
import hashlib
import time
import base64
import xlrd
import pickle

ip = 'host ip'
passwd = 'mima'


# 消除ssl报警
urllib3.disable_warnings()


sess = requests.session()
print('开始')
a1 = sess.get('https://' + ip + '/cas/login', verify=False)
print('正在获取登陆参数')
pwd1 = hashlib.sha256(passwd.encode('utf-8')).hexdigest()
vcode = re.findall(r'vcode" value=".+"', a1.text)[0][14:-1]
password = hashlib.sha256((pwd1 + vcode).encode('utf-8')).hexdigest()

# 组装登陆的参数
data = {
    'username': 'admin',
    'password': password,
    'codeId': re.findall(r'codeId" value=".+"', a1.text)[0][15:-1],
    'vcode': vcode,
    'lt': re.findall(r'value="LT-.+"', a1.text)[0][7:-1],
    'execution': 'e1s1',
    'serviceIP': ip,
    'clientIP': '21.37.11.56',
    'clientMAC': '',
    'loginType': '1',
    '_eventId':  'submit',
    'url':  '',
    'pwdStrength': '3'
}

print('单点登陆')
sess.post('https://' + ip + '/cas/login', data=data, verify=False)
# 没有判断登陆状态,知否真的成功了
print('完成登陆,进入首页,获取session,\n等待2秒钟,防止VMS反应不过来')
sess.get('https://' + ip + '/cas/eportal/home.action', verify=False)
time.sleep(2)
print('登陆VMS')
sess.get('https://' + ip + '/vms/index!CMSIndex.action', verify=False)

# 设置组织集合变量
unit = []


# 获取所有组织方法
def get_unit(indexcode, unit_id):
    global unit
    url = 'https://' + ip + '/vms/fetchAllControlUnit!getAsyncTree.action?flag=organize&postCode='
    a3 = sess.get(url + indexcode + '&searchInput=', data={'node': unit_id}, verify=False)
    a4 = a3.json()
    for x in a4:
        # 只获取组织的,过滤监控点的
        if x['id'][:7] == 'control':
            unit.append({
                'id': x['id'].split('_')[1],
                'text': x['text']
            })
            get_unit(x['indexCode'], x['id'])

# 开始获取所有组织
get_unit('0', 'controlUnit_1')
print('组织获取完毕')
print(unit)

# 获取设备信息的URL
get_device_url = 'https://' + ip + '/vms/deviceInfo!fetchDeviceInfoListN.action'

# 定义修改密码的参数
device_info = {}

# 定义ip地址和设备id的对应关系
device_ip = {}


for x in unit:
    print('开始获取 ' + x['text'] + ' 内设备,组织ID:' + x['id'])
    # 分页获取设备信息参数
    page_data = {
        'start': '0',
        'limit': '20',
        'unitId': x['id'],
        'deviceNameLike': ''
    }
    # 获取组织下的设备数量
    a5 = sess.post(get_device_url, data=page_data, verify=False)
    a6 = a5.json()
    # 如果设备数量为0,那么跳过
    if len(a6['items']) == 0:
        pass
        # print('该组织无任何设备。')
    else:
        print('该组织下共有 ' + str(a6['totalCount']) + ' 个设备,正在获取中')
        # 分页获取每一页的设备情况,单线程获取,防止VMS不堪重负
        for z in range(a6['totalPage']):
            # 修改页起始点位索引
            page_data['start'] = str(20 * z)
            # 获取设备参数
            a7 = sess.post(get_device_url, data=page_data, verify=False)
            a8 = a7.json()['items']
            for y in a8:
                # 根据设备id获取indexcode等其他设备信息
                a9 = 'https://' + ip + '/vms/deviceInfo!findDeviceInfoByDeviceId.action'
                a10 = sess.post(a9, data={'deviceId': y['deviceId']}, verify=False).json()
                # 记录设备id对应的设备信息
                device_info[y['deviceId']] = {
                    'addType': '1',
                    'alarmInCount': y['alarmInCount'],
                    'alarmOutCount': y['alarmOutCount'],
                    'cameraChanCount': y['cameraChanCount'],
                    'deviceId': y['deviceId'],
                    'deviceTypeCode': y['typeCode'],
                    'endIndexCode': '',
                    'endNetworkAddr': '',
                    'ipChanCount': y['ipChanCount'],
                    'name': y['name'],
                    'networkAddr': y['networkAddr'],
                    'networkPort': y['networkPort'],
                    'netZoneId': '',
                    'pagServerId': y['pagServerId'],
                    'preIndexCode': '',
                    'startIndexCode': '',
                    'startNetworkAddr': '',
                    'userName': 'admin',
                    'userPwd': a10['userPwd'],
                    'capabilitySet': '0',
                    'indexCode': a10['indexCode'],
                    'policeId': a10['policeId'],
                    'relateUserId': '',
                    'serialNo': a10['serialNo'],
                    'talkChanCount': a10['talkChanCount'],
                }
                # 记录ip地址与设备id直接的关系
                device_ip[y['networkAddr']] = y['deviceId']
print('获取完成。')

device_info_file = open('device_info_file.pkl', 'wb')
pickle.dump(device_info, device_info_file)
device_info_file.close()

device_ip_file = open('device_ip_file.pkl', 'wb')
pickle.dump(device_ip, device_ip_file)
device_ip_file.close()
# print(device_info)
# print(device_ip)

# 打开需要修改密码的表格
xl = xlrd.open_workbook('camera.xlsx')
# 获取第一个表
xl_ip = xl.sheet_by_index(0)
# 获取ip,新密码
ipc_ip = xl_ip.col_values(0)[1:]
ipc_new_passwd = xl_ip.col_values(1)[1:]

# 定义ipc、新密码的集合
ipc_dict = {}

# 将excel中地址密码信息存储到字典 ipc_dict
ipc_count = len(ipc_ip)
for n in range(ipc_count):
    ipc_dict[ipc_ip[n]] = ipc_new_passwd[n]

# 修改密码的接口地址
notify_url = 'https://' + ip + '/vms/deviceInfo!saveModifyDeviceInfoN.action'


# 修改密码方法
def notify_info(ip_addr, new_pwd):
    # 根据IP地址获取设备id
    dev_id = device_ip[ip_addr]
    # 根据设备id获取修改密码的post参数
    post_data = device_info[dev_id]
    # 加密新密码
    post_data['userPwd'] = base64.b64encode(new_pwd.encode('utf-8')).decode('utf-8')
    # 提交新密码至VMS
    a11 = sess.post(notify_url, data=post_data, verify=False)
    print(a11.text)


# 开始修改密码
for n in ipc_dict.keys():
    notify_info(n, ipc_dict[n])

input('修改完成,按任意键退出:')

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195