Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import gremlin_python
from gremlin_python.rest import ApiException
from pprint import pprint
import ipdb
import webbrowser
from time import sleep
from colors import *
API_KEY = ("0beccc88169060e9dbbd17583e10492dcbb48898de73f1a924481a556ab54419",)
WOW_DOT_GIF ='https://media1.tenor.com/images/c2a921072f98952c52042d6e28c72854/tenor.gif?itemid=9987719'
def pcolor(text, colorname):
print(color(str(text), fg=colorname))
def info(text):
pcolor(text, "blue")
def warn(text):
pcolor(text, "red")
def highlight(text):
pcolor(text, "green")
class GremlinDemo:
def __init__(self, api_key=API_KEY):
self.api_client = gremlin_python.ApiClient(
header_name="Authorization",
header_value="Key 0beccc88169060e9dbbd17583e10492dcbb48898de73f1a924481a556ab54419",
)
self.attacks_client = gremlin_python.AttacksApi(self.api_client)
def start_cpu_attack(self):
command = {"type": "cpu", "args": ["-c", "1", "--length", "10"]}
target = {"type": "Random"}
body = gremlin_python.TaskInput(command=command, target=target)
try:
# Create a new attack.
info("Creating a new attack:")
highlight(f'\t{command}')
highlight(f'\t{target}')
result = self.attacks_client.create(body=body)
resp = self.attacks_client.api_client.last_response.data
resp_clean = str(resp).strip()
attack_url = f'https://app.gremlin.com/attacks/{resp_clean}'
info(
f"Attack created: Let's view it at {attack_url}"
)
webbrowser.open_new_tab(attack_url)
return resp_clean
except ApiException as e:
print("Exception when calling AttacksApi->create: %s\n" % e)
def inspect_cpu_attack(self, guid):
try:
# Get details about an attack.
api_response = self.attacks_client.get(guid)
highlight(api_response)
except ApiException as e:
print("Exception when calling AttacksApi->get: %s\n" % e)
def wait_seconds(n):
warn(f"Sleeping {n} seconds, how exciting!")
sleep(n)
def main():
demo = GremlinDemo()
attack_guid = demo.start_cpu_attack()
wait_seconds(5)
inspection = demo.inspect_cpu_attack(attack_guid)
wait_seconds(3)
webbrowser.open(WOW_DOT_GIF)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment