Date Created: 2024-12-02
By: 16BitMiker
[ BACK.. ]
Remote access between macOS systems can streamline everything from remote troubleshooting to collaborative sessions. But with great power comes the need for great caution—especially when it comes to visibility and security. In this post, we’ll walk through a streamlined Perl one-liner that combines SSH tunneling and VNC for secure and quick Mac-to-Mac access.
Let’s break down exactly how it works, why it’s useful, and what you need to keep in mind when using it.
Here’s the complete Perl one-liner that sets up a secure SSH tunnel and launches a VNC session:
-::=':constants' - '
# Print the SSH tunnel command in green
say q|> |, GREEN $tunnel, RESET;
# Execute the SSH tunnel command
system $tunnel;
# Print the VNC open command in green
say q|> |, GREEN $vnc, RESET;
# Launch the VNC viewer to connect through the tunnel
system $vnc;
# Prompt the user to close the tunnel
print q|> |, UNDERLINE q|Hit enter to shut down the tunnel...|, RESET;
<STDIN>;
# Locate the SSH tunnel process by matching the exact command string
# Then extract the PID and forcefully kill it
qx(ps aux | grep "ssh -fNL 5901:localhost:5900" | grep -v grep)
=~ s~^\S+\s+(\d+)~system qq|kill -9 $1|;~xer;
# Indicate that the tunnel has been closed
say q|> |, RED q|Tunnel closed! Bye!|;
exit 0;
' -- -='open vnc://localhost:5901' -="ssh -fNL 5901:localhost:5900 \$IP"
-MTerm::ANSIColor=':constants'
: Loads the Term::ANSIColor module with color constants like GREEN
, RED
, etc., for enhanced terminal output readability.
-sE
: Enables command-line option parsing via -s
and executes the script with -E
, which allows say
and other modern Perl features.
$tunnel
: The SSH command that forwards your local port 5901 to the remote Mac’s port 5900 (used by VNC).
$vnc
: The command to open the VNC viewer, connecting to localhost:5901
.
system
: Executes shell commands directly from Perl.
ps aux | grep ... | kill -9
: Locates the SSH tunnel process and forcefully terminates it.
<STDIN>
: Waits for user input before tearing down the tunnel.
Before running the command, define the remote Mac's IP and login:
xxxxxxxxxx
export IP=username@192.168.1.100
Replace username@192.168.1.100
with the actual username and IP (or hostname) of the remote Mac.
Once the $IP
variable is set, run the Perl one-liner directly in your terminal:
xxxxxxxxxx
perl -MTerm::ANSIColor=':constants' -sE '...' -- -vnc='open vnc://localhost:5901' -tunnel="ssh -fNL 5901:localhost:5900 $IP"
This sets up the tunnel, opens the VNC client, and waits for you to hit enter before tearing it all down. 🧹
Make sure the remote Mac is configured correctly:
✅ Enable SSH: Go to System Preferences → Sharing → Check "Remote Login".
✅ Enable Screen Sharing: In Sharing, also check "Screen Sharing".
👥 User Access: Ensure your user has permission to access both SSH and Screen Sharing.
Security is critical when exposing any service remotely.
By tunneling VNC over SSH, you avoid exposing port 5900 to the network—this:
Encrypts the VNC traffic.
Prevents direct access to the VNC port from outside.
Adds another layer of authentication via SSH.
Use SSH keys instead of passwords for authentication.
Do not expose port 22 (SSH) to the internet unless firewalled and rate-limited.
Always verify the IP or hostname to avoid DNS spoofing.
Only connect to machines you own or have explicit authorization to access.
When you connect via VNC on macOS, you’re controlling the actual user session—not a headless or hidden desktop. That means:
🖥️ The screen of the remote Mac shows everything you're doing.
👥 Anyone physically near the Mac can see your session.
🔒 Sensitive information is vulnerable to shoulder-surfing.
There’s no stealth mode here. Always operate under the assumption that your session is observable unless you take extra steps (like locking the screen or using third-party virtual display tools).
This Perl one-liner delivers a compact, readable, and functional solution to remote Mac access. You get the best of both worlds:
🔒 Secure SSH tunneling
🚀 Instant VNC access
🧼 Clean shutdown with minimal effort
But just as important as setting up remote access is understanding the implications of using it. Screen visibility, user permissions, and secure authentication methods are all part of the equation.
This solution is great for:
IT administrators managing remote Macs
Developers needing to access systems across the room or across the country
Power users automating remote access workflows
Just use it responsibly, and always leave the tunnel cleaner than you found it. 🧽
Happy tunneling! 🧪