Loading...
X

ONVIF client with command line interface

ONVIF is an open industry forum that provides and promotes standardized interfaces for effective interoperability of IP-based physical security products.

ONVIF protocol can be found on security cameras. Using ONVIF protocol, you can receive information from IP cameras and control them (set settings, rotate, etc.).

This article will talk about python-onvif - an ONVIF client implemented in Python. This program can be considered the successor to foscam-python-lib - Foscam Python Library for H.264 IP Cameras (FI9821W/P/HD816W/P).

This instruction will show working examples of using ONVIF on the command line.

To install python-onvif run:

sudo pip3 install --upgrade onvif_zeep

If you get an error that the pip3 command was not found, then you need to install the python3-pip package (for Debian based distributions) or python-pip (for Arch Linux based distributions).

You can find various examples of using python-onvif (in Python code, in scripts, in interactive mode, etc.) on the program home page: https://github.com/quatanium/python-onvif. The documentation is not informative, and due to the fact that you need to specify the wsdl directory, the path to which by default differs from that used in the examples, and also differs from distribution to distribution, all examples turn out to be inoperative.

Therefore, I propose to consider the use of python-onvif using an example of working code.

Create an extractor.py file with the following content:

import sys
from onvif import ONVIFCamera

# check if a user has been specified to connect. If not, use an empty string as the username
if len(sys.argv) < 4:
	user = ''
else:
	user = sys.argv[3]

# check if a password was specified for the connection. If not, use an empty string as a password
if len(sys.argv) < 5:
	password = ''
else:
	password = sys.argv[4] 		

# we create an object indicating the host, port, user, password and path to wsdl
mycam = ONVIFCamera(sys.argv[1], sys.argv[2], user, password, '/usr/lib/python3.9/site-packages/wsdl/')

# request and display information about the device
resp = mycam.devicemgmt.GetDeviceInformation()
print (str(resp))

# request and display information about network interfaces
resp = mycam.devicemgmt.GetNetworkInterfaces()
print (str(resp))

# request the address of the media stream
media_service = mycam.create_media_service()
profiles = media_service.GetProfiles()
token = profiles[0].token
mycam = media_service.create_type('GetStreamUri')
mycam.ProfileToken = token
mycam.StreamSetup = {'Stream': 'RTP-Unicast', 'Transport': {'Protocol': 'RTSP'}}
print(media_service.GetStreamUri(mycam))

Notice the line

/usr/local/lib/python3.9/site-packages/wsdl/

You need to replace it with your own value. This line is fine for Kali Linux. And on BlackArch/Arch Linux you need to use the following line:

/usr/lib/python3.9/site-packages/wsdl/

When the Python version changes, the line may change as well.

The path to it can be found by the following algorithm:

sudo updatedb # Update DB with file information
locate accesscontrol.wsdl # We are looking for a file that is in the desired directory

For example found:

/usr/local/lib/python3.9/site-packages/wsdl/accesscontrol.wsdl

We take the entire line, except for the file name, that is, /usr/local/lib/python3.9/site-packages/wsdl/.

Run like this:

python3 extractor.py HOST PORT [USER_NAME] [PASSWORD]

The script makes three separate requests and displays three groups of data: device information, network interfaces, and media stream.

USER_NAME and PASSWORD are optional - if they are absent, an attempt will be made to connect with empty credentials.

Launch example:

python3 extractor.py 118.39.210.69 80

Output example:

{
    'Manufacturer': 'BOSCH',
    'Model': 'AUTODOME IP starlight 7000 HD',
    'FirmwareVersion': '25500593',
    'SerialNumber': '044123455',
    'HardwareId': 'F0004D43'
}
[{
    'Enabled': True,
    'Info': {
        'Name': 'Network Interface 1',
        'HwAddress': '00-07-5f-8b-5d-2b',
        'MTU': 1514
    },
    'Link': {
        'AdminSettings': {
            'AutoNegotiation': True,
            'Speed': 100,
            'Duplex': 'Full'
        },
        'OperSettings': {
            'AutoNegotiation': True,
            'Speed': 100,
            'Duplex': 'Full'
        },
        'InterfaceType': 6
    },
    'IPv4': {
        'Enabled': True,
        'Config': {
            'Manual': [],
            'LinkLocal': None,
            'FromDHCP': {
                'Address': '118.39.210.69',
                'PrefixLength': 24
            },
            'DHCP': True,
            '_value_1': None,
            '_attr_1': None
        }
    },
    'IPv6': None,
    'Extension': None,
    'token': '1',
    '_attr_1': {
}
}]
{
    'Uri': 'rtsp://118.39.210.69/rtsp_tunnel?h26x=4&line=1&inst=1',
    'InvalidAfterConnect': False,
    'InvalidAfterReboot': True,
    'Timeout': datetime.timedelta(0),
    '_value_1': None,
    '_attr_1': None
}

Manufacturer, MAC address, video URI:

    'Manufacturer': 'BOSCH',
        'HwAddress': '00-07-5f-8b-5d-2b',
    'Uri': 'rtsp://118.39.210.69/rtsp_tunnel?h26x=4&line=1&inst=1',

Now that we know how to create an ONVIFCamera object and perform elementary actions with it, we can return to https://github.com/quatanium/python-onvif for more examples.

Also see:


Leave Your Observation

Your email address will not be published. Required fields are marked *