Markus
Markus

Reputation: 23

How to open a GUI-Application on a Server (Raspi) via SSH using python/paramiko?

As server I got a Raspberry Pi with a display connected by hdmi. From my Windows 10 pc I want to execute a gui-application on my Pi by ssh, so that the application gets displayed on the Pi's display. The execution should be started by a Python program using paramiko.

If I try the following in the windows-console it works perfectly fine:

But if I try the following in a python script, it runs without error, but no window pops up at the Pi's screen:

import paramiko

hostname = "raspberrypi"
username = "user"
password = "1234"
command = "python3 my_gui_app.py"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:
    client.connect(hostname=hostname, username=username, password=password)

    with client.get_transport().open_session() as session:
        session.get_pty()
        session.request_x11(single_connection=True)
        session.set_environment_variable("DISPLAY", ":0")
        session.exec_command(command)

        # Continuously read the stdout and stderr streams
        while True:
            if session.recv_ready():
                stdout = str(session.recv(4096).decode("utf-8"))
                print(stdout, end="")
            if session.recv_stderr_ready():
                stderr = str(session.recv_stderr(4096).decode("utf-8"))
                print(stderr, end="")

            # Check if the command has finished executing
            if session.exit_status_ready():
                break

finally:
    client.close()

What am I doing wrong?

Upvotes: 0

Views: 329

Answers (1)

Aakhil Shaik
Aakhil Shaik

Reputation: 89

On the server, In /etc/ssh/sshd_config , there is an option called AcceptEnv that allows the SSH client to send environment variables. So when you want to set the environment variable using session.set_environment_variable(), the environment variable must first be allowed on the server using the AcceptEnv.

Use the below command to verify:

grep AcceptEnv /etc/ssh/sshd_config

If DISPLAY variable is not present, add the it and restart sshd service.

If you can not update/edit the sshd_config of server, update your command

command = "export DISPLAY=:0; python3 my_gui_app.py"

Upvotes: 1

Related Questions