Created
September 6, 2018 14:34
-
-
Save Chandru-B/a7b2e281ae5d2aa4ab77639b424a561c to your computer and use it in GitHub Desktop.
This file contains 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 boto3; | |
from pathlib import Path | |
import base64 | |
from Crypto.Cipher import PKCS1_v1_5 | |
from Crypto.PublicKey import RSA | |
import json; | |
import os; | |
from jinja2 import Environment, FileSystemLoader | |
home = str(Path.home()) | |
pem_file_loc = home+"/.ssh/my-dev.pem" | |
def decrypt(ciphertext, keyfile = pem_file_loc ): | |
input = open(keyfile) | |
key = RSA.importKey(input.read()) | |
input.close() | |
cipher = PKCS1_v1_5.new(key) | |
plaintext = cipher.decrypt(ciphertext, None) | |
return plaintext | |
file_loader = FileSystemLoader('templates') | |
env = Environment(loader=file_loader) | |
linux_template = env.get_template('linuxtemplate.remmina') | |
windows_admin_template = env.get_template('windows.admin.remmina') | |
windows_user_template = env.get_template('windows.user.remmina') | |
session = boto3.session.Session(profile_name='aws-dev-profile') | |
ec2=session.resource('ec2') | |
instances = ec2.instances.filter( | |
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]) | |
my_envs = ["dev","qa", "preprod"] | |
for instance in instances: | |
for tags in instance.tags: | |
if tags["Key"] == "Name": | |
for my_env in my_envs: | |
if (tags["Value"].startswith(my_env+"-") and instance.platform != "windows"): | |
output = linux_template.render(env=my_env,name=tags["Value"],instance=instance) | |
with open(home+"/.local/share/remmina/"+tags["Value"]+".remmina", "w") as fh: | |
fh.write(output) | |
elif (tags["Value"].startswith(my_env + "-") and instance.platform == "windows"): | |
if instance.password_data()['PasswordData'] != "": | |
password = decrypt(base64.b64decode(instance.password_data()['PasswordData'])) | |
output = windows_admin_template.render(password=password,env=my_env, name="admin."+tags["Value"], instance=instance) | |
with open(home + "/.local/share/remmina/admin." + tags["Value"] + ".remmina", "w") as fh: | |
fh.write(output) | |
else: | |
for v in instance.volumes.all(): | |
print(v.id) | |
output = windows_user_template.render(password=".", env=my_env,name="user." + tags["Value"], instance=instance) | |
with open(home + "/.local/share/remmina/user." + tags["Value"] + ".remmina", "w") as fh: | |
fh.write(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script will generate remmina rdp files for aws ec2 instances. Please make sure you have appropriate remmina templates using jinja2 template