Wednesday, 16 October 2019

Execute commands remotely on AWS EC2 linux instance in Python

import paramiko

##Initiate SSH Connection
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(hostname=<hostIp>,port=<port>,username=<username>,key_filename=<keypath>)
print("Connection Successfully Established!")
except paramiko.AuthenticationException:
print("Authentication Failed!")

#Execute Commands
ssh.exec_command("ls -lst")

No comments:

Post a Comment