What is the best way to give a Linux command from

2019-09-21 10:20发布

问题:

I have a Django project on my laptop. It works perfectly fine on my machine.

This web app takes input from the user in the form of an image and reads the content in it. If the content is already present on the database, it runs a Linux command on a different machine. Both machines are connected to the same network.

Currently, I am using SSH to connect to the other machine but it asks for password every time. Is there any way I can eliminate the need for entering a password every time I want to run a command?

回答1:

Solution 1: use SSH pre-shared key to login via SSH without a password. See this link for how to do it. After you have configured it properly, you are able to run a command on your server:

ssh hostname-or-ip-of-the-raspi command arg1 arg2 ...

and will execute command arg1 arg2 ... on the Raspberry PI, without being prompted for a password.

Solution 2: use TCP communication, and write a server for the Raspberry PI and a client for your server. You can use raw sockets, or some high level library such as zmq.



回答2:

I am considering you are a simple intermediate programmer and based on that giving you two solutions with their pros and cons.

Solution 1: Using a simple Flask app on Raspberry PI

You can modify the following code to request a simple app running on PI to perform any actions.

Code:

from flask import Flask
app = Flask(__name__)

@app.route('/runMotor')
def hello_world():
    runMotor()
    # Run any script here
    return "Motor Ran"

You can then use your raspberry to call something like:

<your_rasp_ip>:<port>/runMotor

Pros: Easy to implement, You can even move it further to be used from outside your firewall.

Cons: Slow and would not be suitable for very rapid concurrent request. Concurrency is the downfall (or you can queue the requests and then keep a check for this issue)

Solution 2 Using MQTT: MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport.

You can play with your code, checkout example here

Pros: Extremely lightweight and removes concurrency overhead, MQTT is an asynchronous messaging protocol. This is best used for real time systems.

Cons: MQTT is a very light messaging protocol and cannot support heavy payloads.