-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabases.py
More file actions
39 lines (31 loc) · 1.21 KB
/
databases.py
File metadata and controls
39 lines (31 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import boto3
ec2 = boto3.resource('ec2')
rds = boto3.client('rds')
from pick import pick
import base64
import os
import hashlib
def get_db_connection(instance):
dbName = instance.get('DBName', 'XXX')
if instance['Engine'] == 'postgres':
connect_db = 'psql -h ' + instance['Endpoint']['Address'] + ' -p ' + str(instance['Endpoint']['Port']) + ' -d ' + dbName + ' -U ' + instance['MasterUsername'] + ' -W '
elif instance['Engine'] == 'mysqldb':
connect_db = 'mysql -h ' + instance['Endpoint']['Address'] + ' -P ' + str(instance['Endpoint']['Port']) + ' -d ' + dbName + ' -u ' + instance['MasterUsername'] + ' -W '
else:
connect_db = 'XXX'
return connect_db
instances = rds.describe_db_instances()
ssh_vms = []
vms = []
dbs = instances['DBInstances']
for instance in dbs:
dbName = instance.get('DBName', 'XXX')
connect_db = get_db_connection(instance)
c = instance['DBInstanceIdentifier'] + ' > ' + connect_db
if connect_db != 'XXX':
ssh_vms.append(instance['DBInstanceIdentifier'] + ' > ' + connect_db)
vms.append(connect_db)
print(c)
# ssh_, index = pick(ssh_vms, "Select database you want to connect...")
#print(vms[index])
# os.system(vms[index])