Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 16862

Python: Cannot Run Linux Terminal Commands With Scripts [duplicate]

$
0
0

I have two virtual machines interacting with each other. One of them (Linux) communicates through the terminal via OracleVM, and the other (Windows) communicates through PuTTY.

The connection is made by running Python scripts thing1.py and thing2.py respectively, inside folders which are named similarly. The keys and certificates are from AWS, if that is of any importance.As of this moment, they are able to send and receive messages from one another.

I am trying to make them such that the Windows VM could pass commands to the Linux VM instead of just strings. I had partial success being that I am able to run Firefox search by inputting e.g. "command: search instagram". But I have also tried expanding the commands to access the Linux VM local files such as photos and videos, but whenever I try running "ls" on a file that clearly exists and tested on the terminal itself, I keep on getting errors like:

ls: cannot access 'Pictures': No such file or directory

Here is a snippet of my code from thing1.py, let me know if I need to include more information:

path_dict = {"search":"firefox -new-tab -search ","view photo":"imview Pictures/","photos":"Pictures","videos":"Videos","view video":"totem Videos/","list":"ls"}# MESSAGE RECEIVED# Callback when the subscribed topic receives a messagedef on_message_received(topic, payload, **kwargs):    command = payload.decode("utf-8")    if "rcommand" in command:        os.system("cd ~")        to_run = command.replace("rcommand:"," ").strip()        print(f"\nMessage Received: run '{to_run}'\n")        try:            os.popen(to_run)        except FileNotFoundError:            print("Invalid command")    elif "command:" in command:        block = command.split(" ") # split the command sentence to several command blocks        to_run = []        for i in range(1,len(block)):            if path_dict.get(block[i]) == None:                to_run.append(block[i])            else:                to_run.append(path_dict.get(block[i])) # check from the dict which command to run        to_run = " ".join(to_run[:])        #os.system("cd ~")        print(f"\nMessage Received: command: {to_run}\n")        try:            os.popen(to_run)        except FileNotFoundError:            print(f"\nNo executable named '{to_run}' found on this device\n")    else:        print(f"\nMessage Received: {command}\n")    received_all_event.set()

thing2.py only consist of basic IoT communication, so I assume it is not important for me to include it in here or the question will get too long, but let me know otherwise.


Viewing all articles
Browse latest Browse all 16862

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>