diff options
Diffstat (limited to 'inventory.sh')
-rwxr-xr-x | inventory.sh | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/inventory.sh b/inventory.sh new file mode 100755 index 0000000..ed7a037 --- /dev/null +++ b/inventory.sh @@ -0,0 +1,116 @@ +#!/bin/bash + +# Function to get MAC address of the default network interface +get_mac_address() { + INTERFACE=$(ip route | awk '/default/ {print $5; exit}') + + if [[ -n "$INTERFACE" ]]; then + MAC=$(cat /sys/class/net/$INTERFACE/address) + echo "Detected MAC Address: $MAC" + check_device_status "$MAC" + else + echo "No active network interface found!" + exit 1 + fi +} + +# Function for manual input +manual_input() { + read -p "Enter MAC Address (manually or via barcode scan): " MAC + if [[ -n "$MAC" ]]; then + check_device_status "$MAC" + else + echo "Invalid input! MAC Address cannot be empty." + exit 1 + fi +} + +# Function to check device status using the API +check_device_status() { + local MAC_ADDRESS="$1" + API_URL="http://localhost:3000/check-status?mac=$MAC_ADDRESS" + + RESPONSE=$(curl -s "$API_URL") + + if [[ -z "$RESPONSE" ]]; then + echo "No response from server. Check if the API is running." + exit 1 + fi + + # Check if the device is registered or new + STATUS=$(echo "$RESPONSE" | jq -r '.status') + + if [[ "$STATUS" == "registered" ]]; then + SERIAL_NUMBER=$(echo "$RESPONSE" | jq -r '.serialNumber') + echo "Device is already registered. Serial number is $SERIAL_NUMBER." + elif [[ "$STATUS" == "new" ]]; then + echo "Device is new. Proceeding to registration." + register_device "$MAC_ADDRESS" + else + echo "Unknown status: $STATUS" + fi +} + +# Function to register the device +register_device() { + local MAC_ADDRESS="$1" + + # Ask user for variant choice (pro or starter) + echo "Select variant (pro or starter):" + read -p "Enter your choice: " VARIANT + + if [[ "$VARIANT" != "pro" && "$VARIANT" != "starter" ]]; then + echo "Invalid variant choice. Exiting." + exit 1 + fi + + # Get OS name from /etc/os-release + OS_NAME=$(grep "^NAME=" /etc/os-release | cut -d= -f2 | tr -d '"') + + # Optional customer information + read -p "Enter customer name (or press Enter to skip): " CUSTOMER_NAME + read -p "Enter customer address (or press Enter to skip): " CUSTOMER_ADDRESS + read -p "Enter customer phone number (or press Enter to skip): " CUSTOMER_NUMBER + + # Prepare JSON body for the POST request + JSON_BODY=$(jq -n \ + --arg verient "$VARIANT" \ + --arg mac "$MAC_ADDRESS" \ + --arg os "$OS_NAME" \ + --arg status "registered" \ + --arg customerName "$CUSTOMER_NAME" \ + --arg customerAddress "$CUSTOMER_ADDRESS" \ + --arg customerNumber "$CUSTOMER_NUMBER" \ + '{ + Verient: $verient, + mac: $mac, + os: $os, + status: $status, + customerName: $customerName, + customerAddress: $customerAddress, + customerNumber: $customerNumber + }') + + # Send the registration request + REGISTER_URL="http://localhost:3000/upload" + REGISTER_RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" -d "$JSON_BODY" "$REGISTER_URL") + + if [[ -n "$REGISTER_RESPONSE" ]]; then + echo "Device successfully registered. Response: $REGISTER_RESPONSE" + else + echo "No response from the registration API. Check if the API is running." + fi +} + +# Menu for user selection +echo "Select an option:" +echo "1. Input MAC manually (type manually or barcode scan)" +echo "2. Auto-detect device MAC" + +read -p "Enter your choice (1 or 2): " CHOICE + +case $CHOICE in + 1) manual_input ;; + 2) get_mac_address ;; + *) echo "Invalid option. Please select 1 or 2." ;; +esac |