Python requires root to run, but Kivy crashes using sudo - python

I've searched online to find answers to this, but I've come up short. Other examples are different enough to not get me to a solution. This is on a Raspberry Pi 3b, Raspbian, Jessie.
I have a kivy app that uses a bluetooth (ble) peripheral device. My BLE class has to scan for BLE devices which requires root privileges. The BLE class works using sudo outside of kivy so I don't 'think' there is a fundamental problem with the BLE code. FYI, the BLE class uses bluepy (btle). In order to get the peripheral working correctly I have to run:
scanner = btle.Scanner()
dev = scanner.scan(3)
The scan requires running as root. If I didn't need it I would remove it, but then the behavior of the program changes.
My problem is that running my program (w/ BLE class AND kivy) from command prompt like this: python3 FS_run.py runs the application w/out connecting to the BLE peripheral. However, when I run it like this: sudo python3 FS_run.py, I get:
Traceback (most recent call last):
File "FS_run.py", line 1, in <module>
from kivy.app import App
ImportError: No module named 'kivy'
I've seen a lot of posts where successfully running kivy w/ sudo makes the buttons not work. I've changed the permissions (chmod) of the BLE class file and tried running again w/out sudo, but that didn't help. I'm open to other suggestions to circumvent the use of sudo. Perhaps running the BLE in a subprocess, but I wouldn't know how to make it join the rest of the program. Also, I'm fairly new to BLE, I just got the BLE class to work yesterday. Suffice it to say I'm a bit out of my league here.
I'm not a linux guy so I am not sure where to focus my energy to solve this. Not sure if this is helpful, but I saw this on another post so I'll just add it:
which python3 gives /usr/bin/python3
sudo which python3 gives /usr/bin/python3
EDIT:
python -c "import sys; print(sys.path)"
prints different output than when run with sudo.
'/home/pi/kivy' is missing when run with sudo. How do I go about fixing this?
EDIT 2:
Other posts said this fixed it:
sudo cp /home/pi/.kivy/config.ini /root/.kivy/config.ini
Didn't work for me. I got:
cp: cannot create regularfile '/root/.kivy/config.ini': No such file or directory
So then I manually created the .kivy directory in root and then did a sudo cp to copy the file over. Still does not work.

I got it to work by adding at the end of the file "/root/profile"
the line :
export PYTHONPATH=/home/pi/Documents/kivy/kivy:$PYTHONPATH
Then before launching the app :
sudo su
source ~/.profile
I don't know if it helped but I have also :
sudo cp /home/pi/.kivy/config.ini /root/.kivy/config.ini

Related

Kali Linux Vscode Python - Operation not permitted run python file with internal terminal

Hy guys, i am trying to use vscode in kali linux.
But I ran into a problem.
The moment I start the python file from the play symbol in the terminal it gives me a permission error.
But if I boot from the terminal using sudo python main.py it works fine.
Would you know how to help me fix this problem?
i am used to launch with play, i find it annoying to type in the terminal every time.
Screen error
I also tried the solution found on the internet of sudo code --user-data-dir = "~ / .vscode-root" my/path, but it doesn't solve the problem.
the code to execute:
import socket
import struct
import binascii
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0800))
while True:
print (s.recvfrom(2048))
Try to use :
sudo code
in the terminal
If it doesn't work try to remove it and install it again.

Executing a Python Script with Metasploit

I am trying to run vulnerability testing for a class and the first line of code in the script is:
from metasploit.msfrpc import MsfRpcClient
However, when I try to run the program, I get "ModuleNotFoundError: No Module named 'metasploit'"
I am running on Kali Linux; metasploit comes standard, but either I am doing something wrong or am missing a module. Has anyone run into this and can maybe suggest a resolution?
first,
pip3 install --user pymetasploit3
$ msfrpcd -P yourpassword -S
and then in python,
from pymetasploit3.msfrpc import MsfRpcClient
It can work. Good luck.

Scrpay - ModuleNotFoundError: No module named 'http.client' [duplicate]

I am working with my Raspberry Pi 2 B+ and I am using Raspbian. I have a python script located at /home/pi/Desktop/control/gpio.py
When I type /home/pi/Desktop/control/gpio.py into the command line, I get the message
bash: /home/pi/Desktop/control/gpio.py Permission denied
I have tried running sudo -s before running that command also but that doesnt work. My python script is using the Rpi.GPIO library.
If someone could please explain why I am getting this error it would be appreciated!
You will get this error because you do not have the execute permission on your file. There are two ways to solve it:
Not executing the file in the first place. By running python gpio.py python will load the file by reading it, so you don't need to have execute permission.
Granting yourself execute permission. You do this by running chmod u+x yourfile.py.
However, doing so will not work unless you add a shebang at the top of your python program. It will let your linux know which interpreter it should start. For instance:
#!/usr/bin/env python
This would try to run python using your current $PATH settings. If you know which python you want, put it here instead.
#!/usr/bin/python3
Remember the shebang must be the very first line of your program.
do like this maybe work:
cd /home/pi/Desktop/control/
python gpio.py
Because gpio.py is not a executable file, you should run it by python instead

Virtual CAN Bus silmulator does not work on Ubuntu

I am trying to replay a log file using the following code. It's a very simple code to read each signal from the file and create the command. It is creating the command correctly. I print it to check and it works fine, but when I use os.system(command) to simulate VCAN it freezes and doesn't show the command on the terminal.
import os
filename = "canLogs.log"
f = open(filename, "r")
...
(Reading logs from file and create "command")
...
print(command)
os.system(command)
I am using Ubuntu 64-bit (vmWare) on my Mac. This code works fine on Raspberry Pi. And I installed the folllowings:
Socketcan and sudo apt-get install can-utils and pip3 install cantools. And I bring the virtual can interface by
sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set vcan0 up
To test the interface, when I put this cansend vcan0 123#1122334455667788 on the command line, doesn't give me any errors. So probably something is wrong with os.system(command), although I checked it by sending other commands. I wonder if I missed isntalling any other libraries in order to use it on Ubuntu (vmWare).
Any help would be great.
Thanks

How to run a python3 script remotely trough ssh

I am trying to run a python3 script remotely trough ssh, first of all i would like to know if this is even possible if the machine i am trying to run the script on doesnt have a python3 interpreter only a 2001 version of python.
And also i am using the following command to run the script , but its not working:
spawn sh -c "ssh -oPort=$port $ip /usr/bin/env < /home/pythonscript
Pythonscript contains a command meant to output the connected COM ports,it is the following:
import serial.tools.list_ports
print([comport.device for comport in serial.tools.list_ports.comport()])
The output that i get from this is a bunch of system information belonging to the machine i am connecting to, stuff like HOSTNAME,USER,MACHTYPE,MAIL,SHELL,OSTYPE
How would i get my intended output from the command that i am executing
All help appreciated
Firstly you will need to install python3 on each server you will be running this on. You will also need to install pip3 and install pyserial. You could also use virtualenv if you want but I'll leave that to you.
Found a small bug in that script. According to the latest version anyway it's "comports" not "comport: https://pyserial.readthedocs.io/en/latest/tools.html
Updated Version:
from serial.tools.list_ports import comports
print([comport.device for comport in comports()])
I was able to run that remotely simply by doing the following
ssh localhost "python3 /home/user/projects/stack_overflow/56900773_remote_python_ssh/pythonscript.py"
Change localhost to whatever server you want and swap my path for the full path to your script.
When I ran that command I just get a blank list, probably because I have no comports :)

Categories

Resources