Skip to content

Commit 8c3daf0

Browse files
committed
fixed user data, added pub ips, colors
1 parent 5fe5e0a commit 8c3daf0

File tree

1 file changed

+64
-40
lines changed

1 file changed

+64
-40
lines changed

dew.py

100644100755
Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
# list available images: python3 dew.py -i
1212
# show help: python3 dew.py -h
1313
#
14-
# TODO user data is iffy and needs testing
1514
##################################################
1615
import sys
1716
import time
1817
import requests
1918
import json
20-
19+
cRed = '\033[31m'
20+
cOrange = '\033[33m'
21+
cPurple = '\033[35m'
22+
cCyan = '\033[36m'
23+
cReset = '\033[0m'
2124
# api_token - accounts > api > generate new token
2225
# ssh_keys - settings > security > SSH Keys
2326
# tags - use to automatically assign firewalls, etc
@@ -29,13 +32,12 @@
2932
droplet_name = 'pwnbox' # default if you leave input blank
3033
droplet_size = 's-1vcpu-1gb' #cheapest $5 droplet
3134
droplet_image = 'debian-10-x64' # 'ubuntu-20-04-x64'
32-
tag = 'pwn'
35+
tag = 'tagname' # for more than one tag use the format: tag = 'tag1","tag2' maybe i'll fix this
3336
region = 'tor1'
3437
user_data = '' # this is populated in create_droplet()
3538
auto_script = '''#!/bin/bash
36-
3739
apt-get -y update
38-
apt-get -y install nmap wget curl tmux
40+
apt-get -y install nmap wget curl tmux htop
3941
touch /root/donezo
4042
'''
4143
# ^^^ change that stuff ^^^
@@ -45,77 +47,96 @@
4547
'Authorization': 'Bearer ' + api_token,
4648
}
4749

48-
data = '{ \
49-
"name":"' + droplet_name + '", \
50-
"region":"' + region + '", \
51-
"size":"' + droplet_size + '", \
52-
"image":"' + droplet_image + '", \
53-
"ssh_keys":["' + ssh_key_fingerprint + '"], \
54-
"backups":false, \
55-
"ipv6":false, \
56-
"user_data":"' + user_data + '", \
57-
"private_networking":null, \
58-
"volumes": null, \
59-
"tags":["' + tag + '"] \
60-
}'
61-
6250

6351
def create_droplet():
52+
print()
6453
global droplet_name
6554
name_drop = input('enter droplet name (default is ' + droplet_name + ') => ')
6655
if name_drop != '':
6756
droplet_name = name_drop
6857
do_auto = input('include automated script (may not work)? (y or n) => ')
69-
if do_auto == 'n':
70-
user_data = ''
71-
elif do_auto == 'y':
58+
if do_auto == 'y':
7259
user_data = auto_script
60+
elif do_auto == 'n':
61+
user_data = ''
7362
else:
7463
print('that was not y or n')
7564
sys.exit()
65+
data = '{ \
66+
"name":"' + droplet_name + '", \
67+
"region":"' + region + '", \
68+
"size":"' + droplet_size + '", \
69+
"image":"' + droplet_image + '", \
70+
"ssh_keys":["' + ssh_key_fingerprint + '"], \
71+
"backups":false, \
72+
"ipv6":false, \
73+
"user_data":"' + user_data + '", \
74+
"private_networking":null, \
75+
"volumes": null, \
76+
"tags":["' + tag + '"] \
77+
}'
78+
7679
response = requests.post('https://api.digitalocean.com/v2/droplets', headers=headers, data=data)
7780
jresp = response.json()
7881
droplet_id = jresp['droplet']['id']
7982
print('will generate droplet then sleep before getting IP...')
8083
print()
81-
print('droplet id: ' + str(droplet_id))
84+
print('droplet id: ' + cCyan + str(droplet_id) + cReset)
8285
time.sleep(10)
8386
response = requests.get('https://api.digitalocean.com/v2/droplets/' + str(droplet_id) + '/', headers=headers)
8487
jresp = response.json()
85-
ip1 = jresp['droplet']['networks']['v4'][0]['ip_address'] # TODO make this better
86-
iptype1 = jresp['droplet']['networks']['v4'][0]['type']
87-
ip2 = jresp['droplet']['networks']['v4'][1]['ip_address']
88-
iptype2 = jresp['droplet']['networks']['v4'][1]['type']
89-
if iptype1 == 'public':
90-
print('public ip: ' + str(ip1))
91-
elif iptype2 == 'public':
92-
print('public ip: ' + str(ip2))
93-
else:
94-
print('ERROR: no public ip? printing all ip4 info')
88+
pub_ip = 'none'
89+
priv_ip = 'none'
90+
for i in range(len(jresp['droplet']['networks']['v4'])):
91+
if jresp['droplet']['networks']['v4'][i]['type'] == 'public':
92+
pub_ip = jresp['droplet']['networks']['v4'][i]['ip_address']
93+
if jresp['droplet']['networks']['v4'][i]['type'] == 'private':
94+
priv_ip = jresp['droplet']['networks']['v4'][i]['ip_address']
95+
if pub_ip == '' or priv_ip == '':
96+
print(cRed + 'ERROR: ' + cReset + 'ip error, printing all ip info:')
9597
print(jresp['droplet']['networks']['v4'])
98+
else:
99+
print('public ip: ' + cOrange + pub_ip + cReset)
100+
print('private ip: ' + cPurple + priv_ip + cReset)
101+
print('tags: ' + str(jresp['droplet']['tags']))
102+
print()
96103

