fbpixel
Tags:

The SSH (Secure Socket Shell) protocol is widely used to connect to a remote server, or machine, connected to a network. It lets you exchange files, create, modify or run scripts on remote machines.

SSH is usually installed as standard in Linux or Windows distributions.

SSH protocol description

SSH (Secure Shell) is a cryptographic communication protocol used to establish secure, confidential connections between two computer systems over a network. Its principle is based on the use of asymmetric and symmetric encryption methods to protect data in transit between systems. User authentication is generally carried out using cryptographic key pairs, comprising a public key and a private key, ensuring secure verification of the user’s identity. SSH’s main utility lies in its role of securing remote access to servers, transferring secure files and managing systems remotely, offering robust protection against potential threats, including man-in-the-middle attacks and interception of sensitive data.

Open an SSH session

To connect to a device

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ssh <host>@<serveur>
ssh <host>@<serveur>
ssh <host>@<serveur>

example for a raspberry pi

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ssh pi@192.168.1.2
ssh pi@192.168.1.2
ssh pi@192.168.1.2

Once the connection has been established, the monitor will ask for the password to access the remote terminal.

raspberry-pi-ssh-connexion The SSH protocol for remote connections

If your remote machine supports it, the -X tag can be used to display your machine’s windows.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ssh -X <host>@<serveur>
ssh -X <host>@<serveur>
ssh -X <host>@<serveur>

Copy a remote file using the SSH protocol

La commande à utiliser pour copier un fichier d’une machine distante à une autre est la commande scp

scp <source> <destination>

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
scp root@192.168.1.33:script.py C:\Users\ADMIN
scp root@192.168.1.33:script.py C:\Users\ADMIN
scp root@192.168.1.33:script.py C:\Users\ADMIN

A password will be requested to validate the copy.

ssh-copy-distant-file The SSH protocol for remote connections

Login with password

On Linux, you can install and use SSH

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo apt-get install sshpass
sudo apt-get install sshpass
sudo apt-get install sshpass
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sshpass -p {password} ssh {user}@{ipaddress}
sshpass -p {password} ssh {user}@{ipaddress}
sshpass -p {password} ssh {user}@{ipaddress}

On Windows, Putty can take care of password management.

Install PuTTy, then add the executable folder to the Path environment variable (C:\Program Files\PuTTY).

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
putty -ssh "root@192.168.1.32" -pw root #pour ouvrir une connexion en directe
putty -ssh "root@192.168.1.32" -pw root #pour ouvrir une connexion en directe
putty -ssh "root@192.168.1.32" -pw root #pour ouvrir une connexion en directe

To send live orders, we use plink

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
plink -ssh <username>@<host> -pw <password> <command>
plink -ssh <username>@<host> -pw <password> <command>
plink -ssh <username>@<host> -pw <password> <command>

Example

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
plink -ssh root@192.168.1.32 -pw root uname -a
plink -ssh root@192.168.1.32 -pw root uname -a
plink -ssh root@192.168.1.32 -pw root uname -a
ssh-cmd-passord-putty-plink The SSH protocol for remote connections

Using Python to send SSH commands

We use the subprocess library to execute terminal commands

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import subprocess
subprocess.Popen(f"ssh {user}@{host} {cmd}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
import subprocess subprocess.Popen(f"ssh {user}@{host} {cmd}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
import subprocess

subprocess.Popen(f"ssh {user}@{host} {cmd}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

Sending SSH commands requiring a password via Python

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
python3 -m pip install paramiko
python3 -m pip install paramiko
python3 -m pip install paramiko
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ssh = paramiko.SSHClient()
#ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #if missing
ssh.connect(server, username=username, password=password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("uname -a")
print("connected via SSH")
print(ssh_stdout.read().decode())
ssh.close()
ssh = paramiko.SSHClient() #ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #if missing ssh.connect(server, username=username, password=password) ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("uname -a") print("connected via SSH") print(ssh_stdout.read().decode()) ssh.close()
ssh = paramiko.SSHClient()
#ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #if missing
ssh.connect(server, username=username, password=password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("uname -a")
print("connected via SSH")
print(ssh_stdout.read().decode())
ssh.close()
ssh-connexion-paramiko The SSH protocol for remote connections

Applications

Sources