How we will connect or SSH to AWS client?
There are many ways to connect or SSH to AWS client, Paramiko is one of them. Below is the sample program with explanation.
In this example, we will SSH to a AWS client and will execute some commands. We need AWS instance details to connect like IP, Username, Key file
import paramiko
from paramiko import SSHClient
# AWS Instance Details
ip = "1.1.1.1"
user_name = "ubuntu"
path = "/Users/chakradhar/exec/key.pem"
#create an object for SSHClient
client = SSHClient()
#Set policy to use when connecting to servers without a known host key
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#Connecting to AWS client
client.connect(hostname=ip, username=user_name, key_filename=path)
#Command to be executed
sdtin, stdout, stderr = client.exec_command("date")
output = stdout.readlines()
print(output)
Output:
python3 paramiko_example.py
['Fri Jul 16 11:04:16 UTC 2021\n']