Skip to content

Instantly share code, notes, and snippets.

@ZribeDev
Last active April 4, 2024 08:27
Show Gist options
  • Save ZribeDev/de3cd654c4a61be18b0243a6e6ee92b5 to your computer and use it in GitHub Desktop.
Save ZribeDev/de3cd654c4a61be18b0243a6e6ee92b5 to your computer and use it in GitHub Desktop.
Pterodactyl Panel - Remove all servers on a node
# MIT License
#
# Copyright (c) 2024 ZribeDev
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import requests
import json
import time
nodenum = input("Node ID: ")
url = input("URL: ")
apikey = input("API Key (Application): ")
url = f'{url}/api/application/servers'
headers = {
"Authorization": f"Bearer {apikey}",
"Accept": "application/json",
"Content-Type": "application/json"
}
servers_on_node = []
page = 1
while True:
params = {'page': page}
response = requests.get(url, headers=headers, params=params)
data = response.json()
if 'data' in data and len(data['data']) > 0:
servers_on_node.extend([server for server in data['data'] if server['attributes']['node'] == nodenum])
page += 1
else:
break
server_count = len(servers_on_node)
identifiers = [server['attributes']['id'] for server in servers_on_node]
with open('server_identifiers.json', 'w') as file:
json.dump(identifiers, file)
print("Server count on node:", server_count)
time.sleep(1)
print("Deleting servers.")
time.sleep(1)
url2 = f'{url}/api/application/servers/{id}'
headers = {
"Authorization": f"Bearer {apikey}",
"Accept": "application/json",
"Content-Type": "application/json"
}
with open('server_identifiers.json', 'r') as file:
identifiers = json.load(file)
for identifier in identifiers:
request_url = url2.replace('{id}', str(identifier))
response = requests.delete(request_url, headers=headers)
print(f"Deleted identifier {identifier}: {response.text}")
@ZribeDev
Copy link
Author

ZribeDev commented Apr 4, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment