#!/bin/zsh # ROS2Lab - Check my PC (macOS 12 or newer, Apple Silicon or Intel). # # curl --proto '=https' --tlsv1.2 -fsSL https://www.ros2lab.com/install/check-mac.sh | zsh # # STATUS: NOT yet run on a real Mac (written and tested on Linux with stand-in commands). # READ-ONLY. It installs nothing, changes nothing, needs no password and writes no file (no temp files either). # It never calls git, python3, cc or make, so it cannot start the Xcode command-line-tools install prompt. # What it reads: sw_vers, uname -m, sysctl (chip, processor, cores, memory, kern.hv_support), df (free space of your # home folder), system_profiler SPDisplaysDataType (graphics chip and Metal), whether /Applications/UTM.app exists, and # one download from packages.ros.org (the ROS 2 package list, up to 8 seconds, thrown away) to see your internet speed. # Where the result goes: it is printed here. Then it is kept ready on THIS computer only (127.0.0.1, port 7776) for up to # 10 minutes, so that the ROS2Lab page open in your browser can read it when you press "Read the result from my PC". # Nothing is sent to the internet. Ctrl+C stops it at any time. ROS2LAB_NO_SERVE=1 prints only. # Syntax kept valid for both zsh and bash 3.2. Avoid zsh special names (path, status) and words starting with "=". ros2lab_check() { ORIGIN="${ROS2LAB_ORIGIN:-https://www.ros2lab.com}"; PORT="${ROS2LAB_CHECK_PORT:-7776}" have() { command -v "$1" >/dev/null 2>&1; } # one line, printable ASCII only, single spaces; empty -> unknown one() { o1=$(LC_ALL=C tr -c ' -~' ' ' | sed 's/ */ /g; s/^ //; s/ $//'); [ -n "$o1" ] || o1=unknown; printf '%s' "$o1"; } num() { case "$1" in ''|*[!0-9]*) return 1;; esac; return 0; } joinp() { awk 'NF{ if (n++) printf " + "; printf "%s", $0 }'; } osv=$(sw_vers -productVersion 2>/dev/null | one) osb=$(sw_vers -buildVersion 2>/dev/null | one) osn=unknown; [ "$osv" != unknown ] && osn="macOS $osv" arch=$(uname -m 2>/dev/null | one) a64=$(sysctl -n hw.optional.arm64 2>/dev/null); tr64=$(sysctl -n sysctl.proc_translated 2>/dev/null) chip=unknown if [ "$a64" = 1 ] || [ "$tr64" = 1 ]; then chip=apple elif [ "$arch" = x86_64 ]; then chip=intel; fi cpu=$(sysctl -n machdep.cpu.brand_string 2>/dev/null | one) cores=$(sysctl -n hw.ncpu 2>/dev/null | one); num "$cores" || cores=unknown mem=$(sysctl -n hw.memsize 2>/dev/null | one) ram_gb=unknown; num "$mem" && ram_gb=$(awk -v b="$mem" 'BEGIN{printf "%.1f", b/1073741824}') # APFS "purgeable" space is not counted: Finder may show more free space than this. kb=$(df -k "$HOME" 2>/dev/null | awk 'NR==2{print $4}') disk_free_gb=unknown; num "$kb" && disk_free_gb=$(awk -v k="$kb" 'BEGIN{printf "%.1f", k/1048576}') sp=$(system_profiler SPDisplaysDataType 2>/dev/null) gpu=$(printf '%s\n' "$sp" | awk -F': ' '/Chipset Model:/{print $2}' | joinp | one) metal=$(printf '%s\n' "$sp" | awk -F': ' '/Metal Support:|Metal Family:/{print $2; exit}' | one) hv=$(sysctl -n kern.hv_support 2>/dev/null) hv_support=unknown; [ "$hv" = 1 ] && hv_support=yes; [ "$hv" = 0 ] && hv_support=no utm=no; [ -d /Applications/UTM.app ] && utm=yes net_ros=no; net_mbps=0 if have curl; then s=$(curl -s -o /dev/null -w '%{speed_download} %{http_code}' --max-time 8 http://packages.ros.org/ros2/ubuntu/dists/noble/main/binary-amd64/Packages 2>/dev/null) case "$s" in *" 200") net_ros=yes; net_mbps=$(printf '%s' "$s" | awk '{printf "%.1f", $1*8/1000000}');; esac fi R="===== ROS2LAB PC CHECK 1 ===== system: mac os: $osn os_version: $osv os_build: $osb arch: $arch chip: $chip cpu: $cpu cores: $cores ram_gb: $ram_gb disk_free_gb: $disk_free_gb disk_path: your home folder gpu: $gpu metal: $metal hv_support: $hv_support utm: $utm net_ros: $net_ros net_mbps: $net_mbps ===== END =====" printf '\n%s\n\n' "$R" [ "${ROS2LAB_NO_SERVE:-0}" = 1 ] && return 0 if [ ! -x /usr/bin/nc ] && ! have nc; then echo "Copy the lines above (from the first ===== to the last =====) and paste them into the page."; return 0; fi echo "Now go back to the ROS2Lab page and press \"Read the result from my PC\"." echo "(The result waits on this computer only, for up to 10 minutes. Ctrl+C stops. Or copy the lines above and paste them into the page.)" NC=/usr/bin/nc; [ -x "$NC" ] || NC=nc CR=$(printf '\r') # One fixed answer for every connection (nc cannot choose its reply after reading the request): 200 with the block. # A browser preflight (OPTIONS) accepts any 2xx, so OPTIONS gets this 200 instead of 204. Content-Length plus # "Connection: close" make the browser close the connection, which ends nc (macOS nc has no -q, old ones no -N). RESP="HTTP/1.1 200 OK$CR Access-Control-Allow-Origin: $ORIGIN$CR Access-Control-Allow-Private-Network: true$CR Content-Type: text/plain; charset=utf-8$CR Cache-Control: no-store$CR Content-Length: ${#R}$CR Connection: close$CR $CR $R" start=$(date +%s) # Watchdog: after 10 minutes, knock once on the port so the waiting nc returns and the loop sees the time is up. ( n=0; while [ $n -lt 600 ]; do sleep 1; n=$((n+1)); done; curl -s -o /dev/null --max-time 2 "http://127.0.0.1:$PORT/stop" ) /dev/null 2>&1 & wd=$! trap 'kill "$wd" 2>/dev/null; printf "\nStopped. Nothing was sent.\n"; exit 0' INT TERM got=0; failed=0 while :; do req=$(printf '%s' "$RESP" | "$NC" -l 127.0.0.1 "$PORT" 2>/dev/null); rc=$? if [ "$rc" -ne 0 ] && [ -z "$req" ]; then failed=1; break; fi case "$req" in "GET /check "*) got=1; break;; esac [ $(( $(date +%s) - start )) -ge 600 ] && break done kill "$wd" 2>/dev/null; trap - INT TERM if [ "$failed" = 1 ]; then echo "Could not keep the result ready (port $PORT is busy or blocked). Copy the lines above and paste them into the page." elif [ "$got" = 1 ]; then echo "The page has read the result. Done." else echo "Stopped. Nothing was sent."; fi return 0 } ros2lab_check