Python

 

 

 

 

Python - SSH

 

 

NOTE : The Python scrip in this note ran on Windows 10, Python 3.9.1 and the ssh server that the script is connecting to was running on two different types : fedora 33 on a separate PC and Ubuntu 20.04.1 running on a Virtual Box on the same PC.

 

 

 

Installation of paramiko

 

To run the examples in this page, you need to install the package : paramiko as shown below

 

pip install paramiko

 

 

 

Basic Connection

 

File Name : pyTTY.py

import socket

import paramiko

from paramiko import SSHClient

 

#Create a Client socket and connect

pyTTY = SSHClient()

pyTTY.load_system_host_keys()

pyTTY.set_missing_host_key_policy(paramiko.AutoAddPolicy())

pyTTY.connect('192.168.0.51', username='jaeku', password=********')

 

# Execute a command and print the result

stdin, stdout, stderr = pyTTY.exec_command('pwd')

print(stdout.readlines())

 

stdin, stdout, stderr = pyTTY.exec_command('ls')

print(stdout.readlines())

 

 

# Close all the std io

stdin.close()

stdout.close()

stderr.close()

 

# Close the client

pyTTY.close()

Result :

['/home/jaeku\n']

['Desktop\n', 'Documents\n', 'Downloads\n', 'Music\n', 'Pictures\n', 'Public\n', 'Templates\n', 'Videos\n']

 

NOTE : For IPv6 connection, you can set the server IP in the format as shown below.

    pyTTY.connect('2607:fea8:e260:a340:74ad:72ca:da40:815d', username='jaeku', password='********')

 

 

 

Command for change directory

 

For most of commands, you just need to put a string of command as shown in the above example, but in case of any command that use change directory ('cd'), you need to put the command 'cd' and any follow-up commands into a single command string as shown below.

 

File Name : pyTTY.py

import socket

import paramiko

from paramiko import SSHClient

 

#Create a Client socket and connect

pyTTY = SSHClient()

pyTTY.load_system_host_keys()

pyTTY.set_missing_host_key_policy(paramiko.AutoAddPolicy())

pyTTY.connect('192.168.0.51', username='jaeku', password='********')

 

# Execute a command and print the result

stdin, stdout, stderr = pyTTY.exec_command("cd /home/jaeku/Documents && pwd && ls -al")

print(stdout.readlines())

 

# Close all the std io

stdin.close()

stdout.close()

stderr.close()

 

# Close the client

pyTTY.close()

Result :

['/home/jaeku/Documents\n', 'total 16\n', 'drwxr-xr-x  2 jaeku jaeku 4096 Dec 19 15:59 .\n', 'drwxr-xr-x 16 jaeku jaeku 4096 Dec 13 02:36 ..\n', '-rw-r--r--  1 root  root     5 Dec 19 15:59 docs.txt\n', '-rw-r--r--  1 root  root     5 Dec 19 15:59 test.txt\n']

 

 

 

Troubleshoot Tips :

 

In terms of scripting, the examples shown above would work without much difficulties. If you still have issues with the connection, it is likely to be related to sshd setting on server side. My recommendation is to try with PuTTY and check if you can connect to the server (ssh destination) first. If you have any issues with PuTTY due to server side issue, refer to my notes on ssh troubleshooting.