#!/bin/sh # Moku · check Remote Login on this Mac and show how to connect. # https://portholelab.com/ssh · v2 (this file never changes; fixes ship as v3) # # What this does: # 1. Checks whether Remote Login (the Mac's SSH server) is on. # 2. If it's off, opens System Settings at Sharing and waits while you # switch Remote Login on yourself. # 3. Prints your username and this Mac's address, and opens a page with # a QR code for the Moku app to scan. # # What it does not do: ask for your password, use sudo, change any # setting itself, or send anything to anyone. # # Everything runs from main(), called on the very last line, so a download # cut short runs nothing. MOKU_PAGE='https://portholelab.com/ssh/' zh() { case "${LC_ALL:-${LC_MESSAGES:-${LANG:-}}}" in zh*) return 0 ;; esac # Terminal on a Chinese Mac often leaves LANG unset. defaults read -g AppleLanguages 2>/dev/null | sed -n '2p' | grep -q 'zh' } say() { if zh; then printf '%s\n' "$1"; else printf '%s\n' "$2"; fi; } ssh_on() { nc -z -G 1 127.0.0.1 22 >/dev/null 2>&1; } primary_ip() { dev=$(route -n get default 2>/dev/null | awk '/interface:/ {print $2; exit}') ip='' [ -n "$dev" ] && ip=$(ipconfig getifaddr "$dev" 2>/dev/null) [ -n "$ip" ] || ip=$(ipconfig getifaddr en0 2>/dev/null) printf '%s' "$ip" } main() { if [ "$(uname -s)" != Darwin ]; then say "这条命令是给 Mac 用的。请回到 portholelab.com/ssh 选你的系统。" \ "This command is for Macs. Go back to portholelab.com/ssh and pick your system." exit 1 fi if ssh_on; then say "远程登录已经打开。" "Remote Login is already on." else say "远程登录还没打开。正在打开「系统设置 › 通用 › 共享」……" \ "Remote Login is off. Opening System Settings › General › Sharing…" say "请打开「远程登录」开关。打开后这里会自动继续。" \ "Switch on \"Remote Login\". This continues by itself once it's on." open 'x-apple.systempreferences:com.apple.Sharing-Settings.extension' 2>/dev/null || open /System/Library/PreferencePanes/SharingPref.prefPane 2>/dev/null tries=0 until ssh_on; do tries=$((tries + 1)) if [ "$tries" -ge 300 ]; then say "等了 5 分钟还没打开。打开后再运行一次这条命令即可。" \ "Still off after 5 minutes. Run this command again once it's on." exit 1 fi sleep 1 done say "好了,远程登录已打开。" "Remote Login is on." fi addr=$(primary_ip) user=$(id -un) # Run through sudo (it isn't needed), still name the person who typed it. if [ "$user" = root ] && [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != root ]; then user=$SUDO_USER fi printf '\n\033[1;32m' say "完成。在 Moku 里填这些:" "Done. In Moku, enter:" printf '\033[0m' say " 用户名:$user" " Username: $user" say " 地址: ${addr:-?}" " Address: ${addr:-?}" say " 密码: 你开机登录 Mac 用的密码" " Password: the one you use to log in to this Mac" if [ -z "$addr" ]; then say "没找到这台 Mac 的网络地址。请确认它连着 Wi-Fi 或网线。" \ "Couldn't find this Mac's network address. Check it's on Wi-Fi or Ethernet." return 0 fi case "$user" in *[!A-Za-z0-9._-]*) target="$addr" ;; *) target="$user%40$addr" ;; esac # The page reads this after "#", which browsers never send to a server. url="${MOKU_PAGE}#show=ssh%3A%2F%2F$target:22" printf '\n' say "或者在 Moku 里点「扫码导入」,扫这个页面上的二维码:" \ "Or tap \"Scan QR code\" in Moku and scan the code on this page:" printf ' %s\n' "$url" open "$url" 2>/dev/null printf '\n' say "别让陌生人连你的 Wi-Fi:同一网络里的人都能尝试登录这台 Mac。" \ "Keep strangers off your Wi-Fi: anyone on the same network can try to log in to this Mac." } main "$@"