From 74ec8e9c3f1ed1accf62b7e7e676379cdc8ddc2d Mon Sep 17 00:00:00 2001 From: Jan Date: Mon, 11 Aug 2025 17:57:34 +0200 Subject: [PATCH] Added Batch Print Script --- tools/batch_print/computer.json | 15 ++++ tools/batch_print/print.py | 124 ++++++++++++++++++++++++++++++++ tools/batch_print/print.sh | 48 +++++++++++++ tools/batch_print/test.py | 33 +++++++++ 4 files changed, 220 insertions(+) create mode 100644 tools/batch_print/computer.json create mode 100644 tools/batch_print/print.py create mode 100644 tools/batch_print/print.sh create mode 100644 tools/batch_print/test.py diff --git a/tools/batch_print/computer.json b/tools/batch_print/computer.json new file mode 100644 index 0000000..0301609 --- /dev/null +++ b/tools/batch_print/computer.json @@ -0,0 +1,15 @@ +{ + "status_id": "2", + "name": "HP-Tablet", + "model_id": "2", + "serial": "Null", + "_snipeit_mac_address_1": "Null", + "_snipeit_betriebssystem_2": "Windows 11", + "_snipeit_festplatte_4": "128", + "_snipeit_prozessor_5": "Intel Pentium 4410 1.5ghz", + "_snipeit_arbeitsspeicher_6": "4", + "_snipeit_optisches_laufwerk_7": "nein", + "_snipeit_display_8": "", + "_snipeit_akku_9": "", + "_snipeit_anydeskid_10": "" +} diff --git a/tools/batch_print/print.py b/tools/batch_print/print.py new file mode 100644 index 0000000..e1544df --- /dev/null +++ b/tools/batch_print/print.py @@ -0,0 +1,124 @@ +import string +import argparse +import sys +import json +import os +import random +import requests + +def check_mac_exists(mac,api_key): + + + + url = f'https://computer.z31.it/api/v1/hardware?limit=5&search={mac}' + headers = { + 'accept': 'application/json', + 'authorization': f'Bearer {api_key}', + 'content-type': 'application/x-www-form-urlencoded' + } + + response = requests.get(url, headers=headers) + if response.status_code != 200: + print(f"Error fetching data: HTTP {response.status_code}") + exit(1) + + data = response.json() + # Extract asset_tag from all rows + asset_tags = [row.get('asset_tag', '') for row in data.get('rows', []) if row.get('asset_tag')] + if asset_tags: + return True + else: + return False + exit(0) + +def generate_random_mac(): + mac = [ + 0x02, # fixed first byte + random.randint(0x00, 0xFF), + random.randint(0x00, 0xFF), + random.randint(0x00, 0xFF), + random.randint(0x00, 0xFF), + random.randint(0x00, 0xFF), + ] + return ":".join(f"{octet:02x}" for octet in mac) + +def generate_random_serial(length=8): + """Generate a random uppercase alphanumeric serial number.""" + chars = string.ascii_uppercase + string.digits + return "DUMMY-" +"".join(random.choices(chars, k=length)) + +def main(): + parser = argparse.ArgumentParser( + description="Validate a JSON file, print its contents, an amount, and generate a random private MAC address." + ) + parser.add_argument( + "-j","--path", + type=str, + help="Path to JSON File containing Computer Info" + ) + parser.add_argument( + "-i", "--amount", + type=int, + help="Number of Labels that should be printed (Can't be more than 50)" + ) + + args = parser.parse_args() + + json_file = args.path + amount = args.amount + + response = requests.get("http://10.200.4.12:8888/snipe_api") + response.raise_for_status() # raise error if request failed + api_key = response.text.strip() # or response.json() if JSON returned + if amount > 50: + print("Maximum amount can be 50.") + sys.exit(1) + # Check if file exists + if not os.path.isfile(json_file): + print(f"Error: File '{json_file}' does not exist.") + sys.exit(1) + + # Validate JSON + try: + with open(json_file, "r") as f: + data = json.load(f) + except json.JSONDecodeError as e: + print(f"Error: Invalid JSON in file '{json_file}': {e}") + sys.exit(1) + + # Display JSON and amount + + # Generate and display MAC + generate_random_mac() + data['_snipeit_mac_address_1'] = generate_random_mac() + data["serial"] = generate_random_serial() + + for i in range(1, args.amount + 1): + exists = True + while exists: + generate_random_mac() + mac = generate_random_mac() + exists = check_mac_exists(mac,api_key) + + data['_snipeit_mac_address_1'] = generate_random_mac() + data["serial"] = generate_random_serial() + #Snipe IT + headers = { + "accept": "application/json", + "authorization": f"Bearer {api_key}", + "Content-Type": "application/json" + } + asset =requests.post("https://computer.z31.it/api/v1/hardware",headers=headers,data=json.dumps(data)) + tag =json.loads(asset.content)['payload']['asset_tag'] + print_data = { + "id": tag, + "distribution": "Windows", + "version": "11", + "cpu": "Intel Pentium 4410 1.5ghz", + "memory": "4", + "disk": "128" + } + res = requests.post("http://10.200.4.12:8888/batch", data=json.dumps(print_data)) + print(res.content) +if __name__ == "__main__": + main() diff --git a/tools/batch_print/print.sh b/tools/batch_print/print.sh new file mode 100644 index 0000000..9919eee --- /dev/null +++ b/tools/batch_print/print.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +# Function to generate a random locally administered (private) MAC address +generate_random_mac() { + # First byte: set locally administered bit (0x02) and unicast (0x00) + # Remaining bytes: random + printf '02:%02x:%02x:%02x:%02x:%02x\n' \ + $((RANDOM % 256)) \ + $((RANDOM % 256)) \ + $((RANDOM % 256)) \ + $((RANDOM % 256)) \ + $((RANDOM % 256)) +} + +# Check if the right number of arguments are given +if [ "$#" -ne 2 ]; then + echo "Usage: $0 " + exit 1 +fi + +# Assign arguments to variables +json_file="$1" +amount="$2" + +# Check if jq is installed +if ! command -v jq >/dev/null 2>&1; then + echo "Error: jq is required but not installed." + exit 1 +fi + +# Check if JSON file exists +if [ ! -f "$json_file" ]; then + echo "Error: File '$json_file' does not exist." + exit 1 +fi + +# Validate JSON file content +jq empty "$json_file" 2>/dev/null +if [ $? -ne 0 ]; then + echo "Error: Invalid JSON in file '$json_file'." + exit 1 +fi + +# If valid, display JSON and amount +echo "Valid JSON detected!" +echo "Amount: $amount" + +mac=$(generate_random_mac) diff --git a/tools/batch_print/test.py b/tools/batch_print/test.py new file mode 100644 index 0000000..c4a30c7 --- /dev/null +++ b/tools/batch_print/test.py @@ -0,0 +1,33 @@ +import requests +def check_mac_in_hardware(mac): + + response = requests.get("http://10.200.4.12:8888/snipe_api") + response.raise_for_status() # raise error if request failed + + api_key = response.text.strip() # or response.json() if JSON returned + + url = f'https://computer.z31.it/api/v1/hardware?limit=5&search={mac}' + headers = { + 'accept': 'application/json', + 'authorization': f'Bearer {api_key}', + 'content-type': 'application/x-www-form-urlencoded' + } + + response = requests.get(url, headers=headers) + if response.status_code != 200: + print(f"Error fetching data: HTTP {response.status_code}") + exit(1) + + data = response.json() + + # Extract asset_tag from all rows + asset_tags = [row.get('asset_tag', '') for row in data.get('rows', []) if row.get('asset_tag')] + print(asset_tags) + if asset_tags: + print("Mac already exists in Snipe IT") + else: + print("Does not exist") + exit(0) + + +check_mac_in_hardware("c8:f7:50:7f:ee:ce")