Virtual CAN Bus silmulator does not work on Ubuntu - python

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

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.

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 :)

Python requires root to run, but Kivy crashes using sudo

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

Python Pexpect and Check Point Gaia Expert Mode

I administer a few Check Point Firewalls at work that run on the Gaia operating system. Gaia is a hardened, purpose-built Linux OS using the 2.6 kernel.
I am a novice at Python and I need to write a script that will enter "expert mode" from the clish shell. Entering expert mode is similar to invoking su as it gives you root privileges in the BASH shell.
Clish is a Cisco like custom shell made to ease OS configuration changes. I saw a similar discussion at pexpect and ssh: how to format a string of commands after su - root -c, but people responding recommended sudo.
This is not an option for me as sudo is not supported by the OS and if you were to install it, clish would not recognize the command. The goal of my script would be to SSH to the device, login, invoke expert mode, then run grep admin /etc/passwd and date. Again, sudo is not an option.
clish does not support SSH. But you can change the shell of your user to /bin/bash instead of /etc/clish.sh
set user <myuser> shell /bin/bash
save config

How to use python subprocess.check_output with root / sudo

I'm writing a Python script that will run on a Raspberry that will read the temperature from a sensor and log to Thingspeak. I have this working with a bash script but wan't to do it with Python since it will be easier to manipulate and check the read values. The sensor reading is done with a library called loldht. I was trying to do it like this:
from subprocess import STDOUT, check_output
output = check_output("/home/pi/bin/lol_dht22/loldht", timeout=10)
The problem is that I have to run the library with sudo to be able to access the pins. I will run the script as a cron. Is it possible to run this with sudo?
Or could I create a bash script that executes 'sudo loldht' and then run the bash script from python?
I will run the script as a cron. Is it possible to run this with sudo?
You can put python script.py in the cron of a user with sufficient privileges (e.g. root or a user with permissions to files and devices in question)
I don't know which OS you're using, but if Raspbian is close to Debian, there is no need for sudo or root, just use a user with sufficient permissions.
It seems I can also do this check_output check_output(["sudo", "/home/pi/bin/lol_dht22/loldht", "7"], timeout=10)
Sure but the unix user that's going to invoke that Python script will need the sudo privilege (Otherwise can't call the sudo from subprocess). In which case you might as well do as above, run the cron from a user with the required permissions.
You can run sudo commands with cron. Just use sudo crontab -e to set the cron and it should work fine.
You should very careful with running things as root. Since root has access to everything, a simple error can potentially render the system unusable.
The proper way to have access to the hardware as a normal user is to change the permissions on the required device files.
It seems that the utility you mention uses the WiringPi library. Some digging in the source code indicates that it uses the /dev/gpiomem (or /dev/mem) devices.
On raspbian, device permissions are set with udev. See here and also here.
You could give every user access to /dev/gpiomem and other gpio devices by creating a file e.g. /etc/udev/rules.d/local.rules and putting the following text in it:
ACTION=="add", KERNEL=="gpio*", MODE="0666"
ACTION=="add", KERNEL=="i2c-[0-9]*", MODE="0666"
The first line makes the gpio devices available, the second one I2C devices.

Categories

Resources