Skip to content

Instantly share code, notes, and snippets.

@brodeynewman
Last active January 29, 2025 18:11
Show Gist options
  • Select an option

  • Save brodeynewman/a5c5c43e855469dc865350e8a7a23377 to your computer and use it in GitHub Desktop.

Select an option

Save brodeynewman/a5c5c43e855469dc865350e8a7a23377 to your computer and use it in GitHub Desktop.
Get ethernet info from redfish API
#!/bin/bash
IPMI_PASS="pass"
USERNAME="user"
PORT="623"
VERIFY_CA="False"
IP_RANGE=(10.10.5.{1..36})
OUTPUT_FILE="mac_mapping.json"
echo "{" > $OUTPUT_FILE
for IP in "${IP_RANGE[@]}"; do
echo "Processing IP: $IP"
# Check if the IP is reachable
ping -c 1 -W 1 "$IP" > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo " \"$IP\": { \"error\": \"Unreachable\" }," >> $OUTPUT_FILE
continue
fi
SYSTEM_URL="https://$IP/redfish/v1/Systems/System.Embedded.1"
ETHERNET_URL="$SYSTEM_URL/EthernetInterfaces"
SYSTEM_INFO=$(curl -s -k -u "$USERNAME:$IPMI_PASS" \
-H "Content-Type: application/json" \
"$SYSTEM_URL")
echo "$SYSTEM_INFO"
HOSTNAME=$(echo "$SYSTEM_INFO" | jq -r '.HostName // "Unknown"' 2>/dev/null)
SERIAL_NUMBER=$(echo "$SYSTEM_INFO" | jq -r '.SerialNumber // "Unknown"' 2>/dev/null)
MAC_RESPONSE=$(curl -s -k -u "$USERNAME:$IPMI_PASS" \
-H "Content-Type: application/json" \
"$ETHERNET_URL")
INTERFACE_IDS=$(echo "$MAC_RESPONSE" | jq -r '.Members[] | .["@odata.id"]' 2>/dev/null)
echo "Extracted interface IDs for $IP:"
echo "$INTERFACE_IDS"
if [ -n "$INTERFACE_IDS" ]; then
echo " \"$IP\": {" >> $OUTPUT_FILE
echo " \"HostName\": \"$HOSTNAME\"," >> $OUTPUT_FILE
echo " \"SerialNumber\": \"$SERIAL_NUMBER\"," >> $OUTPUT_FILE
echo " \"MACAddresses\": [" >> $OUTPUT_FILE
for INTERFACE_ID in $INTERFACE_IDS; do
INTERFACE_DETAILS=$(curl -s -k -u "$USERNAME:$IPMI_PASS" \
-H "Content-Type: application/json" \
"https://$IP$INTERFACE_ID")
MAC=$(echo "$INTERFACE_DETAILS" | jq -r '.MACAddress' 2>/dev/null)
if [ -n "$MAC" ]; then
echo " \"$MAC\"," >> $OUTPUT_FILE
else
echo " \"Error: No MACAddress found for $INTERFACE_ID\"," >> $OUTPUT_FILE
fi
done
sed -i '' '$ s/,$//' $OUTPUT_FILE
echo " ]" >> $OUTPUT_FILE
echo " }," >> $OUTPUT_FILE
else
echo " \"$IP\": { \"error\": \"No Ethernet interfaces found\" }," >> $OUTPUT_FILE
fi
done
sed -i '' '$ s/,$//' $OUTPUT_FILE
echo "}" >> $OUTPUT_FILE
echo "MAC address mapping complete. Output saved to $OUTPUT_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment