DEV Community

Cover image for Understanding Local and Remote Hosts Using SSH + SCP + Python HTTP Server
Guy Friley
Guy Friley

Posted on

Understanding Local and Remote Hosts Using SSH + SCP + Python HTTP Server

Overview

Today I've been learning more about Linux, and more specifically the Linux Command Line. I have been learning Linux through a combination of reading The Linux Command Line: A Complete Introduction by William Shotts and using online resources such as KodeKloud and TryHackMe.

I want to share today what I have learned about Secure Shell, Secure Copy, and HTTP Server using Python.

The Task

  1. Login to a remote host using SSH.
  2. Transfer a file using SCP
  3. Host a file in a directory being used as a Web Server using Python's HTTP module.

Login to a Remote Host

At some point while using Linux, and managing a system(s), there will be a need to remote into another machine using the command line. In order to achieve this, we will need to use Secure Shell (ssh).

Before being able to login to a remote host, we need to ensure we have credentials for that remote machine. If we do not, we won't be able to login.

We also need to know the ip address of the remote host.

ssh <remote_host_username>@<ip-address> - This will then prompt us for our password.

Once we have those items, we can login, and it's a simple process. We can do all of the things (as long as we have permissions with the credentials we logged in as) that we can do on our local host.

Transfer a fil using scp

Secure Copy or scp is a way to transfer files/directories from our local host to our remote host and vice versa. It is secure because scp uses the ssh protocol to transfer the files/directory by ensuring that we use authentication, and encryption to send and receive the files.

To send from local host to remote:
scp <file_to_transfer> <username>@<remote_host_ip>:<path>

To receive from remote host to local:

scp <username>@<remote_host_ip>:<path> <path_to_put_file_on_local_host>

Web Server with Python HTTP Server

We can serve files from a host by using the HTTP module from Python.

  1. Select a directory we want to serve files from: cd <path_to_webserver_directory>
  2. Run python3 -m http.server and this will start running our service. Once we do this, it will run in that terminal, so we will need to run a different terminal.
  3. We can then use wget to retrieve files from the remote host. http will run on port 8000, so we need to remember to add the port to the end of our ip address so the command knows what port the http service is running on.

wget <http://ip-address:8000/my_file> and this will retrieve the file and put it in your working directory.

This is just a simple post to update what I have been learning today.

Top comments (0)