I have a simple python script on a Raspberry Pi connected using WiFi that activates an LED. Simple enough.
Is it possible to launch that python script from another device attached to the same network using a simple button in VB.net? Thanks in advance for your help!
You will need to use system call in VB, which run the following command in your PC:
ssh user#rpi:/path/to/command.py arg1 arg2
see this, How do I call a Windows shell command using VB6?.
You will need to run ssh server on the rpi, and possibly set passwordless ssh access using keys.
For ssh on windows, you need either cygwin or putty.
Related
I'm making an rpi based terminal in python and I want to run a powershell command on my computer. How can I send a command to a usb device
You could run socat on your Windows PC to read from serial and execute whatever you receive - if you like big security holes😉 Try adding socat tag to attract the right folk if that's an option.
Or you could run a Python script that sits in a loop reading from serial and then using subprocess.run() to execute the commands it receives.
So, I want to be able to write Python code in my Visual Studio Code on my Windows PC. On my network is a raspberry pi 4, which I would like to execute said code on and receive any errors or output from.
Is it possible for me to write some python code on my Windows PC, "run" it on the Raspberry pi, and receive any outputs of the program on my Windows PC?
The reason I wish to do this is that Visual Studio Code generally helps me write any code, and it is more time consuming for me to use other IDE's, and my code uses PyBluez, something I can't just test on my Windows PC (which has no Bluetooth module)
I hope my question is in the right format and such! This is my first time posting! Any comments appreciated!
Yes you can do that, but it might not be very straight forward. In order to achieve this, you need your Raspberry Pi to be on the same network as your Windows PC (i.e. on the same WiFi network or connected via Ethernet). Then you need to get the IP address of your Raspberry Pi through the following command:-
ifconfig -a
The IP address will be of the following format: W.X.Y.Z
Now from your Windows PC, you can send your python script/scripts through the following command from cmd:-
scp script.py pi#W.X.Y.Z:/home/
And then you can access your Raspberry Pi and run the program by sshing into it through the following commands from cmd:-
ssh pi#W.X.Y.Z
You'll need to enter the Raspberry Pi's password for both commands above, but after that you should have your script on your Pi and you should be able to run it there from your Windows PC.
The links below have more verbose explanation:-
https://www.raspberrypi.org/documentation/remote-access/ip-address.md
https://www.raspberrypi.org/documentation/remote-access/ssh/scp.md
https://www.raspberrypi.org/documentation/remote-access/ssh/
I hope this helps.
It seems that my answer was to use the Remote Development pack on Visual Studio Code (it's an extension) to ssh into my raspberry pi. It's worked well for me for the past few days, and I highly recommend it. It allowed me to access the entire sd card and access any files I need to, while also giving me an SSH terminal and run the program ON the other machine.
For anyone who does this; set up the whole ssh key thing, to stop having to give the password to the pi so often.
The scp command would also work, I think, but is more complex than what I want to do.
Thank you so much for the answer, JL Peyret!
My question regards SSH on Raspberry Pi.
I am able to successfully ssh on to the Pi using the command:
sudo ssh pi#<ipaddress>
and then entering the password.
Let's say I have a Python script file on the Pi that I execute over SSH. Let's say the script reads:
import time
while True:
print('Hello')
time.sleep(1)
This will print 'Hello' every second whilst the terminal/command prompt window is still open (that is, the computer I am using to access the Pi is running and the SSH session remains open). If I close the connection, then the code will stop being executed on the Pi.
Is there a way I can use SSH to keep the code running on the Pi even when I close the window running SSH on the computer I am using to access the Pi? As in the Pi will keep printing 'Hello' even after I shut down my computer. Maybe by entering a command to open a terminal window on the Pi itself and running the script in that terminal window?
Is there a way this can be done?
Thanks
There are two options I can think of:
create a cron job. This method is usually used to execute scripts/programs repeatedly. The job is triggered by the cron program, so it doesn't matter whether or not you are connected to the Pi, as long as it runs. You just have to connect once and setup the job (typically using crontab -e).
use screen (on Wikipedia) or tmux (on Wikipedia). Those are called terminal multiplexers, and allow you to keep shells (and thus any script/program) running although you aren't connected. Note that, in this case, you will have to manually start your script each time, so this solution is well-suited to scripts that run for a long time but are not started too often.
I am looking for a way to open command prompt of remote PC by using Python. In this process I came only upto open a command prompt from Python and other code is connecting to a windows remote PC but how to combine these two and opening the command prompt of remote PC ?
Simply, I want to send some arguments/commands to remote PC command prompt.
Thank you.
First you need to install SSH on the remote PC's. You test the connection and make sure you are able to login. Google can help if you've not setup ssh before.
Then you can create a python script locally to execute commands over the ssh connection.
You'll probably want to look at either
Fabric -> http://docs.fabfile.org/en/1.8/
or
Paramiko -> http://www.paramiko.org/
Which will be able to run your commands.
I'm connected to a Raspberry Pi via ssh. I'm trying to run a script on the RPi but rather than using a nano editor on the terminal I want to execute a python script based on my mac (the server of the SSH).
However when typing
'sudo python /Users/User/Pythonscript.py'
terminal returns
'python: can't open file '/Users/User/Pythonscript.py': [Errno 2] No such file or directory'
And yet this file does exist under that directory.
Any ideas?
Ok, if I have properly understood, you have script on your client and want to execute it on Pi from ssh.
scp /path/to/script.py user#hostname:/path/to/
then add your rsa key to your server. This perfect guide tells you how.
Then just write .sh, which get access to your server ssh user#hostname, then cd /path/to and, finally python script.py
You have a file on your Mac and want to execute it on your Pi. Two things need to be done: First, get the file to the Pi. Second log in to the Pi and run it. Apparently, you managed step two, so I'll address step 1.
The simple solution: scp, e.g. scp /Users/User/Pythonscript.py <user>#<ip_of_pi>:<target_dir>
The solution that might be longer-term more feasible, if you want to develop locally: sshfs. It's available via Homebrew. You mount a directory locally, and anything you change will automatically be reflected on the respective directory on the Pi. Here's a tutorial how to install and use sshfs. On first glance, it seems reasonable.
No matter how you get your script to the Pi, you need to find it on the Pi and execute it there.