Created
June 25, 2018 14:05
-
-
Save joenorton8014/5f0d1b3bffc1c5a11bcdda091fcd8270 to your computer and use it in GitHub Desktop.
Digital Ocean Notes
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 digitalocean | |
from digitalocean import SSHKey | |
from Crypto.PublicKey import RSA | |
from datetime import datetime | |
import time | |
import smtplib | |
from email.MIMEMultipart import MIMEMultipart | |
from email.MIMEText import MIMEText | |
import os | |
from digitalocean import SSHKey | |
# pip install -U python-digitalocean | |
# https://github.com/koalalorenzo/python-digitalocean | |
# Todo List: | |
# DONE: | |
# function to list all droplets | |
# function to list all firewalls | |
# function to list all ssh keys | |
# generate SSH Keys for new droplet | |
# save SSH Keys in a defined location | |
# Not done: | |
# function for create new droplet | |
# function for adding a droplet to a firewall | |
# function for create new firewall | |
# ssh to new droplet and install ptf | |
# ... | |
######################################################### | |
# Standard stuff not related to DO | |
######################################################### | |
def Get_Time_Stamp(): | |
day = time.strftime("%Y%m%d_") | |
clock = time.strftime("%I%M%S") | |
timestamp = day + clock | |
return timestamp | |
# Send an email with the search results: | |
def Send_Email(search_results,search_subject): | |
day = time.strftime("%Y%m%d_") | |
clock = time.strftime("%I%M%S") | |
timestamp = day+clock | |
# create message object | |
msg = MIMEMultipart() | |
# fill in all the normal email parts | |
msg['Subject'] = "Splunk Alert!: " + search_subject | |
msg['From'] = '' | |
msg['To'] = '' | |
SERVER = '' | |
gmail_user = '' | |
gmail_password = '' | |
body = "" | |
body += search_results | |
msg.attach(MIMEText(body)) | |
server = smtplib.SMTP_SSL(SERVER) | |
server.ehlo() | |
server.login(gmail_user , gmail_password) | |
server.sendmail(msg['From'], msg['To'], msg.as_string()) | |
server.quit() | |
######################################################### | |
# SSH Key creation and manipulation | |
######################################################### | |
def Create_SSH_Private_Key(ssh_pass_code): | |
key = RSA.generate(2048) | |
privatekey = key.exportKey(passphrase=ssh_pass_code, pkcs=8) | |
publickey = key.publickey().exportKey('OpenSSH') | |
ssh_keys = [privatekey,publickey] | |
print("New SSH keys created!") | |
return ssh_keys | |
def Write_SSH_Keys(ssh_key_folder,ssh_keys,timestamp): | |
privatekey = ssh_keys[0] | |
publickey = ssh_keys[1] | |
if os.path.exists(ssh_key_folder) == False: | |
try: | |
os.makedirs(ssh_key_folder) | |
print("Folder didn't exist so I created it!") | |
except OSError, err: | |
if err.errno != errno.EEXIST or not os.path.isdir(newdir): | |
raise | |
private_key_name = ssh_key_folder + "/" + "id_rsa" + "-" + str(timestamp) | |
public_key_name = ssh_key_folder + "/" + "id_rsa" + "-" + str(timestamp) + ".pub" | |
priv = open(private_key_name , "w") | |
priv.write(privatekey) | |
priv.close() | |
pub = open(public_key_name , "w") | |
pub.write(publickey) | |
pub.close() | |
ssh_keys_written = [private_key_name,public_key_name] | |
return ssh_keys_written | |
######################################################### | |
# DO creation | |
######################################################### | |
def Upload_SSH_Key(ssh_keys_written,api_key): | |
key = SSHKey(token=api_key, name='uniquehostname', public_key= open(ssh_keys_written[1]).read()) | |
key.create() | |
def Create_Droplet(api_key,droplet_name,available_region): | |
droplet = digitalocean.Droplet(token=api_key, | |
name=droplet_name, | |
region= available_region, | |
image='ubuntu-14-04-x64', # Ubuntu 14.04 x64 | |
size_slug='512mb', # 512MB | |
backups=False) | |
droplet.create() | |
actions = droplet.get_actions() | |
for action in actions: | |
action.load() | |
# Once it shows complete, droplet is up and running | |
print action.status | |
######################################################### | |
# DO creation | |
######################################################### | |
def Delete_SSH_Key(api_key,ssh_key_id): | |
key = SSHKey(token=api_key,id=ssh_key_id) | |
try: | |
key.destroy() | |
print "Key successful deleted" | |
except: | |
print "There was an error" | |
######################################################### | |
# DO Listing | |
######################################################### | |
def List_All_VMs(manager): | |
droplet_list = manager.get_all_droplets() | |
for vm in droplet_list: | |
print '\n' | |
print 'Name: ' + vm.name | |
print 'IP Address: ' + str(vm.ip_address) | |
print 'OS: ' + str(vm.image['distribution']) + ' ' + str(vm.image['name']) | |
print 'Creation Time: ' + vm.created_at | |
print 'Disk Size: ' + str(vm.disk) | |
print 'Memory: ' + str(vm.memory) | |
print 'VCPUs: ' + str(vm.vcpus) | |
print 'Region: ' + vm.region['name'] | |
print 'SSH Keys: ' + str(vm.ssh_keys) | |
def List_All_Firewalls(manager): | |
firewall_list = manager.get_all_firewalls() | |
for firewall in firewall_list: | |
print("Firewall Name: " + firewall.name) | |
print("\n" + | |
"Outbound Rules: " | |
+ "\n") | |
for fwrule in firewall.outbound_rules: | |
print("protcol: " + fwrule.protocol + " ports: " + fwrule.ports + " destinations: " + str(fwrule.destinations.addresses[0])) | |
print("\n" + | |
"Inbound Rules: " | |
+ "\n") | |
for fwrule in firewall.inbound_rules: | |
print("protcol: " + fwrule.protocol + " ports: " + fwrule.ports + " sources: " + str(fwrule.sources.addresses[0])) | |
def List_All_SSHKeys(manager): | |
keys = manager.get_all_sshkeys() | |
for key in keys: | |
print("SSH Name: " + key.name) | |
print("SSH Key ID : " + str(key.id)) | |
print(key.__dict__.items()[0][1] + "\n") | |
def List_Available_Regions(manager): | |
regions = manager.get_all_regions() | |
available_regions_list = [] | |
for location in regions: | |
if location.__dict__['available'] == True: | |
available_regions_list.append(location.__dict__['slug']) | |
return available_regions_list | |
######################################################### | |
# main() | |
######################################################### | |
timestamp = Get_Time_Stamp() | |
api_key = '' | |
ssh_pass_code = "supersecret" | |
ssh_key_folder = "/home/joe/" | |
host_name = "testvm" | |
# DO test stuff: | |
manager = digitalocean.Manager(token=api_key) | |
my_droplets = manager.get_all_droplets() | |
my_firewalls = manager.get_all_firewalls() | |
print(my_droplets) | |
# SSH Key Creation Testing: | |
print("DO keys before test: \n\n") | |
List_All_SSHKeys(manager) | |
# Create new key pair: | |
ssh_keys = Create_SSH_Private_Key(ssh_pass_code) | |
# write key pair to disk, return key pair in list: | |
ssh_keys_written = Write_SSH_Keys(ssh_key_folder,ssh_keys,timestamp) | |
# Upload key pair to DO: | |
Upload_SSH_Key(ssh_keys_written,key) | |
# List all keys again | |
print("DO keys after test: \n\n") | |
List_All_SSHKeys(manager) | |
# Create a list of available regions for creating VMs: | |
available_regions_list = List_Available_Regions(manager) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment