You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Bash脚本从多台DataPower批量拉取REST API服务并格式化展示

Got it, let's solve this batch DataPower status pull and formatting problem step by step. You already have the single-device curl working, so we'll expand that to handle 20 instances, clean up the JSON output, and get it into the formats you need (console, browser, Excel).

Step 1: Organize Your DataPower Device List

First, let's get all your device URIs in one place so the script can loop through them. You have two easy options:

  • Option A: Define an array in the script
    If you prefer keeping everything in one file, add this at the top of your bash script:

    # Define all your DataPower management URIs here
    DP_URIS=(
      "https://dpbox001:5554/mgmt/status/DEV/ObjectStatus"
      "https://dpbox002:5554/mgmt/status/STG/ObjectStatus"
      "https://dpbox003:5554/mgmt/status/PRD/ObjectStatus"
      # Add the rest of your 20 devices here
    )
    
  • Option B: Use a text file
    Create a dp_devices.txt file with one URI per line:

    https://dpbox001:5554/mgmt/status/DEV/ObjectStatus
    https://dpbox002:5554/mgmt/status/STG/ObjectStatus
    ...
    

    Then read it into the script with:

    DP_URIS=($(cat dp_devices.txt))
    
Step 2: Batch Pull Data with Error Handling

Next, we'll loop through each URI, pull the data with curl, and handle common issues like timeouts or connection failures. We'll also add authentication (adjust the DP_USER and DP_PASS variables to match your credentials):

# Set your DataPower credentials (better to use environment variables in production!)
DP_USER="your_username"
DP_PASS="your_password"

# Loop through each device URI
for uri in "${DP_URIS[@]}"; do
  echo "=== Pulling status from: $uri ==="
  
  # Pull JSON data with curl - handle self-signed certs with --insecure, add timeout
  response=$(curl -s --insecure --max-time 10 --user "$DP_USER:$DP_PASS" "$uri")
  
  # Check if curl succeeded
  if [ $? -ne 0 ]; then
    echo "ERROR: Failed to connect to $uri"
    echo ""
    continue
  fi
  
  # Pass the response to formatting (we'll add this next)
  echo "$response" | jq '.' # Temporary: just pretty-print raw JSON to test
  echo ""
done
Step 3: Format JSON into Readable Structured Output

To turn the raw JSON into something human-readable (and usable for Excel/browser), we'll use jq—a lightweight command-line JSON processor. First, install it:

  • On Debian/Ubuntu: sudo apt install jq
  • On macOS: brew install jq
  • On RHEL/CentOS: sudo dnf install jq

Example 1: Console-Friendly Key-Value Output

Modify the loop to extract specific fields (adjust the jq filter to match your JSON structure—this example assumes your response has deviceName, serviceStatus, and lastUpdated fields):

# Inside the loop, replace the jq '.' line with:
echo "$response" | jq -r '
  .ObjectStatus[] | 
  "Device: \(.deviceName)\nStatus: \(.serviceStatus)\nLast Updated: \(.lastUpdated)\n---"
'

Example 2: Tab-Separated (TSV) for Clean Console/Excel Import

If you want a table-like output that's easy to copy-paste into Excel, use @tsv:

echo "$response" | jq -r '
  ["Device Name", "Service Status", "Last Updated"],
  (.ObjectStatus[] | [.deviceName, .serviceStatus, .lastUpdated])
  | @tsv
'
Step 4: Output to Different Targets

1. Console

The examples above already output directly to the console—just run the script and you'll see formatted results.

2. Browser (HTML Table)

Generate an HTML file with all statuses, then open it in your browser:

Add this at the top of your script to initialize the HTML file:

# Create HTML header
cat > dp_status.html << EOF
<!DOCTYPE html>
<html>
<head>
  <title>DataPower Service Status</title>
  <style>
    table { border-collapse: collapse; width: 100%; }
    th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
    th { background-color: #f2f2f2; }
  </style>
</head>
<body>
  <h1>DataPower Service Status</h1>
  <table>
    <tr>
      <th>Device URI</th>
      <th>Service Name</th>
      <th>Status</th>
      <th>Last Updated</th>
    </tr>
EOF

Then inside the loop, append rows to the HTML file:

# After pulling the response, add this:
echo "$response" | jq -r --arg uri "$uri" '
  .ObjectStatus[] | 
  "<tr><td>\($uri)</td><td>\(.deviceName)</td><td>\(.serviceStatus)</td><td>\(.lastUpdated)</td></tr>"
' >> dp_status.html

At the end of the script, close the HTML and open it:

# Close HTML tags
echo "</table></body></html>" >> dp_status.html

# Open in browser (adjust command for your OS)
# macOS:
open dp_status.html
# Linux:
xdg-open dp_status.html

3. Excel (CSV File)

Generate a CSV file that Excel can open directly. Initialize the file first:

# Create CSV header
echo "Device URI,Service Name,Status,Last Updated" > dp_status.csv

Then inside the loop, append rows:

echo "$response" | jq -r --arg uri "$uri" '
  .ObjectStatus[] | 
  [$uri, .deviceName, .serviceStatus, .lastUpdated] | @csv
' >> dp_status.csv

Now you can open dp_status.csv directly in Excel.

Final Notes
  • Security: Avoid hardcoding credentials in the script—use environment variables like export DP_USER="your_user" and export DP_PASS="your_pass" instead.
  • Certificates: If your DataPower uses trusted certificates, remove the --insecure flag from curl.
  • JSON Structure: Adjust the jq filters to match your actual JSON response—use jq '.' on a single device's response to inspect the structure first.
  • Error Handling: The script includes basic error handling for curl failures, but you can expand it to log errors to a file if needed.

内容的提问来源于stack exchange,提问作者venkata12

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:26:55