Skip to content

Commit

Permalink
BAN-2695: MQTT troubleshooting script added to mac commands gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
asinghjc committed Jan 21, 2025
1 parent 4fed7a5 commit b2410ab
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#### Name

Mac - MQTT Troubleshooting Script | v1.1 JCCG

#### commandType

mac

#### Command

```
#!/bin/bash
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Default values
BROKER="a1hrq03pdcca60-ats.iot.us-east-1.amazonaws.com"
PORT=443
TIMEOUT=10
VERBOSE=false
# Help function
show_help() {
echo "MQTT Troubleshooting Script"
echo "Usage: $0 [options]"
echo "Options:"
echo " -h, --help Show this help message"
echo " -b, --broker MQTT Broker address (required)"
echo " -p, --port Broker port (default: 1883)"
echo " -v, --verbose Enable verbose output"
exit 1
}
# Parse command line arguments
PARSED_ARGUMENTS=$(getopt -a mqtt-troubleshooter -o hvb:p: --long help,verbose,broker:,port:, -- "$@")
VALID_ARGUMENTS=$?
[ $VALID_ARGUMENTS -ne 0 ] && show_help
eval set -- "$PARSED_ARGUMENTS"
while :
do
case "$1" in
-h | --help) show_help; shift ;;
-b | --broker) BROKER="$2"; shift 2 ;;
-p | --port) PORT="$2"; shift 2 ;;
-v | --verbose) VERBOSE=true; shift ;;
--) shift; break ;;
*) echo "Unexpected option: $1"; show_help ;;
esac
done
# Validate required arguments
if [ -z "$BROKER" ]; then
echo "${RED}Error: Broker address is required${NC}"
show_help
fi
# Logging function
log() {
local level="$1"
local message="$2"
local color=""
case "$level" in
"INFO") color=$GREEN ;;
"WARN") color=$YELLOW ;;
"ERROR") color=$RED ;;
"DEBUG") color=$BLUE ;;
*) color=$NC ;;
esac
if [ "$VERBOSE" = true ] || [ "$level" != "DEBUG" ]; then
echo -e "${color}[${level}]${NC} $message"
fi
}
# Network connectivity check
check_network() {
log "INFO" "Checking network connectivity to $BROKER:$PORT"
nc -z -w$TIMEOUT "$BROKER" "$PORT"
if [ $? -eq 0 ]; then
log "INFO" "Network connection successful"
return 0
else
log "ERROR" "Network connection failed"
return 1
fi
}
# Main diagnosis function
diagnose() {
echo -e "${BLUE}=== MQTT Troubleshooting Diagnosis ===${NC}"
# Run diagnostic checks
local network_check=0
check_network
# Print final summary
echo -e "\n${BLUE}=== Diagnostic Summary ===${NC}"
echo -e "Network Connection: ${network_check=0 && echo -e "${GREEN}PASS${NC}" || echo -e "${RED}FAIL${NC}"}"
# Provide troubleshooting suggestions
if [ $network_check -ne 0 ]; then
echo -e "\n${YELLOW}Troubleshooting Network Issues:${NC}"
echo "- Verify broker address and port"
echo "- Check firewall settings"
echo "- Confirm network connectivity"
fi
}
# Run diagnosis
diagnose
exit 0
```

#### Description

This script helps diagnose common MQTT connectivity and communication issues by performing various network checks.

#### *Import This Command*

