Write a bash script for mcafee web gateways, that is able to login to the internal rest…
- User
Write a bash script for mcafee web gateways, that is able to login to the internal rest API and restarts the proxy listener service.
Assistant
#!/bin/bash # McAfee Web Gateway REST API Proxy Service Restart Script # Usage: ./restart_proxy.sh [hostname] [username] [password] # Check if required arguments are provided if [ $# -ne 3 ]; then echo "Usage: $0 [hostname] [username] [password]" exit 1 fi HOSTNAME="$1" USERNAME="$2" PASSWORD="$3" API_BASE="https://$HOSTNAME/mwg/api/v1" AUTH_ENDPOINT="$API_BASE/auth/login" PROXY_RESTART_ENDPOINT="$API_BASE/services/proxy/restart" # Function to handle errors handle_error() { echo "Error: $1" exit 1 } # Login to the API and get authentication token echo "Authenticating to McAfee Web Gateway..." AUTH_RESPONSE=$(curl -s -k -X POST "$AUTH_ENDPOINT" \ -H "Content-Type: application/json" \ -d "{\"username\":\"$USERNAME\",\"password\":\"$PASSWORD\"}" \ --connect-timeout 30) # Extract token from response TOKEN=$(echo "$AUTH_RESPONSE" | grep -o '"token":"[^"]*"' | cut -d'"' -f4) if [ -z "$TOKEN" ]; then handle_error "Failed to authenticate. Check credentials and hostname." fi echo "Successfully authenticated." # Restart proxy listener service echo "Restarting proxy listener service..." RESTART_RESPONSE=$(curl -s -k -X POST "$PROXY_RESTART_ENDPOINT" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $TOKEN" \ --connect-timeout 30) # Check if restart was successful if echo "$RESTART_RESPONSE" | grep -q "success"; then echo "Proxy listener service restart initiated successfully." else handle_error "Failed to restart proxy listener service: $RESTART_RESPONSE" fi echo "Operation completed." exit 0Would you like me to explain or break down the code?
- User
Please check if the listeners are active prior. Maybe perform an Authenticated proxy connect to a testwebsite to see if all is working.
Assistant
#!/bin/bash # McAfee Web Gateway REST API Proxy Service Management Script # Usage: ./manage_proxy.sh [hostname] [username] [password] [test_url] # Check if required arguments are provided if [ $# -lt 3 ]; then echo "Usage: $0 [hostname] [username] [password] [test_url]" echo "test_url is optional, defaults to https://www.google.com" exit 1 fi HOSTNAME="$1" USERNAME="$2" PASSWORD="$3" TEST_URL="${4:-https://www.google.com}" API_BASE="https://$HOSTNAME/mwg/api/v1" AUTH_ENDPOINT="$API_BASE/auth/login" PROXY_STATUS_ENDPOINT="$API_BASE/services/proxy/status" PROXY_RESTART_ENDPOINT="$API_BASE/services/proxy/restart" PROXY_PORT="9090" # Default McAfee proxy port, adjust if needed # Function to handle errors handle_error() { echo "Error: $1" exit 1 } # Login to the API and get authentication token echo "Authenticating to McAfee Web Gateway..." AUTH_RESPONSE=$(curl -s -k -X POST "$AUTH_ENDPOINT" \ -H "Content-Type: application/json" \ -d "{\"username\":\"$USERNAME\",\"password\":\"$PASSWORD\"}" \ --connect-timeout 30) # Extract token from response TOKEN=$(echo "$AUTH_RESPONSE" | grep -o '"token":"[^"]*"' | cut -d'"' -f4) if [ -z "$TOKEN" ]; then handle_error "Failed to authenticate. Check credentials and hostname." fi echo "Successfully authenticated." # Function to check proxy listener status check_proxy_status() { echo "Checking proxy listener status..." STATUS_RESPONSE=$(curl -s -k -X GET "$PROXY_STATUS_ENDPOINT" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $TOKEN" \ --connect-timeout 30) # Check if response contains active status if echo "$STATUS_RESPONSE" | grep -q '"status":"running"'; then echo "Proxy listeners are active and running." return 0 else echo "Proxy listeners are not active or in error state." echo "Status response: $STATUS_RESPONSE" return 1 fi } # Function to test proxy connectivity test_proxy_connection() { echo "Testing proxy connectivity to $TEST_URL..." HTTP_CODE=$(curl -s -k -o /dev/null -w "%{http_code}" \ --proxy "http://$HOSTNAME:$PROXY_PORT" \ --proxy-user "$USERNAME:$PASSWORD" \ "$TEST_URL" --connect-timeout 30) if [ "$HTTP_CODE" -ge 200 ] && [ "$HTTP_CODE" -lt 400 ]; then echo "Proxy connectivity test successful (HTTP code: $HTTP_CODE)." return 0 else echo "Proxy connectivity test failed (HTTP code: $HTTP_CODE)." return 1 fi } # Function to restart proxy listener service restart_proxy() { echo "Restarting proxy listener service..." RESTART_RESPONSE=$(curl -s -k -X POST "$PROXY_RESTART_ENDPOINT" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $TOKEN" \ --connect-timeout 30) # Check if restart was successful if echo "$RESTART_RESPONSE" | grep -q "success"; then echo "Proxy listener service restart initiated successfully." # Wait for the service to fully restart echo "Waiting for proxy service to restart (30 seconds)..." sleep 30 return 0 else echo "Failed to restart proxy listener service: $RESTART_RESPONSE" return 1 fi } # Main execution flow # First check current status check_proxy_status PROXY_STATUS=$? # Test current proxy connectivity test_proxy_connection CONNECTIVITY_TEST=$? # Decide if restart is needed if [ $PROXY_STATUS -eq 0 ] && [ $CONNECTIVITY_TEST -eq 0 ]; then echo "Proxy service is functioning correctly. No restart needed." else echo "Issues detected with proxy service. Initiating restart..." restart_proxy # Verify status after restart echo "Verifying proxy status after restart..." check_proxy_status # Test connectivity after restart echo "Testing connectivity after restart..." test_proxy_connection FINAL_TEST=$? if [ $FINAL_TEST -eq 0 ]; then echo "Proxy service is now functioning correctly." else handle_error "Proxy service is still experiencing issues after restart." fi fi echo "Operation completed." exit 0Would you like me to explain or break down the code?
- User
the REST API Endpoint is per default on tcp/4711 and it has no /v1 or something. The Proxies have UUIDs, how to get them and check?
Assistant
#!/bin/bash # McAfee Web Gateway REST API Proxy Service Management Script # Usage: ./manage_proxy.sh [hostname] [username] [password] [test_url] # Check if required arguments are provided if [ $# -lt 3 ]; then echo "Usage: $0 [hostname] [username] [password] [test_url]" echo "test_url is optional, defaults to https://www.google.com" exit 1 fi HOSTNAME="$1" USERNAME="$2" PASSWORD="$3" TEST_URL="${4:-https://www.google.com}" API_PORT="4711" API_BASE="https://$HOSTNAME:$API_PORT/mwg/api" AUTH_ENDPOINT="$API_BASE/auth/login" PROXY_LIST_ENDPOINT="$API_BASE/services/proxies" PROXY_PORT="9090" # Default McAfee proxy port, adjust if needed # Function to handle errors handle_error() { echo "Error: $1" exit 1 } # Login to the API and get authentication token echo "Authenticating to McAfee Web Gateway..." AUTH_RESPONSE=$(curl -s -k -X POST "$AUTH_ENDPOINT" \ -H "Content-Type: application/json" \ -d "{\"username\":\"$USERNAME\",\"password\":\"$PASSWORD\"}" \ --connect-timeout 30) # Extract token from response TOKEN=$(echo "$AUTH_RESPONSE" | grep -o '"token":"[^"]*"' | cut -d'"' -f4) if [ -z "$TOKEN" ]; then handle_error "Failed to authenticate. Check credentials and hostname." fi echo "Successfully authenticated." # Get list of proxies and their UUIDs echo "Retrieving proxy services list..." PROXY_LIST_RESPONSE=$(curl -s -k -X GET "$PROXY_LIST_ENDPOINT" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $TOKEN" \ --connect-timeout 30) # Check if we got a valid response if [ -z "$PROXY_LIST_RESPONSE" ]; then handle_error "Failed to retrieve proxy list." fi # Extract proxy UUIDs and names echo "Available proxy services:" echo "$PROXY_LIST_RESPONSE" | grep -E '"uuid"|"name"' | tr -d ' ' | sed 's/,$//' # Store proxy UUIDs in an array PROXY_UUIDS=($(echo "$PROXY_LIST_RESPONSE" | grep -o '"uuid":"[^"]*"' | cut -d'"' -f4)) if [ ${#PROXY_UUIDS[@]} -eq 0 ]; then handle_error "No proxy UUIDs found." fi echo "Found ${#PROXY_UUIDS[@]} proxy services." # Function to check proxy status by UUID check_proxy_status() { local uuid=$1 local name=$2 echo "Checking status of proxy $name (UUID: $uuid)..." STATUS_RESPONSE=$(curl -s -k -X GET "$PROXY_LIST_ENDPOINT/$uuid/status" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $TOKEN" \ --connect-timeout 30) # Output the status echo "Status for $name: $STATUS_RESPONSE" # Check if response indicates the proxy is running if echo "$STATUS_RESPONSE" | grep -q '"status":"running"'; then return 0 else return 1 fi } # Function to restart proxy by UUID restart_proxy() { local uuid=$1 local name=$2 echo "Restarting proxy $name (UUID: $uuid)..." RESTART_RESPONSE=$(curl -s -k -X POST "$PROXY_LIST_ENDPOINT/$uuid/restart" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $TOKEN" \ --connect-timeout 30) echo "Restart response: $RESTART_RESPONSE" # Wait for the service to fully restart echo "Waiting for proxy service to restart (30 seconds)..." sleep 30 # Check if restart was successful by checking status again if check_proxy_status "$uuid" "$name"; then echo "Proxy $name restarted successfully." return 0 else echo "Failed to restart proxy $name or status check failed." return 1 fi } # Function to test proxy connectivity test_proxy_connection() { echo "Testing proxy connectivity to $TEST_URL..." HTTP_CODE=$(curl -s -k -o /dev/null -w "%{http_code}" \ --proxy "http://$HOSTNAME:$PROXY_PORT" \ --proxy-user "$USERNAME:$PASSWORD" \ "$TEST_URL" --connect-timeout 30) if [ "$HTTP_CODE" -ge 200 ] && [ "$HTTP_CODE" -lt 400 ]; then echo "Proxy connectivity test successful (HTTP code: $HTTP_CODE)." return 0 else echo "Proxy connectivity test failed (HTTP code: $HTTP_CODE)." return 1 fi } # Main execution flow - Check and manage each proxy ALL_PROXIES_OK=true for i in "${!PROXY_UUIDS[@]}"; do UUID=${PROXY_UUIDS[$i]} NAME=$(echo "$PROXY_LIST_RESPONSE" | grep -A1 "${UUID}" | grep "name" | cut -d'"' -f4) echo "Processing proxy: $NAME" # Check current status if check_proxy_status "$UUID" "$NAME"; then echo "Proxy $NAME is running." else echo "Proxy $NAME is not running properly. Will attempt restart." ALL_PROXIES_OK=false restart_proxy "$UUID" "$NAME" fi echo "----------------------------------------" done # Test overall proxy connectivity test_proxy_connection CONNECTIVITY_TEST=$? if [ "$ALL_PROXIES_OK" = true ] && [ $CONNECTIVITY_TEST -eq 0 ]; then echo "All proxy services are functioning correctly." else echo "Some issues were detected and addressed. Final connectivity test result: $CONNECTIVITY_TEST" fi echo "Operation completed." exit 0Would you like me to explain or break down the code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.