quatanium / python-onvif

ONVIF Client Implementation in Python
MIT License
474 stars 321 forks source link

Example how to set IPv4Configuration? #10

Open tetherit opened 9 years ago

tetherit commented 9 years ago

Specifically it would be great to have an example for how to enable DHCP. I am trying to do mycam.devicemgmt.IPv4Configuration but it says Method not found. Not quite sure which method to use.

sinchb commented 9 years ago

IPv4Configuration isn't a method defined by onvif. I think this is what you want. BTW, all the operations can find here

tetherit commented 9 years ago

Ah, thank you, I am still struggling though, I tried to do this:

>>> from onvif import ONVIFCamera
>>> mycam = ONVIFCamera('192.168.88.11', 80, 'admin', '', '/usr/local/lib/python2.7/site-packages/etc/onvif/wsdl/')
>>> network_interfaces = mycam.devicemgmt.create_type('SetNetworkInterfaces')
>>> network_interfaces.NetworkInterface.IPv4.DHCP = True
>>> network_interfaces.NetworkInterface.IPv4.DHCP
True
>>> mycam.devicemgmt.SetNetworkInterfaces(network_interfaces)
False
>>> # Hmmm, another try...
>>> network_interfaces = mycam.devicemgmt.GetNetworkInterfaces()
>>> network_interfaces[0].IPv4.Config.DHCP = True
>>> network_interfaces[0].IPv4.Config.DHCP
True
>>> mycam.devicemgmt.SetNetworkInterfaces(network_interfaces[0])
False

Any ideas?

EDIT: Just a note, using this Ruby library, https://github.com/jimxl/ruby-onvif-client, I'm able to change to DHCP. Maybe the camera is buggy but would be good to have a workaround and to be able to use it with this Python library :)

sinchb commented 9 years ago

well, here is an example to set the network interface(page 22).

I think your passed wrong parameter to SetNetworkInterfaces. this will work?

mycam.devicemgmt.SetNetworkInterfaces(network_interfaces)
tetherit commented 9 years ago

Hi, sorry for the late reply, that's the first thing I tried, if I do that I get this output:

>>> from onvif import ONVIFCamera
>>> mycam = ONVIFCamera('192.168.88.11', 80, 'admin', '', '/usr/local/lib/python2.7/site-packages/etc/onvif/wsdl/')
>>> network_interfaces = mycam.devicemgmt.GetNetworkInterfaces()
>>> network_interfaces[0].IPv4.Config.DHCP = True
>>> network_interfaces[0].IPv4.Config.DHCP
True
>>> mycam.devicemgmt.SetNetworkInterfaces(network_interfaces)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/onvif/client.py", line 28, in wrapped
    raise ONVIFError(err)
onvif.exceptions.ONVIFError: Unknown error: Method instance argument after ** must be a mapping, not list```
sinchb commented 9 years ago

I think you missed one parameter: InterfaceToken, look at this and this(page 22)

tetherit commented 9 years ago

Doesn't seem to help:

>>> from onvif import ONVIFCamera
>>> mycam = ONVIFCamera('192.168.88.11', 80, 'admin', '', '/usr/local/lib/python2.7/site-packages/etc/onvif/wsdl/')
>>> network_interfaces = mycam.devicemgmt.GetNetworkInterfaces()
>>> network_interfaces[0]['Info']['Name']
Test
>>> network_interfaces[0].IPv4.Config.DHCP = True
>>> mycam.devicemgmt.SetNetworkInterface('Test',  network_interfaces[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/onvif/client.py", line 177, in __getattr__
    return self.service_wrapper(getattr(self.ws_client.service, name))
  File "/usr/local/lib/python2.7/site-packages/suds/client.py", line 299, in __getattr__
    return getattr(port, name)
  File "/usr/local/lib/python2.7/site-packages/suds/client.py", line 403, in __getattr__
    return getattr(m, name)
  File "/usr/local/lib/python2.7/site-packages/suds/client.py", line 494, in __getattr__
    return self[name]
  File "/usr/local/lib/python2.7/site-packages/suds/client.py", line 507, in __getitem__
    raise MethodNotFound, qn
suds.MethodNotFound: Method not found: 'DeviceService.DevicePort.SetNetworkInterface'
>>>
>>>
>>> mycam.devicemgmt.SetNetworkInterface('Test',  network_interfaces)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/onvif/client.py", line 177, in __getattr__
    return self.service_wrapper(getattr(self.ws_client.service, name))
  File "/usr/local/lib/python2.7/site-packages/suds/client.py", line 299, in __getattr__
    return getattr(port, name)
  File "/usr/local/lib/python2.7/site-packages/suds/client.py", line 403, in __getattr__
    return getattr(m, name)
  File "/usr/local/lib/python2.7/site-packages/suds/client.py", line 494, in __getattr__
    return self[name]
  File "/usr/local/lib/python2.7/site-packages/suds/client.py", line 507, in __getitem__
    raise MethodNotFound, qn
suds.MethodNotFound: Method not found: 'DeviceService.DevicePort.SetNetworkInterface'

Can you provide an example of something that works for you with one of your cameras?

sinchb commented 9 years ago

There is no method named "SetNetworkInterface", It is SetNetworkInterfaces

and are you sure that "Test" is the valid interface token you want to configure?

tetherit commented 9 years ago

That's what network_interfaces[0]['Info']['Name'] returns - according to that document that would be the interface? - it is also the only interface:

>>> network_interfaces
[(NetworkInterface){
   _DeviceEntity = "Token"
   Enabled = True
   Info = 
      (NetworkInterfaceInfo){
         Name = "Test"
         HwAddress = "00:fc:14:0e:00:05"
         MTU = 1500
      }
   Link = 
      (NetworkInterfaceLink){
         AdminSettings = 
            (NetworkInterfaceConnectionSetting){
               AutoNegotiation = True
               Speed = 100
               Duplex = "Full"
            }
         OperSettings = 
            (NetworkInterfaceConnectionSetting){
               AutoNegotiation = True
               Speed = 100
               Duplex = "Full"
            }
         InterfaceType = "0"
      }
   IPv4 = 
      (IPv4NetworkInterface){
         Enabled = True
         Config = 
            (IPv4Configuration){
               DHCP = True
               Manual[] = 
                  (PrefixedIPv4Address){
                     Address = "192.168.88.11"
                     PrefixLength = 23
                  },
               LinkLocal = 
                  (PrefixedIPv4Address){
                     Address = "192.168.88.11"
                     PrefixLength = 23
                  }
               FromDHCP = 
                  (PrefixedIPv4Address){
                     Address = "192.168.88.11"
                     PrefixLength = 23
                  }
            }
      }
 }]

Alright, here's what I see then:

>>> from onvif import ONVIFCamera
>>> mycam = ONVIFCamera('192.168.88.11', 80, 'admin', '', '/usr/local/lib/python2.7/site-packages/etc/onvif/wsdl/')
>>> network_interfaces = mycam.devicemgmt.GetNetworkInterfaces()
>>> network_interfaces[0]['Info']['Name']
Test
>>> network_interfaces[0].IPv4.Config.DHCP = True
>>> mycam.devicemgmt.SetNetworkInterfaces('Test',  network_interfaces[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/onvif/client.py", line 28, in wrapped
    raise ONVIFError(err)
onvif.exceptions.ONVIFError: Unknown error: Method instance argument after ** must be a mapping, not str
>>>
>>>
>>> mycam.devicemgmt.SetNetworkInterfaces('Test',  network_interfaces)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/onvif/client.py", line 28, in wrapped
    raise ONVIFError(err)
onvif.exceptions.ONVIFError: Unknown error: Method instance argument after ** must be a mapping, not str
sinchb commented 9 years ago

well, the way you passed the parameters isn't correct... Please read the onvif doc, and type instance is the recommended.

BTW, I think the value of Token in you configuration is "Token", while "Test" is the value of "info.Name"

tetherit commented 9 years ago

I can't pass params I got from GetNetworkInterfaces?

>>> network_interfaces[0].__class__
<class suds.sudsobject.NetworkInterface at 0x107c72598>
>>> 
>>> params = mycam.devicemgmt.create_type('SetNetworkInterfaces')
>>> params.__class__
<class suds.sudsobject.SetNetworkInterfaces at 0x10884f460>

Is this different from a type instance?

Also, 'Token' doesn't seem to work either:

>>>  network_interfaces = mycam.devicemgmt.GetNetworkInterfaces()
>>> mycam.devicemgmt.SetNetworkInterfaces('Token',  network_interfaces)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/onvif/client.py", line 28, in wrapped
    raise ONVIFError(err)
onvif.exceptions.ONVIFError: Unknown error: Method instance argument after ** must be a mapping, not str
>>> mycam.devicemgmt.SetNetworkInterfaces('Token',  network_interfaces[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/onvif/client.py", line 28, in wrapped
    raise ONVIFError(err)
onvif.exceptions.ONVIFError: Unknown error: Method instance argument after ** must be a mapping, not str

Do you have a working example of setting networking?

tetherit commented 9 years ago

OK, I tried to use a type instance, also a no go :(

>>> from onvif import ONVIFCamera
>>> mycam = ONVIFCamera('192.168.88.11', 80, 'admin', '', '/usr/local/lib/python2.7/site-packages/etc/onvif/wsdl/')
>>> params = mycam.devicemgmt.create_type('SetNetworkInterfaces')
>>> params.InterfaceToken = "Token"
>>> params.NetworkInterface.Enabled = True
>>> params.NetworkInterface.IPv4.Enabled = True
>>> params.NetworkInterface.IPv4.DHCP = True
>>> mycam.devicemgmt.SetNetworkInterfaces(params)
False
>>> params.InterfaceToken = "Test"
>>> mycam.devicemgmt.SetNetworkInterfaces(params)
False

Any ideas? - anyway to get more output than just "False"?

sinchb commented 9 years ago

ehhh... I think your program worked. False is the response of SetNetworkInterfaes from camera. It indicates whether or not a reboot is required after configuration updates. see the response definition of SetNetworkInterfaces

tetherit commented 9 years ago

Unfortunately not :(

screen shot 2015-07-28 at 11 08 35

No change after reboot either.

sinchb commented 9 years ago

really strange... well, sorry for that. I will test on my camera tomorrow, and then i'll tell you the result.

tetherit commented 9 years ago

Thank you :)

sinchb commented 9 years ago

Here is my result, everything is OK....

In [14]: mycam.devicemgmt.GetNetworkInterfaces()
Out[14]:
[(NetworkInterface){
    _token = "eth0"
    Enabled = True
    Info =
       (NetworkInterfaceInfo){
          Name = "eth0"
          HwAddress = "c4:2f:90:13:11:15"
          MTU = 1500
       }
    Link =
       (NetworkInterfaceLink){
          AdminSettings =
             (NetworkInterfaceConnectionSetting){
                AutoNegotiation = True
                Speed = 100
                Duplex = "Full"
             }
          OperSettings =
             (NetworkInterfaceConnectionSetting){
                AutoNegotiation = True
                Speed = 100
                Duplex = "Full"
             }
          InterfaceType = "0"
       }
    IPv4 =
       (IPv4NetworkInterface){
          Enabled = True
          Config =
             (IPv4Configuration){
                Manual[] =
                   (PrefixedIPv4Address){
                      Address = "192.168.100.173"
                      PrefixLength = 24
                   },
                DHCP = False
             }
       }
    IPv6 =
       (IPv6NetworkInterface){
          Enabled = True
          Config =
             (IPv6Configuration){
                AcceptRouterAdvert = False
                DHCP = "Off"
                LinkLocal[] =
                   (PrefixedIPv6Address){
                      Address = "fe80::c62f:90ff:fe13:1115"
                      PrefixLength = 64
                   },
                FromDHCP[] =
                   (PrefixedIPv6Address){
                      Address = "fe80::c62f:90ff:fe13:1115"
                      PrefixLength = 64
                   },
             }
       }
  }, (NetworkInterface){
    _token = "wlan0"
    Enabled = False
    Info =
       (NetworkInterfaceInfo){
          Name = "wlan0"
          HwAddress = "8c:18:d9:62:ad:4c"
          MTU = 1500
       }
    IPv4 =
       (IPv4NetworkInterface){
          Enabled = True
          Config =
             (IPv4Configuration){
                FromDHCP =
                   (PrefixedIPv4Address){
                      Address = "169.254.38.101"
                      PrefixLength = 16
                   }
                DHCP = True
             }
       }
  }]

In [15]: params = mycam.devicemgmt.create_type('SetNetworkInterfaces')

In [16]: params.InterfaceToken='eth0'

In [17]: params.NetworkInterface.Enabled=True

In [18]: params.NetworkInterface.IPv4.Enabled = True

In [19]: params.NetworkInterface.IPv4.DHCP = True

In [20]: print mycam.devicemgmt.SetNetworkInterfaces(params)
True

In [21]: mycam.devicemgmt.GetNetworkInterfaces()
Out[21]:
[(NetworkInterface){
    _token = "eth0"
    Enabled = True
    Info =
       (NetworkInterfaceInfo){
          Name = "eth0"
          HwAddress = "c4:2f:90:13:11:15"
          MTU = 1500
       }
    Link =
       (NetworkInterfaceLink){
          AdminSettings =
             (NetworkInterfaceConnectionSetting){
                AutoNegotiation = True
                Speed = 100
                Duplex = "Full"
             }
          OperSettings =
             (NetworkInterfaceConnectionSetting){
                AutoNegotiation = True
                Speed = 100
                Duplex = "Full"
             }
          InterfaceType = "0"
       }
    IPv4 =
       (IPv4NetworkInterface){
          Enabled = True
          Config =
             (IPv4Configuration){
                FromDHCP =
                   (PrefixedIPv4Address){
                      Address = "192.168.100.173"
                      PrefixLength = 24
                   }
                DHCP = True
             }
       }
    IPv6 =
       (IPv6NetworkInterface){
          Enabled = True
          Config =
             (IPv6Configuration){
                AcceptRouterAdvert = False
                DHCP = "Off"
                LinkLocal[] =
                   (PrefixedIPv6Address){
                      Address = "fe80::c62f:90ff:fe13:1115"
                      PrefixLength = 64
                   },
                FromDHCP[] =
                   (PrefixedIPv6Address){
                      Address = "fe80::c62f:90ff:fe13:1115"
                      PrefixLength = 64
                   },
             }
       }
  }, (NetworkInterface){
    _token = "wlan0"
    Enabled = False
    Info =
       (NetworkInterfaceInfo){
          Name = "wlan0"
          HwAddress = "8c:18:d9:62:ad:4c"
          MTU = 1500
       }
    IPv4 =
       (IPv4NetworkInterface){
          Enabled = True
          Config =
             (IPv4Configuration){
                FromDHCP =
                   (PrefixedIPv4Address){
                      Address = "169.254.38.101"
                      PrefixLength = 16
                   }
                DHCP = True
             }
       }
  }]

what is the camera brand? Maybe you can reset your camera and try again.

tetherit commented 8 years ago

This is a Chinese no brand camera, so it is possibly faulty, but the ruby library works, so not sure what it is doing that is different. I tried the above basing it on your example and I get this:

>>> mycam = ONVIFCamera('192.168.88.11', 80, 'admin', '', '/usr/local/wsdl')
>>> mycam.devicemgmt.GetNetworkInterfaces()
WARNING:suds.umx.typed:attribute (DeviceEntity) type, not-found
[(NetworkInterface){
   _DeviceEntity = "Token"
   Enabled = True
   Info = 
      (NetworkInterfaceInfo){
         Name = "Test"
         HwAddress = "00:fc:14:0e:00:05"
         MTU = 1500
      }
   Link = 
      (NetworkInterfaceLink){
         AdminSettings = 
            (NetworkInterfaceConnectionSetting){
               AutoNegotiation = True
               Speed = 100
               Duplex = "Full"
            }
         OperSettings = 
            (NetworkInterfaceConnectionSetting){
               AutoNegotiation = True
               Speed = 100
               Duplex = "Full"
            }
         InterfaceType = "0"
      }
   IPv4 = 
      (IPv4NetworkInterface){
         Enabled = True
         Config = 
            (IPv4Configuration){
               DHCP = False
               Manual[] = 
                  (PrefixedIPv4Address){
                     Address = "192.168.88.11"
                     PrefixLength = 23
                  },
               LinkLocal = 
                  (PrefixedIPv4Address){
                     Address = "192.168.88.11"
                     PrefixLength = 23
                  }
               FromDHCP = 
                  (PrefixedIPv4Address){
                     Address = "192.168.88.11"
                     PrefixLength = 23
                  }
            }
      }
 }]
>>> interfaces = mycam.devicemgmt.GetNetworkInterfaces()
WARNING:suds.umx.typed:attribute (DeviceEntity) type, not-found
>>> params = mycam.devicemgmt.create_type('SetNetworkInterfaces')
>>> params.InterfaceToken='Test'
>>> params.NetworkInterface.Enabled=True
>>> params.NetworkInterface.IPv4.Enabled = True
>>> params.NetworkInterface.IPv4.DHCP = True
>>> print mycam.devicemgmt.SetNetworkInterfaces(params)
False
>>> mycam.devicemgmt.GetNetworkInterfaces()
WARNING:suds.umx.typed:attribute (DeviceEntity) type, not-found
[(NetworkInterface){
   _DeviceEntity = "Token"
   Enabled = True
   Info = 
      (NetworkInterfaceInfo){
         Name = "Test"
         HwAddress = "00:fc:14:0e:00:05"
         MTU = 1500
      }
   Link = 
      (NetworkInterfaceLink){
         AdminSettings = 
            (NetworkInterfaceConnectionSetting){
               AutoNegotiation = True
               Speed = 100
               Duplex = "Full"
            }
         OperSettings = 
            (NetworkInterfaceConnectionSetting){
               AutoNegotiation = True
               Speed = 100
               Duplex = "Full"
            }
         InterfaceType = "0"
      }
   IPv4 = 
      (IPv4NetworkInterface){
         Enabled = True
         Config = 
            (IPv4Configuration){
               DHCP = False
               Manual[] = 
                  (PrefixedIPv4Address){
                     Address = "192.168.88.11"
                     PrefixLength = 23
                  },
               LinkLocal = 
                  (PrefixedIPv4Address){
                     Address = "192.168.88.11"
                     PrefixLength = 23
                  }
               FromDHCP = 
                  (PrefixedIPv4Address){
                     Address = "192.168.88.11"
                     PrefixLength = 23
                  }
            }
      }
 }]
molobrakos commented 7 years ago

(Responding to old thread) This works for me to change the default IP from 192.168.1.10 to 192.168.1.20 ("Chinese camera" from AliExpress):

mgmt.SetNetworkInterfaces(dict(
    InterfaceToken='eth0',     
    NetworkInterface=dict(     
        IPv4=dict(
            Enabled=True,
            Manual=[dict(
                Address='192.168.1.20',
                PrefixLength=24)],
            DHCP=False)))))

mgmt.GetDeviceInformation() returns

(reply){
   Manufacturer = "H264"
   Model = "50H20L_S39"
   FirmwareVersion = "V4.02.R11.00002520.10010.244000..ONVIF 2.4"
   SerialNumber = "2b874578dfb749b9"
   HardwareId = "00001"
 }