To import this command into your JumpCloud tenant run the below command using the [JumpCloud PowerShell Module](https://github.com/TheJumpCloud/support/wiki/Installing-the-JumpCloud-PowerShell-Module)

```
Import-JCCommand -URL "https://github.com/TheJumpCloud/support/blob/master/PowerShell/JumpCloud%20Commands%20Gallery/Mac%20Commands/Mac%20-%20MQTT%20Troubleshooting.md"
```
11 changes: 9 additions & 2 deletions PowerShell/JumpCloud Commands Gallery/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
"name": "Linux - Troubleshooting MQTT script | v1.0 JCCG",
"type": "linux",
"command": "#!/bin/bash\n################################################################################\n# This script is used for MQTT Troubleshooting\n# Requires: netcat (nc), openssl\n################################################################################\n\nif [[ \"${UID}\" != 0 ]]; then\n (>&2 echo \"Error: $0 must be run as root\")\n exit 1\nfi\n\nRED='\\033[0;31m'\nGREEN='\\033[0;32m'\nYELLOW='\\033[0;33m'\nBLUE='\\033[0;34m'\nNC='\\033[0m' # No Color\n# Default values\nBROKER=\"a1hrq03pdcca60-ats.iot.us-east-1.amazonaws.com\"\nPORT=443\nTIMEOUT=10\nUSE_TLS=true\nCA_CERT_PATH=\"/etc/ssl/certs\"\nCLIENT_CERT=\"/opt/jc/client.crt\"\nCLIENT_KEY=\"/opt/jc/client.key\"\nVERBOSE=false\n# Help function\nshow_help() {\n echo \"MQTT Troubleshooting Script\"\n echo \"Usage: $0 [options]\"\n echo \"Options:\"\n echo \" -h, --help Show this help message\"\n echo \" -b, --broker MQTT Broker address (required)\"\n echo \" -p, --port Broker port (default: 1883)\"\n echo \" --tls Use TLS connection\"\n echo \" --ca-cert-path Path to CA certificate\"\n echo \" --client-cert Path to client certificate\"\n echo \" --client-key Path to client key\"\n echo \" -v, --verbose Enable verbose output\"\n exit 1\n}\n# Parse command line arguments\nPARSED_ARGUMENTS=$(getopt -a -n mqtt-troubleshooter -o hvb:p: --long help,verbose,broker:,port:,tls,ca-cert:,client-cert:,client-key: -- \"$@\")\nVALID_ARGUMENTS=$?\n[ $VALID_ARGUMENTS -ne 0 ] && show_help\neval set -- \"$PARSED_ARGUMENTS\"\nwhile :\ndo\n case \"$1\" in\n -h | --help) show_help; shift ;;\n -b | --broker) BROKER=\"$2\"; shift 2 ;;\n -p | --port) PORT=\"$2\"; shift 2 ;;\n --tls) USE_TLS=true; shift ;;\n --ca-cert-path) CA_CERT_PATH=\"$2\"; shift 2 ;;\n --client-cert) CLIENT_CERT=\"$2\"; shift 2 ;;\n --client-key) CLIENT_KEY=\"$2\"; shift 2 ;;\n -v | --verbose) VERBOSE=true; shift ;;\n --) shift; break ;;\n *) echo \"Unexpected option: $1\"; show_help ;;\n esac\ndone\n# Validate required arguments\nif [ -z \"$BROKER\" ]; then\n echo \"${RED}Error: Broker address is required${NC}\"\n show_help\nfi\n# Logging function\nlog() {\n local level=\"$1\"\n local message=\"$2\"\n local color=\"\"\n case \"$level\" in\n \"INFO\") color=$GREEN ;;\n \"WARN\") color=$YELLOW ;;\n \"ERROR\") color=$RED ;;\n \"DEBUG\") color=$BLUE ;;\n *) color=$NC ;;\n esac\n if [ \"$VERBOSE\" = true ] || [ \"$level\" != \"DEBUG\" ]; then\n echo -e \"${color}[${level}]${NC} $message\"\n fi\n}\n# Network connectivity check\ncheck_network() {\n log \"INFO\" \"Checking network connectivity to $BROKER:$PORT\"\n nc -z -w$TIMEOUT \"$BROKER\" \"$PORT\"\n if [ $? -eq 0 ]; then\n log \"INFO\" \"Network connection successful\"\n return 0\n else\n log \"ERROR\" \"Network connection failed\"\n return 1\n fi\n}\n# TLS connection check\ncheck_tls() {\n if [ \"$USE_TLS\" = true ]; then\n log \"INFO\" \"Checking TLS connection\"\n # Validate TLS parameters\n if [ -z \"$CA_CERT_PATH\" ]; then\n log \"ERROR\" \"CA Certificate path is required for TLS connection\"\n return 1\n fi\n # Attempt TLS connection using openssl\n echo \"Q\" | openssl s_client -connect \"$BROKER:$PORT\" -CApath \"$CA_CERT_PATH\" \\\n ${CLIENT_CERT:+-cert \"$CLIENT_CERT\"} \\\n ${CLIENT_KEY:+-key \"$CLIENT_KEY\"} \\\n -verify=2 -brief\n \n if [ $? -eq 0 ]; then\n log \"INFO\" \"TLS connection successful\"\n return 0\n else\n log \"ERROR\" \"TLS connection failed\"\n return 1\n fi\n fi\n return 0\n}\n# Main diagnosis function\ndiagnose() {\n echo -e \"${BLUE}=== MQTT Troubleshooting Diagnosis ===${NC}\"\n # Run diagnostic checks\n local network_check=0\n local tls_check=0\n check_network\n network_check=$?\n if [ $network_check -eq 0 ]; then\n check_tls\n tls_check=$?\n fi\n # Print final summary\n echo -e \"\\n${BLUE}=== Diagnostic Summary ===${NC}\"\n echo -e \"Network Connection: ${network_check=0 && echo -e \"${GREEN}PASS${NC}\" || echo -e \"${RED}FAIL${NC}\"}\"\n echo -e \"TLS Connection: ${tls_check=0 && echo -e \"${GREEN}PASS${NC}\" || echo -e \"${RED}FAIL${NC}\"}\"\n # Provide troubleshooting suggestions\n if [ $network_check -ne 0 ]; then\n echo -e \"\\n${YELLOW}Troubleshooting Network Issues:${NC}\"\n echo \"- Verify broker address and port\"\n echo \"- Check firewall settings\"\n echo \"- Confirm network connectivity\"\n fi\n if [ $tls_check -ne 0 ]; then\n echo -e \"\\n${YELLOW}Troubleshooting TLS Issues:${NC}\"\n echo \"- Verify CA certificate path\"\n echo \"- Check client certificates\"\n echo \"- Confirm TLS configuration\"\n fi\n}\n# Run diagnosis\ndiagnose\nexit 0",
"link": "https://github.com/TheJumpCloud/support/blob/master/PowerShell/JumpCloud%20Commands%20Gallery/Linux%20Commands/Linux%20-%20MQTT-troubleshooting.md",
"link": "https://github.com/TheJumpCloud/support/blob/master/PowerShell/JumpCloud%20Commands%20Gallery/Linux%20Commands/Linux%20-%20MQTT%20Troubleshooting.md",
"Description": "This script is used for troubleshooting MQTT on the Linux machine."
},
{
Expand Down Expand Up @@ -244,6 +244,13 @@
"link": "https://github.com/TheJumpCloud/support/blob/master/PowerShell/JumpCloud%20Commands%20Gallery/Mac%20Commands/Mac%20-%20Set%20ReleaseChannel%20In%20JumpCloud%20Password%20Manager.md",
"description": "This command will set the desired release channel for JumpCloud's Password Manager in application's directory. The relesase channel options are beta, dogfood and public."
},
{
"name": "Mac - MQTT Troubleshooting Script | v1.1 JCCG",
"type": "mac",
"command": "#!/bin/bash\n# Color codes for output\nRED='\\033[0;31m'\nGREEN='\\033[0;32m'\nYELLOW='\\033[0;33m'\nBLUE='\\033[0;34m'\nNC='\\033[0m' # No Color\n# Default values\nBROKER=\"a1hrq03pdcca60-ats.iot.us-east-1.amazonaws.com\"\nPORT=443\nTIMEOUT=10\nVERBOSE=false\n# Help function\nshow_help() {\n echo \"MQTT Troubleshooting Script\"\n echo \"Usage: $0 [options]\"\n echo \"Options:\"\n echo \" -h, --help Show this help message\"\n echo \" -b, --broker MQTT Broker address (required)\"\n echo \" -p, --port Broker port (default: 1883)\"\n echo \" -v, --verbose Enable verbose output\"\n exit 1\n}\n# Parse command line arguments\nPARSED_ARGUMENTS=$(getopt -a mqtt-troubleshooter -o hvb:p: --long help,verbose,broker:,port:, -- \"$@\")\nVALID_ARGUMENTS=$?\n[ $VALID_ARGUMENTS -ne 0 ] && show_help\neval set -- \"$PARSED_ARGUMENTS\"\nwhile :\ndo\n case \"$1\" in\n -h | --help) show_help; shift ;;\n -b | --broker) BROKER=\"$2\"; shift 2 ;;\n -p | --port) PORT=\"$2\"; shift 2 ;;\n -v | --verbose) VERBOSE=true; shift ;;\n --) shift; break ;;\n *) echo \"Unexpected option: $1\"; show_help ;;\n esac\ndone\n# Validate required arguments\nif [ -z \"$BROKER\" ]; then\n echo \"${RED}Error: Broker address is required${NC}\"\n show_help\nfi\n# Logging function\nlog() {\n local level=\"$1\"\n local message=\"$2\"\n local color=\"\"\n case \"$level\" in\n \"INFO\") color=$GREEN ;;\n \"WARN\") color=$YELLOW ;;\n \"ERROR\") color=$RED ;;\n \"DEBUG\") color=$BLUE ;;\n *) color=$NC ;;\n esac\n if [ \"$VERBOSE\" = true ] || [ \"$level\" != \"DEBUG\" ]; then\n echo -e \"${color}[${level}]${NC} $message\"\n fi\n}\n# Network connectivity check\ncheck_network() {\n log \"INFO\" \"Checking network connectivity to $BROKER:$PORT\"\n nc -z -w$TIMEOUT \"$BROKER\" \"$PORT\"\n if [ $? -eq 0 ]; then\n log \"INFO\" \"Network connection successful\"\n return 0\n else\n log \"ERROR\" \"Network connection failed\"\n return 1\n fi\n}\n# Main diagnosis function\ndiagnose() {\n echo -e \"${BLUE}=== MQTT Troubleshooting Diagnosis ===${NC}\"\n # Run diagnostic checks\n local network_check=0\n check_network\n # Print final summary\n echo -e \"\\n${BLUE}=== Diagnostic Summary ===${NC}\"\n echo -e \"Network Connection: ${network_check=0 && echo -e \"${GREEN}PASS${NC}\" || echo -e \"${RED}FAIL${NC}\"}\"\n # Provide troubleshooting suggestions\n if [ $network_check -ne 0 ]; then\n echo -e \"\\n${YELLOW}Troubleshooting Network Issues:${NC}\"\n echo \"- Verify broker address and port\"\n echo \"- Check firewall settings\"\n echo \"- Confirm network connectivity\"\n fi\n}\n# Run diagnosis\ndiagnose\nexit 0",
"link": "https://github.com/TheJumpCloud/support/blob/master/PowerShell/JumpCloud%20Commands%20Gallery/Mac%20Commands/Mac%20-%20MQTT%20Troubleshooting.md",
"description": "This script helps diagnose common MQTT connectivity and communication issues by performing various network checks."
},
{
"name": "Windows - 64-Bit Command | v1.0 JCCG",
"type": "windows",
Expand Down Expand Up @@ -444,7 +451,7 @@
"name": "Windows - MQTT Troubleshooting Script | v1.0 JCCG",
"type": "windows",
"command": "# Parameters\n$BrokerAddress = \"a1hrq03pdcca60-ats.iot.us-east-1.amazonaws.com\"\n$BrokerPort = 443\n\n# Network Connectivity Checks\nfunction Test-NetworkConnectivity {\n Write-Host \"1. Network Connectivity Checks\" -ForegroundColor Cyan\n \n # DNS Resolution\n Write-Host \" Checking DNS Resolution...\" -ForegroundColor Gray\n try {\n $resolvedIP = (Resolve-DnsName $BrokerAddress -ErrorAction Stop)[0].IPAddress\n Write-Host \" DNS Resolution Successful: $resolvedIP\" -ForegroundColor Green\n }\n catch {\n Write-Host \" DNS Resolution Failed: $($_.Exception.Message)\" -ForegroundColor Red\n return $false\n }\n\n # Ping Test\n Write-Host \" Performing Ping Test...\" -ForegroundColor Gray\n $pingResult = Test-Connection -ComputerName $BrokerAddress -Count 4 -Quiet\n if ($pingResult) {\n Write-Host \" Ping Successful\" -ForegroundColor Green\n }\n else {\n Write-Host \" Ping Failed\" -ForegroundColor Red\n return $false\n }\n\n # Port Connectivity\n Write-Host \" Checking Port Connectivity...\" -ForegroundColor Gray\n $tcpClient = New-Object System.Net.Sockets.TcpClient\n try {\n $tcpClient.Connect($BrokerAddress, $BrokerPort)\n if ($tcpClient.Connected) {\n Write-Host \" Port $BrokerPort is Open\" -ForegroundColor Green\n $tcpClient.Close()\n }\n }\n catch {\n Write-Host \" Unable to Connect to Port ${BrokerPort}: $($_.Exception.Message)\" -ForegroundColor Red\n return $false\n }\n\n return $true\n}\n\n# TCP Connection Test\nfunction Test-TcpConnection {\n Write-Host \"2. TCP Connection Test\" -ForegroundColor Cyan\n \n try {\n $testNetConn = Test-Netconnection -Port ${BrokerPort} ${BrokerAddress} | Select-Object -Property TcpTestSucceeded -ExpandProperty TcpTestSucceeded\n if ($testNetConn) {\n Write-Host \" TCP Connection Successful\" -ForegroundColor Green\n return $true\n }\n else {\n Write-Host \" TCP Connection Failed\" -ForegroundColor Red\n return $false\n }\n }\n catch {\n Write-Host \" TCP Connection Error: $($_.Exception.Message)\" -ForegroundColor Red\n return $false\n }\n}\n\n# Main Troubleshooting Function\nfunction Start-MQTTTroubleshooting {\n Clear-Host\n Write-Host \"MQTT Troubleshooting Script\" -ForegroundColor Magenta\n Write-Host \"===========================\" -ForegroundColor Magenta\n \n # Perform Network Checks\n $networkCheckPassed = Test-NetworkConnectivity\n \n # If network checks pass, proceed with TCP connection test\n if ($networkCheckPassed) {\n $tcpConnectionPassed = Test-TcpConnection\n \n if ($tcpConnectionPassed) {\n Write-Host \"`nTroubleshooting Complete: All Tests Passed\" -ForegroundColor Green\n }\n else {\n Write-Host \"`nTroubleshooting Complete: Some Tests Failed\" -ForegroundColor Yellow\n }\n }\n else {\n Write-Host \"`nTroubleshooting Stopped: Network Connectivity Issues Detected\" -ForegroundColor Red\n }\n}\n\n# Run the troubleshooting script\nStart-MQTTTroubleshooting",
"link": "https://github.com/TheJumpCloud/support/blob/master/PowerShell/JumpCloud%20Commands%20Gallery/Windows%20Commands/Windows%20-%20MQTT%20troubleshooting.md",
"link": "https://github.com/TheJumpCloud/support/blob/master/PowerShell/JumpCloud%20Commands%20Gallery/Windows%20Commands/Windows%20-%20MQTT%20Troubleshooting.md",
"description": "This script helps diagnose common MQTT connectivity and communication issues by performing various network checks."
}
]

0 comments on commit b2410ab

Please sign in to comment.