weixin_39620279
weixin_39620279
2020-11-27 07:56

Cannot use bluepy without sudo

Hi,

I am starting with bluepy and found that I cannot call the scanner without sudo.


$ python3 scanner.py
Traceback (most recent call last):
  File "scanner.py", line 18, in <module>
    scanner.scan(10.0, passive=True)
  File "/home/spachner/.local/lib/python3.6/site-packages/bluepy/btle.py", line 734, in scan
    self.start(passive=passive)
  File "/home/spachner/.local/lib/python3.6/site-packages/bluepy/btle.py", line 672, in start
    self._mgmtCmd("le on")
  File "/home/spachner/.local/lib/python3.6/site-packages/bluepy/btle.py", line 283, in _mgmtCmd
    "Failed to execute mgmt cmd '%s'" % (cmd))
bluepy.btle.BTLEException: Failed to execute mgmt cmd 'le on'
</module>

Calling with sudo works fine:


$ sudo python3 scanner.py
2018-10-03 18:31:28 38:01:95:0a:4f:d8 [(255, 'Manufacturer', '750042040180ae3801950a4fd83a01950a4fd701000000000000')]
2018-10-03 18:31:28 38:01:95:0a:4f:d8 [(255, 'Manufacturer', '750042040180ae3801950a4fd83a01950a4fd701000000000000')]

How avoid using sudo?

Thanks in advance

spachner

该提问来源于开源项目:IanHarvey/bluepy

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

