Python GUI

How To Write A Python Script Remotely Over SSH

ssh remote file work heading image 8182584

When it comes to working in an organization, it’s quite common to write/develop code and deploy applications on your server. Secure Shell (SSH) is the communication protocol to gain access to a whole remote computer/server either on a local network or any server deployed in some other part of the world. PyScripter has a feature to create an SSH Client connection to an SSH server which then allows you to create your Python script remotely. Let’s get into the details of how to work with remote Python scripts and files using the PyScripter Python IDE.

Prerequisites

Two types of SSH servers

  • Local network server
  • Remote server

Working with a Python script on the local network server

Let’s create a GNU/Linux SSH server. The setup I have is a Debian-based OS – PureOS running on my machine. I have a windows virtual machine running in which I installed PyScripter IDE. So the GNU/Linux-based PureOS will serve as an SSH server and the Windows virtual machine will serve as an SSH client in the local network. You can have a setup of two different machines connected to the same LAN or WiFi. Most of the recent operating systems have SSH pre-installed in them. On the GNU/Linux-based OS check whether an SSH server is already running with the following command:

How To A Write Python Script Remotely Using SSH an SSH window

If the server is already running, you’ll get a result similar to the above image where Active: active (running) with green color. If the server is inactive you’ll get a result similar to the below image;

How To A Write Python Script Remotely Using SSH stopping the SSH service

To switch on the server enter the following below command and recheck the status using the above status command.

Now your server should be running. Please check the IP address of your server using the following command;

How To A Write Python Script Remotely Using SSH finding the IP address

The IP address is highlighted in the above image. Also, check the port number on which the SSH server is active using the following command;

ssh port status 4252776

The TCP port number(22) on which SSH is running is highlighted in the above image. We also need the username of your server. You can use the whoami command to check your username.

whoami 5559017

How to connect to the SSH server from your local machine

Now let’s log in to the SSH server from the SSH client. I’ll use the Windows OS in my Virtual Machine as an SSH client. You can use a separate Windows machine that’s connected to the same LAN or Wi-Fi to create an SSH client. We can check whether OpenSSH client is already installed by opening the Settings > Apps > Apps & features > Optional features or otherwise add/install that feature as it’s shown in the below image;

How To A Write Python Script Remotely Using SSH how to set up OpenSSH to edit Python scripts on a remote server

Now, it’s time to login the SSH server as a client from the PyScripter installed Windows machine. We shall utilize the username, ip address and port number of the GNU/Linux Server which we noted down earlier. Please login to the server using the following command;

My IP address will change here as I changed my Wi-Fi Hotspot compared to the above screenshots. Please observe the below image in my case where the IP address is changed from 192.168.0.108 to 192.168.85.1 as compared to the previous screenshot. The steps to log in to the SSH server are mentioned in the below figure;

How To A Write Python Script Remotely Using SSH making the SSH connection

Now, we’re logged into the SSH server computer where we can create, delete and mange files of the server from our SSH Client from the PowerShell. We can also use any other command line tools like git bash to implement the SSH Client on Windows. Here we’re logging in with the server computer’s password which can be compromised. We shall generate a public-private keypair to login into server instead of a password. To generate a SSH key based login pair, we can use the ssh-keygen command as shown in the below figure;

How To A Write Python Script Remotely Using SSH generating the SSH key

We should now copy the public key in the id_rsa.pub file of the current Windows Client to authorized_keys file of the GNU/Linux Server. We shall use the following command to do it;

Observe the below image where “username” and “ip_address” are updated with my respective server details. We can then login to the Server without using any password.

How To A Write Python Script Remotely Using SSH manipulating the publicprivate key pair

How to edit a Python script on a remote server using PyScripter

We’re now set to work with remote files in PyScripter. Please open PyScripter and choose the File > Open Remote File option as shown below;

How To A Write Python Script Remotely Using SSH showing the menu options with the location of the open remote file option

Follow the below mentioned sequence of steps to connect to an SSH server;

How To A Write Python Script Remotely Using SSH how to manage the SSH connection

After adding the Server, we can now open any of the files in the server computer in the PyScripter. We should choose one of the servers from the list of added servers and the path of the file in the server. Use the below image as a guide;

How To A Write Python Script Remotely Using SSH opening the remote script

Let’s check the content of check.py file in the Server computer to cross-check when it’s opened with PyScripter. As you click “OK” in the above image, the remote check.py file will get opened. Please check out the below figure where we open the check.py file with both the terminal and PyScripter.

How To A Write Python Script Remotely Using SSH the remote path

After editing the Python file, save it and it’ll save to Server. Now, you can successfully edit, run and save the file to Server from the PyScripter IDE. Let’s now implement the functionality of accessing the file from a remotely hosted Server.

How to work with the Python script on the remote server

In real-time work structures in an organization, we host an application on a Remote Server. I chose Digital Ocean hosting to create a Remote Server. Digital Ocean generously offered me a referral link using which you can have $100 credit for 2 months. Use the following link:

https://m.do.co/c/1e4060599604

DigitalOcean Referral Badge

Use the above referral link and create an account. Create a Server – “Droplet” as they call it. Choose the basic options to create the server to test working with the remote server files in the PyScripter IDE.

msedge fcookiqv3z 2901620

We need to provide an SSH key when setting up the server for a password less login similar to what we’ve done with local server. Let’s create another SSH key-pair to use with the digital ocean server using the ssh-keygen utility.

How To A Write Python Script Remotely Using SSH powershell commands to do the SSH magic
How To A Write Python Script Remotely Using SSH where to find the authentication key

Finally, create the droplet and Digital Ocean’s droplet service will assign a public IP address to access the created server.

droplet ip 4201631

Let’s now try to access it (server) using PowerShell. Please open the PowerShell as an administrator to add the identity of the above created Server (droplet) to the Client computer. Check the below image for instructions;

How To A Write Python Script Remotely Using SSH the complications of a droplet server

Now we can successfully SSH and connect to the Server computer as you can observe in the below image;

How To A Write Python Script Remotely Using SSH connecting to an Ubuntu instance

How to create a Python script on a remote Linux server using PyScripter

Let’s now create an empty Python file in the Server and write some code in it by opening it using PyScripter IDE. We can use a UNIX command “touch <filename>” to create a new file.

How To A Write Python Script Remotely Using SSH updating the file's dates

Now, open the PyScripter to open the above created Python file in the server. We can access the remote file from the remote server as we’ve accessed the file in the local server. We can list the SSH connection of remote server using its Public IP address similar to that we’ve done with local server. Open ( File > Open Remote File > pyscripter wav8aidngn 2768675 > pyscripter 6vsewllz3f 6710287 ) and then fill the server connection details in the Edit SSH Servers form/window.

How To A Write Python Script Remotely Using SSH more details of the SSH server

Now the file at a given path in the remote server will open and we can work on it in the PyScripter IDE.

How To A Write Python Script Remotely Using SSH executing thecheckpy script

Save the edits using “Ctrl + S” shortcut and the file will be automatically saved in the remote server with changes. To demonstrate the saved changes, let’s check the content of the file in the remote server using the PowerShell connection. We shall use a UNIX based command “cat <filename>” to view the content of the file in the terminal (PowerShell).

How To A Write Python Script Remotely Using SSH a command line shell showing the results of executing a Python script remotely

Summary of the things we learned about how to edit a Python script remotely

PyScripter IDE can become your goto IDE to work with remote files. You can do all the operations like open, create, edit, update and save the remote file right from your comfort of your favourite PyScripter IDE. There are various other features come along with PyScripter IDE to make it as your default IDE to work. We’re currently working Debug extensions adapter using which we can create debuggers for other programming languages. PyScripter can then be used to run other language codes like JavaScript, C language etc. Please download it at: PyScripter – Free Download – Embarcadero and give it a try. If you face any difficulty in following the above steps, please feel free to ask questions in the comments.

About author

I'm a programmer interested in graphics and animation. I love to showcase my understanding of concepts using animations. Other than that, I love to spend my free time in nature.
Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And OpenCV Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And NumPy Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

Leave a Reply

Your email address will not be published. Required fields are marked *