-
更改OSD通道名称
# coding=utf-8
import os
import time
import requests
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
import xml.etree.ElementTree as ET#和监控摄像头通讯需要一个双方认可的密钥,可以随机生成
def generate_key():# 生成一个16字节的随机字节数组,16字节对应128位random_bytes = os.urandom(16)# 将字节数组转换成十六进制字符串hex_key = random_bytes.hex()return hex_key#格式数据,给摄像头输出def fun_GetIP(url):# 尝试使用Digest Auth登录,不是Basic Authsession = requests.Session()session.auth = HTTPDigestAuth(USERNAME, PASSWORD) # 确保USERNAME和PASSWORD已经被定义while True:try:# 发送GET请求response = session.get(url,timeout=5)# 检查响应状态码if response.status_code == 200:# 将HTML转化为ElementTree对象ip_config = ET.fromstring(response.text) # 使用response.text# 将Element转换为字符串并打印#xml_str = ET.tostring(ip_config, encoding='unicode')#print(xml_str)# 提取命名空间# 注意:命名空间的格式应该是这样的:'{http://www.hikvision.com/ver20/XMLSchema}'ns = {'ns': 'http://www.hikvision.com/ver20/XMLSchema'} # 注意这里命名空间的格式# 查找ipv4的ipaddress# 根据返回的XML结构,可能需要使用ip_config# 并且命名空间需要正确添加到'ipAddress'前面ip_address = ip_config.find('.//ns:ipAddress', ns).textreturn ip_addresselse:print("Request failed with status code:", response.status_code)return Noneexcept requests.exceptions.Timeout:print("Connection timed out. Do you want to retry?")choice = input("输入1重连,输入2退出: ")if choice != '1':print("Returning to main program.")return Noneexcept Exception as e:print("An error occurred:", e)return Nonedef fun_New_IPAddress(url,xml_data,rebootUrl):# 尝试使用Basic Auth登录session = requests.Session()session.auth = HTTPDigestAuth(USERNAME, PASSWORD)try:# 发送GET请求response = session.get(url)# 发送PUT请求,更新IP地址time.sleep(2)headers = {'Content-Type': 'application/xml'}response = session.put(url, data=xml_data, headers=headers)# 检查响应状态码,确认更新是否成功if response.status_code == 200:print("\n IP更改成功!")response1=session.put(rebootUrl,data='', headers=headers)#重启设备print('重启设备------------------------------------------------')else:print(f"Failed to update OSD name. Status code: {response.status_code}")# 检查响应状态if response.status_code == 200:print('建立连接成功!')else:print("建立连接失败:", response.status_code)except Exception as e:print("An error occurred:", e)if __name__=='__main__':#207辅楼楼顶设备间2(外)# 摄像头的IP地址、用户名和密码HOST1=input('请输入ip地址前6位不要少写最后的【点】:默认:10.25.')if not HOST1:HOST1='10.25.'USERNAME = 'admin'PASSWORD = 'qlyy1234'#跳转1:while 1:HOST2 = input('\n请输入原始ip地址最后6位(例如:27.21):\n\n')HOST=HOST1+HOST2asekey=generate_key()#url1:输出格式的地址;url2:输出OSD名字的地址,后边的密钥可以是任意值url1=f'http://{HOST}/ISAPI/System/Video/inputs/channels/1/overlays'url2=f'http://{HOST}/ISAPI/System/Video/inputs/channels/1/?security=1&iv={asekey}'url3=f'http://{HOST}/ISAPI/System/Network/interfaces/1'#更改ip地址url4=f'http://{HOST}/ISAPI/System/Network/interfaces?security=1&iv={asekey}'#获取ip地址rebootUrl=f'http://{HOST}/ISAPI/System/reboot'#获取通道名称OldIPAddress=fun_GetIP(url4)print(f'当前IP地址:{OldIPAddress}')HOST2=input('\n请输入新的地址后6位(例如:31.21)【q:退出】:\n\n')if not HOST2:New_IPAddress=OldIPAddresselif HOST2 == 'q':exitelse:New_IPAddress=HOST1+HOST2xml_data3 = f"""<?xml version: "1.0" encoding="UTF-8"?><NetworkInterface><id>1</id><IPAddress><ipVersion>dual</ipVersion><addressingType>static</addressingType><ipAddress>{New_IPAddress}</ipAddress><subnetMask>255.255.224.0</subnetMask><ipV6AddressingType>ra</ipV6AddressingType><DefaultGateway><ipAddress>10.25.27.254</ipAddress></DefaultGateway><PrimaryDNS><ipAddress>114.114.114.114</ipAddress></PrimaryDNS><SecondaryDNS><ipAddress>8.8.8.8</ipAddress></SecondaryDNS></IPAddress></NetworkInterface>"""#执行摄像头通道名称更改fun_New_IPAddress(url3,xml_data3,rebootUrl)
海康威视更改ip
# coding=utf-8
import os
import time
import requests
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
import xml.etree.ElementTree as ET#和监控摄像头通讯需要一个双方认可的密钥,可以随机生成
def generate_key():# 生成一个16字节的随机字节数组,16字节对应128位random_bytes = os.urandom(16)# 将字节数组转换成十六进制字符串hex_key = random_bytes.hex()return hex_key#格式数据,给摄像头输出def fun_GetIP(url):# 尝试使用Digest Auth登录,不是Basic Authsession = requests.Session()session.auth = HTTPDigestAuth(USERNAME, PASSWORD) # 确保USERNAME和PASSWORD已经被定义while True:try:# 发送GET请求response = session.get(url,timeout=5)# 检查响应状态码if response.status_code == 200:# 将HTML转化为ElementTree对象ip_config = ET.fromstring(response.text) # 使用response.text# 将Element转换为字符串并打印#xml_str = ET.tostring(ip_config, encoding='unicode')#print(xml_str)# 提取命名空间# 注意:命名空间的格式应该是这样的:'{http://www.hikvision.com/ver20/XMLSchema}'ns = {'ns': 'http://www.hikvision.com/ver20/XMLSchema'} # 注意这里命名空间的格式# 查找ipv4的ipaddress# 根据返回的XML结构,可能需要使用ip_config# 并且命名空间需要正确添加到'ipAddress'前面ip_address = ip_config.find('.//ns:ipAddress', ns).textreturn ip_addresselse:print("Request failed with status code:", response.status_code)return Noneexcept requests.exceptions.Timeout:print("Connection timed out. Do you want to retry?")choice = input("输入1重连,输入2退出: ")if choice != '1':print("Returning to main program.")return Noneexcept Exception as e:print("An error occurred:", e)return Nonedef fun_New_IPAddress(url,xml_data,rebootUrl):# 尝试使用Basic Auth登录session = requests.Session()session.auth = HTTPDigestAuth(USERNAME, PASSWORD)try:# 发送GET请求response = session.get(url)# 发送PUT请求,更新IP地址time.sleep(2)headers = {'Content-Type': 'application/xml'}response = session.put(url, data=xml_data, headers=headers)# 检查响应状态码,确认更新是否成功if response.status_code == 200:print("\n IP更改成功!")response1=session.put(rebootUrl,data='', headers=headers)#重启设备print('重启设备------------------------------------------------')else:print(f"Failed to update OSD name. Status code: {response.status_code}")# 检查响应状态if response.status_code == 200:print('建立连接成功!')else:print("建立连接失败:", response.status_code)except Exception as e:print("An error occurred:", e)if __name__=='__main__':#207辅楼楼顶设备间2(外)# 摄像头的IP地址、用户名和密码HOST1=input('请输入ip地址前6位不要少写最后的【点】:默认:10.25.')if not HOST1:HOST1='10.25.'USERNAME = 'admin'PASSWORD = 'qlyy1234'#跳转1:while 1:HOST2 = input('\n请输入原始ip地址最后6位(例如:27.21):\n\n')HOST=HOST1+HOST2asekey=generate_key()#url1:输出格式的地址;url2:输出OSD名字的地址,后边的密钥可以是任意值url1=f'http://{HOST}/ISAPI/System/Video/inputs/channels/1/overlays'url2=f'http://{HOST}/ISAPI/System/Video/inputs/channels/1/?security=1&iv={asekey}'url3=f'http://{HOST}/ISAPI/System/Network/interfaces/1'#更改ip地址url4=f'http://{HOST}/ISAPI/System/Network/interfaces?security=1&iv={asekey}'#获取ip地址rebootUrl=f'http://{HOST}/ISAPI/System/reboot'#获取通道名称OldIPAddress=fun_GetIP(url4)print(f'当前IP地址:{OldIPAddress}')HOST2=input('\n请输入新的地址后6位(例如:31.21)【q:退出】:\n\n')if not HOST2:New_IPAddress=OldIPAddresselif HOST2 == 'q':exitelse:New_IPAddress=HOST1+HOST2xml_data3 = f"""<?xml version: "1.0" encoding="UTF-8"?><NetworkInterface><id>1</id><IPAddress><ipVersion>dual</ipVersion><addressingType>static</addressingType><ipAddress>{New_IPAddress}</ipAddress><subnetMask>255.255.224.0</subnetMask><ipV6AddressingType>ra</ipV6AddressingType><DefaultGateway><ipAddress>10.25.27.254</ipAddress></DefaultGateway><PrimaryDNS><ipAddress>114.114.114.114</ipAddress></PrimaryDNS><SecondaryDNS><ipAddress>8.8.8.8</ipAddress></SecondaryDNS></IPAddress></NetworkInterface>"""#执行摄像头IPaddressfun_New_IPAddress(url3,xml_data3,rebootUrl)