Created
January 14, 2019 09:29
-
-
Save nikkisharma536/acb9b2aff2e548aac60fb91678ccbc53 to your computer and use it in GitHub Desktop.
ETL project - ssh utility function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import paramiko | |
| def execute_remote(key_path, instance_ip, username, cmd_arr): | |
| key = paramiko.RSAKey.from_private_key_file(key_path) | |
| client = paramiko.SSHClient() | |
| client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
| # Connect/ssh to an instance | |
| try: | |
| # Here 'ubuntu' is user name and 'instance_ip' is public IP of EC2 | |
| client.connect(hostname=instance_ip, username=username, pkey=key) | |
| # Execute a command(cmd) after connecting/ssh to an instance | |
| str_cmd = ' '.join(cmd_arr) | |
| print('Executing remote command: %s' % str_cmd) | |
| stdin, stdout, stderr = client.exec_command(str_cmd) | |
| print(stdout.read()) | |
| # close the client connection once the job is done | |
| client.close() | |
| except Exception as e: | |
| print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment