11
11
# list available images: python3 dew.py -i
12
12
# show help: python3 dew.py -h
13
13
#
14
- # TODO user data is iffy and needs testing
15
14
##################################################
16
15
import sys
17
16
import time
18
17
import requests
19
18
import json
20
-
19
+ cRed = '\033 [31m'
20
+ cOrange = '\033 [33m'
21
+ cPurple = '\033 [35m'
22
+ cCyan = '\033 [36m'
23
+ cReset = '\033 [0m'
21
24
# api_token - accounts > api > generate new token
22
25
# ssh_keys - settings > security > SSH Keys
23
26
# tags - use to automatically assign firewalls, etc
29
32
droplet_name = 'pwnbox' # default if you leave input blank
30
33
droplet_size = 's-1vcpu-1gb' #cheapest $5 droplet
31
34
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
33
36
region = 'tor1'
34
37
user_data = '' # this is populated in create_droplet()
35
38
auto_script = '''#!/bin/bash
36
-
37
39
apt-get -y update
38
- apt-get -y install nmap wget curl tmux
40
+ apt-get -y install nmap wget curl tmux htop
39
41
touch /root/donezo
40
42
'''
41
43
# ^^^ change that stuff ^^^
45
47
'Authorization' : 'Bearer ' + api_token ,
46
48
}
47
49
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
-
62
50
63
51
def create_droplet ():
52
+ print ()
64
53
global droplet_name
65
54
name_drop = input ('enter droplet name (default is ' + droplet_name + ') => ' )
66
55
if name_drop != '' :
67
56
droplet_name = name_drop
68
57
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' :
72
59
user_data = auto_script
60
+ elif do_auto == 'n' :
61
+ user_data = ''
73
62
else :
74
63
print ('that was not y or n' )
75
64
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
+
76
79
response = requests .post ('https://api.digitalocean.com/v2/droplets' , headers = headers , data = data )
77
80
jresp = response .json ()
78
81
droplet_id = jresp ['droplet' ]['id' ]
79
82
print ('will generate droplet then sleep before getting IP...' )
80
83
print ()
81
- print ('droplet id: ' + str (droplet_id ))
84
+ print ('droplet id: ' + cCyan + str (droplet_id ) + cReset )
82
85
time .sleep (10 )
83
86
response = requests .get ('https://api.digitalocean.com/v2/droplets/' + str (droplet_id ) + '/' , headers = headers )
84
87
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:' )
95
97
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 ()
96
103
97
104
98
105
def del_droplet (drop_id ):
106
+ print ()
99
107
if int (drop_id ) in safe_droplets :
100
108
print ('you dont want to delete that one!' )
101
109
else :
102
110
response = requests .delete ('https://api.digitalocean.com/v2/droplets/' + drop_id , headers = headers )
103
111
if response .status_code == 204 :
104
- print ('droplet deleted' )
112
+ print (cCyan + 'droplet deleted' + cReset )
105
113
else :
106
- print ('ERROR: something went wrong!' )
114
+ print (cRed + 'ERROR: ' + cReset + 'something went wrong!' )
115
+ print ()
107
116
108
117
109
118
def list_droplets ():
119
+ print ()
110
120
response = requests .get ('https://api.digitalocean.com/v2/droplets' , headers = headers )
111
121
jresp = response .json ()
112
- ipaddr = 'error: public ip not found'
113
- print ('name\t \t id\t \t ip\n ----------\t ----------\t --------------' )
122
+ print ('name\t \t id\t \t \t public ip\t \t private ip\n ----------\t -----------------\t --------------\t \t --------------' )
114
123
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'
115
132
for i in range (len (d ['networks' ]['v4' ])):
116
133
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 ()
119
140
120
141
121
142
def list_images ():
@@ -128,6 +149,7 @@ def list_images():
128
149
res = [] # remove duplicates
129
150
[res .append (x ) for x in image_list if x not in res ]
130
151
print (* res , sep = '\n ' )
152
+ print ()
131
153
132
154
133
155
def print_help ():
@@ -141,6 +163,7 @@ def print_help():
141
163
'''
142
164
print (help_str )
143
165
166
+
144
167
# start script
145
168
if len (sys .argv ) > 1 :
146
169
if sys .argv [1 ] == '-d' :
@@ -164,4 +187,5 @@ def print_help():
164
187
elif len (sys .argv ) == 1 :
165
188
print_help ()
166
189
else :
167
- add_droplet ()
190
+ print ('something went wrong' )
191
+ sys .exit ()
0 commit comments