Python Paramiko module
Introduction:
Figure [1] Raspberry Pi |
A computer by definition is a digital electronic machine that can be programmed to carry out sequences of arithmetic or logical operations (computation) automatically & looks like the image above. As we can see, its not possible to input commands and receive output from the physical device itself so we need some form of input and output device attachments.
This is where a 'computer terminal' comes in, its a device that can be used for entering data into, & transcribing data from a computer. Early computer terminals looked like the image below. You can see some familiar elements from modern day computers such as a keyboard in the image.
Figure [2] Early computer terminal VT100 |
Main Section:
Figure [3] Data center |
SSH:
Figure [4] SSH key based authentication |
ssh <username>@<ip_addr or hostname>
Once we have logged into a remote machine, we can then execute commands on them. We can even transfer files using SFTP or SCIP protocols.
Working with Paramiko to SSH into devices remotely:
Paramiko is a pure-Python implementation of the SSHv2 protocol, providing both client and server functionality. It provides the foundation for the high-level SSH library Fabric, which is what we recommend you use for common client use-cases such as running remote shell commands or transferring files.
To install Paramiko on a machine with Python (3.4+) and Pip (PyPy) installed with all dependencies:
pip install paramiko[all]
Here's an example from linode documentation of how to connect and run commands on a remote system:
import paramiko
# Update the next three lines with your
# server's information
host = "YOUR_IP_ADDRESS"
username = "YOUR_LIMITED_USER_ACCOUNT"
password = "YOUR_PASSWORD"
client = paramiko.client.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, username=username, password=password)
_stdin, _stdout,_stderr = client.exec_command("df")
print(stdout.read().decode())
client.close()
One of the issues that I came across recently was to configure a FortiMail Virtual Machine on which the operation looks like the following:
config system interface
edit <port>
set ip <ip_address> <netmask>
set allowaccess (http https ping ssh telnet)
end
References:
[1] https://en.wikipedia.org/wiki/Computer_terminal
[2] https://en.wikipedia.org/wiki/Time-sharing
[3] https://en.wikipedia.org/wiki/Terminal_emulator
[4] Recommended read -> TTY Demystified - http://www.linusakesson.net/programming/tty/
[5] https://en.wikipedia.org/wiki/Pseudoterminal
[6] https://en.wikipedia.org/wiki/Secure_Shell
[7] https://docs.paramiko.org/en/stable/
[8] https://www.linode.com/docs/guides/use-paramiko-python-to-ssh-into-a-server/
Comments
Post a Comment