97104

98105
def del_droplet(drop_id):
106+
print()
99107
if int(drop_id) in safe_droplets:
100108
print('you dont want to delete that one!')
101109
else:
102110
response = requests.delete('https://api.digitalocean.com/v2/droplets/' + drop_id, headers=headers)
103111
if response.status_code == 204:
104-
print('droplet deleted')
112+
print(cCyan + 'droplet deleted' + cReset)
105113
else:
106-
print('ERROR: something went wrong!')
114+
print(cRed + 'ERROR: ' + cReset + 'something went wrong!')
115+
print()
107116

108117

109118
def list_droplets():
119+
print()
110120
response = requests.get('https://api.digitalocean.com/v2/droplets', headers=headers)
111121
jresp = response.json()
112-
ipaddr = 'error: public ip not found'
113-
print('name\t\tid\t\tip\n----------\t----------\t--------------')
122+
print('name\t\tid\t\t\tpublic ip\t\tprivate ip\n----------\t-----------------\t--------------\t\t--------------')
114123
for d in jresp['droplets']:
124+
if d['id'] in safe_droplets:
125+
safe_color = cRed
126+
safe_string = ' (safe)'
127+
else:
128+
safe_color = cCyan
129+
safe_string = '\t'
130+
pub_ip = 'none'
131+
priv_ip = 'none'
115132
for i in range(len(d['networks']['v4'])):
116133
if d['networks']['v4'][i]['type'] == 'public':
117-
ipaddr = d['networks']['v4'][i]['ip_address']
118-
print(d['name'] + '\t\t' + str(d['id']) + '\t' + str(ipaddr))
134+
pub_ip = d['networks']['v4'][i]['ip_address']
135+
if d['networks']['v4'][i]['type'] == 'private':
136+
priv_ip = d['networks']['v4'][i]['ip_address']
137+
print(d['name'] + '\t\t' + safe_color + str(d['id']) + safe_string + cReset \
138+
+ '\t' + str(pub_ip) + '\t\t' + str(priv_ip))
139+
print()
119140

120141

121142
def list_images():
@@ -128,6 +149,7 @@ def list_images():
128149
res = [] # remove duplicates
129150
[res.append(x) for x in image_list if x not in res]
130151
print(*res, sep='\n')
152+
print()
131153

132154

133155
def print_help():
@@ -141,6 +163,7 @@ def print_help():
141163
'''
142164
print(help_str)
143165

166+
144167
# start script
145168
if len(sys.argv) > 1:
146169
if sys.argv[1] == '-d':
@@ -164,4 +187,5 @@ def print_help():
164187
elif len(sys.argv) == 1:
165188
print_help()
166189
else:
167-
add_droplet()
190+
print('something went wrong')
191+
sys.exit()

0 commit comments

Comments
 (0)