11条回答

  • weixin_39789979 weixin_39789979 4月前

    I have the exact same issue on my Raspberry Pi Zero W with Raspbian Stretch lite (4.14). I tried all possible combinations of setcap (also looking into #190), without any success.

    
    pi:/usr/local/lib/python3.5/dist-packages/bluepy $ sudo setcap 'cap_net_raw,cap_net_admin+eip' bluepy-helper
    pi:/usr/local/lib/python3.5/dist-packages/bluepy $ getcap bluepy-helper
    bluepy-helper = cap_net_admin,cap_net_raw+eip
    
    pi:~/projects/bluepy/bluepy $ python3 scanner.py
    Traceback (most recent call last):
      File "scanner.py", line 18, in <module>
        scanner.scan(10.0, passive=True)
      File "/home/pi/.local/lib/python3.5/site-packages/bluepy/btle.py", line 852, in scan
        self.start(passive=passive)
      File "/home/pi/.local/lib/python3.5/site-packages/bluepy/btle.py", line 790, in start
        self._mgmtCmd("le on")
      File "/home/pi/.local/lib/python3.5/site-packages/bluepy/btle.py", line 312, in _mgmtCmd
        raise BTLEManagementError("Failed to execute management command '%s'" % (cmd), rsp)
    bluepy.btle.BTLEManagementError: Failed to execute management command 'le on' (code: 20, error: Permission Denied)
    </module>

    The strange thing is: after setting the capabilities, I was able to execute 'le on' manually without using sudo:

    
    pi:/usr/local/lib/python3.5/dist-packages/bluepy $ sudo setcap '' bluepy-helper
    pi:/usr/local/lib/python3.5/dist-packages/bluepy $ ./bluepy-helper 0
    # bluepy-helper.c version 1.3.0 built at 12:54:59 on Dec  3 2018
    le on
    rsp=$mgmtcode=$mgmterrestat=h14emsg='Permission Denied
    quit
    pi:/usr/local/lib/python3.5/dist-packages/bluepy $ sudo setcap 'cap_net_raw,cap_net_admin+eip' bluepy-helper
    pi:/usr/local/lib/python3.5/dist-packages/bluepy $ ./bluepy-helper 0
    # bluepy-helper.c version 1.3.0 built at 12:54:59 on Dec  3 2018
    le on
    rsp=$mgmtcode=$success
    quit
    

    Any ideas what might be going wrong here?

    Thanks a lot! Michael

    点赞 评论 复制链接分享
  • weixin_39574050 weixin_39574050 4月前

    Adding a note here since all other solutions suggestions failed in my case.

    As per https://github.com/noble/noble/issues/682#issuecomment-517513403

    The setcap command fails if your executable node is in an encrypted directory. I.e. if you chose to encrypt your home directory during install and bluepy-helper was located somewhere in home. Reported in that thread to be a bug in encryptfs. If you move the executable outside of the encrypted directory it is purported to work.

    Feel free to follow up with a less-hacky solution if you happen upon one.

    点赞 评论 复制链接分享
  • weixin_39762348 weixin_39762348 4月前

    I have the exact same issue on my Raspberry Pi Zero W with Raspbian Stretch lite (4.14). I tried all possible combinations of setcap (also looking into #190), without any success.

    
    pi:/usr/local/lib/python3.5/dist-packages/bluepy $ sudo setcap 'cap_net_raw,cap_net_admin+eip' bluepy-helper
    pi:/usr/local/lib/python3.5/dist-packages/bluepy $ getcap bluepy-helper
    bluepy-helper = cap_net_admin,cap_net_raw+eip
    <p>pi:~/projects/bluepy/bluepy $ python3 scanner.py
    Traceback (most recent call last):
      File "scanner.py", line 18, in <module>
        scanner.scan(10.0, passive=True)
      File "/home/pi/.local/lib/python3.5/site-packages/bluepy/btle.py", line 852, in scan
        self.start(passive=passive)
      File "/home/pi/.local/lib/python3.5/site-packages/bluepy/btle.py", line 790, in start
        self._mgmtCmd("le on")
      File "/home/pi/.local/lib/python3.5/site-packages/bluepy/btle.py", line 312, in _mgmtCmd
        raise BTLEManagementError("Failed to execute management command '%s'" % (cmd), rsp)
    bluepy.btle.BTLEManagementError: Failed to execute management command 'le on' (code: 20, error: Permission Denied)
    </module></p>

    The strange thing is: after setting the capabilities, I was able to execute 'le on' manually without using sudo:

    
    pi:/usr/local/lib/python3.5/dist-packages/bluepy $ sudo setcap '' bluepy-helper
    pi:/usr/local/lib/python3.5/dist-packages/bluepy $ ./bluepy-helper 0
    <h1>bluepy-helper.c version 1.3.0 built at 12:54:59 on Dec  3 2018</h1>
    <p>le on
    rsp=$mgmtcode=$mgmterrestat=h14emsg='Permission Denied
    quit
    pi:/usr/local/lib/python3.5/dist-packages/bluepy $ sudo setcap 'cap_net_raw,cap_net_admin+eip' bluepy-helper
    pi:/usr/local/lib/python3.5/dist-packages/bluepy $ ./bluepy-helper 0</p>
    <h1>bluepy-helper.c version 1.3.0 built at 12:54:59 on Dec  3 2018</h1>
    <p>le on
    rsp=$mgmtcode=$success
    quit
    </p>

    Any ideas what might be going wrong here?

    Thanks a lot! Michael

    Did you find out how to fix this? I'm having exactly the same problem.

    点赞 评论 复制链接分享
  • weixin_39641236 weixin_39641236 4月前

    From the logs it seems that you have two bluepy installations: - Your system-wide installation at /usr/local/lib/python3.5/dist-packages/bluepy - A user-local installation at ~/.local/lib/python3.5/site-packages/bluepy

    It appears that you added the capabilities to the helper-binary of the system-wide installations, but then tried to use the local one, which has its own helper-binary that was not given additional capabilities.

    TL;DR: when using pip install --user or virtualenv, make sure you set the capabilities on the correct helper-binary ;)

    点赞 评论 复制链接分享
  • weixin_39762348 weixin_39762348 4月前

    From the logs it seems that you have two bluepy installations:

    * Your system-wide installation at `/usr/local/lib/python3.5/dist-packages/bluepy`
    
    * A user-local installation at `~/.local/lib/python3.5/site-packages/bluepy`
    

    It appears that you added the capabilities to the helper-binary of the system-wide installations, but then tried to use the local one (which has it's own helper-binary that was not given additional capabilities).

    TL;DR: when using pip install --user or virtualenv, make sure you set the capabilities on the correct helper-binary ;)

    I should ask more questions, because I JUST figured it out myself as your message was plopping up here. That's the second time that happened this week :sweat_smile: So yeah, that was the problem and it's working now.

    点赞 评论 复制链接分享
  • weixin_39641236 weixin_39641236 4月前

    The linux protocol stack needs additional capabilities. If you do not want to run your script as root (or under sudo), you may also give the required capabilities to the bluepy-helper binary, that comes with bluepy. E.g.

    bash
    sudo setcap 'cap_net_raw,cap_net_admin+eip' bluepy-helper
    

    For more details check this stackexchange thread: https://unix.stackexchange.com/a/182559

    点赞 评论 复制链接分享
  • weixin_39937412 weixin_39937412 4月前

    On my Pi (currently Debian Stretch) the above command just gives a very unhelpful

    
      fatal error: Invalid argument
    

    The following two commands do, however, fix it:

    
      sudo setcap cap_net_raw+e  <path>/bluepy-helper
      sudo setcap cap_net_admin+eip  <path>/bluepy-helper
    </path></path>

    Where <PATH> is the place where bluepy-helper is installed; it's easiest to just search for this, e.g.

    
      find /usr/local/lib -name bluepy-helper
    
    点赞 评论 复制链接分享
  • weixin_39620279 weixin_39620279 4月前

    Hi nescio007,

    many thanks for the advice. Works perfect on Ubuntu 17.10. I found bluepy-helper being installed at /home/spachner/.local/lib/python3.6/site-packages/bluepy/bluepy-helper

    regards

    spachner

    点赞 评论 复制链接分享
  • weixin_39613839 weixin_39613839 4月前

    Why don't include the setcap call to the install tool?

    点赞 评论 复制链接分享
  • weixin_38513151 weixin_38513151 2月前

    运行,报错raise BTLEManagementError("Failed to execute management command '%s'" % (cmd), rsp)
    bluepy.btle.BTLEManagementError: Failed to execute management command 'le on' (code: 20, error: Permission Denied)

    请问什么原因?

    点赞 评论 复制链接分享
  • weixin_38513151 weixin_38513151 2月前
    raise BTLEManagementError("Failed to execute management command '%s'" % (cmd), rsp)
    bluepy.btle.BTLEManagementError: Failed to execute management command 'le on' (code: 20, error: Permission Denied)
    

    运行,报上面的错,是什么原因?

     

    点赞 评论 复制链接分享