Running Shell Commands Via Text Message
In this article, we will explore how to run shell commands via text message using Python and Flask.
Prerequisites
In order to follow along with this tutorial, you will need to have the following installed on your machine:
- Python
- Flask
- A mobile phone with SMS capabilities
Setting Up a Flask App
First, we need to create a new Flask app. Open your favorite text editor and create a new file called app.py
. In this file, we will define a route that listens for incoming text messages.
from flask import Flask, request
import subprocess
app = Flask(__name__)
@app.route('/sms', methods=['POST'])
def receive_sms():
message = request.form['Body']
result = subprocess.run(message, shell=True, capture_output=True)
return result.stdout
if __name__ == '__main__':
app.run()
Running the App
Save the app.py
file and open a terminal. Navigate to the directory where the file is located and run the following command to start the Flask app:
$ python app.py
Your Flask app is now running and listening for incoming text messages on the route /sms
.
Testing the App
Now that the app is running, you can test it by sending a text message to the phone number associated with the mobile device. The body of the text message should contain the shell command that you want to execute.
Security Considerations
It’s important to note that allowing remote execution of shell commands via text message can pose serious security risks. Always be cautious and make sure that your app has proper authentication and authorization mechanisms in place.
Conclusion
In this article, we learned how to run shell commands via text message using Python and Flask. While this can be a convenient way to remotely execute commands, it’s important to be mindful of the security implications and take appropriate precautions.
I'm loving the background music but the microphone quality ruins it 🙁
You should invest in a lapel mic! Only about $30 to give you professional level audio. Keep up the good work!