How to run a python3 script remotely trough ssh - python

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

Related

Calling Python through ssh in Python script causes "Illegal instruction (core dumped)"

I have a Python script call.py to log in to another machine through ssh and execute another Python script saved on this machine, something like this
# call.py
import os
# log into user#host machine and execute python script
os.system("sshpass -p password ssh user#host '( python3 my_script.py )'")
but when I try to run it I get this error
Illegal instruction (core dumped) ( python3 my_script.py )
I tried a few things:
If I log into ssh and launch python3 my_script.py through the terminal it works
I also tried running the same call.py script without the ssh part, so trying to launch my_script.py on the same machine and it works
It seems that only the combination of ssh + python inside python that gives me this error. Any pointers on what could cause the error will be very much appreciated.
Edit: I didn't realize it would play a role, but I'm adding now the important detail that the target machine is an Nvidia Jetson board.
I found the solution for my case, in case it can help someone else. As pointed out by Thomas, the problem was that I was working with different systems, in the specific my target machine was a Jetson while I was running ssh from my laptop.
On my Jetson board I had seen the Illegal instruction (core dumped) when trying to import OpenCV, to which the solution was to export OPENBLAS_CORETYPE=ARMV8 to the ~/.bashrc.
In order to make this work through ssh, I had to modify my command to call python this way:
OPENBLAS_CORETYPE=ARMV8 python3
and now it works!

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

Python Script Won't Run from Aws Ubuntu Terminal

I just set up my first aws server for a personal project I'm doing. I'm running ubuntu linux, and I have a python script that accesses an sqlite database file in order to send email. I have these same files on my own ubuntu machine and the script works fine. I'm having trouble, however, figuring out how to run my script from the terminal in my aws vm. Normally I use idle to run my python script on my linux machine, so I'm trying to figure out how to run it from the terminal and it's giving me some trouble.
I tried
python script.py
which did nothing, so I converted it to an executable, ran it, and got
./script.py: line 1: import: command not found
...and so on
I realized that I had to add
#!/usr/bin/env python3
to my script, so I did that, converted to executable again, and ran it by entering
./script.py
which also did nothing. If the program had run, it would have delivered an email to my inbox. Is there any way I can tell if it's actually trying to run my script? Or am I trying to run it incorrectly?
You can modify the script to add verbose that prints out to the console the status of the script, or if you just want to know whether your script is running in the background, you can check whether the process is active using ps (process name would be the name of the script) :
ps aux | grep "script.py"
Anyways the former is a better practice, since you can exactly know execution flow of your script.

Running python script on local server only shows source code

I have a question regarding a Python running problem.
I set up a local server with the Windows command prompt using the python -m http.server 8000 command, but when I run my script on http://localhost:8000/cgi-bin/hello.py, it just shows me the source code instead of actually running the script.
I use the code i found on this link.
#!/usr/bin/python
print('Content-Type: text/html')
print()
print('<html>')
print('<head><title>Hello from Python</title></head>')
print('<body>')
print('<h2>Hello from Python</h2>')
print('</body></html>')
I've seen several people having the same problem, but I couldn't really find a solution.
I work with Python 3.5
You should follow the documentation. The command for Python 3 is:
python -m http.server --cgi 8000

is there a way to use fabric run() and sudo() at a time?

I'm trying to automate a test in python 2.7 (in eclipse on linux ubuntu 12.04).
The test checks configurations on another pc, so I'm using fabric for the ssh connection.
I need to execute a script:
run("cd somepath && ./execute_script.sh")
The problem is that my script needs a sudo to run, but changing the command to this:
sudo("cd somepath && ./execute_script.sh")
does not work since "cd" doesn't work in combination with sudo.
I also cannot split the command in two parts, because that would create 2 shells, and the second one would forget the path I've been going to in the first one.
If I do it like this:
run("cd somepath && sudo ./execute_script.sh")
the test wouldn't work completely automatic since you would have to enter the password at a time.
Is there a way to some sort of combine run() and sudo()?
How about:
from fabric.api import cd,sudo
with cd('somepath'):
sudo('./execute_script.sh')

Categories

Resources