port share
To create a custom shell command on your Mac for running the ssh arifpro@srv.us -R command with flexible port options, you can define a shell function or script. Here’s how to do it:
Requirements
- SSH Access: You need SSH access to the server
srv.uswith the usernamearifpro. - Shell Configuration: You should have a
.zshrcor.bashrcfile in your home directory. ssh-keygen -t ed25519to generate a new SSH key pair and add it to the github account to use github username to share the port.
Option 1: Add a Shell Function in Your .zshrc (or .bashrc)
-
Open your shell configuration file:
nano ~/.zshrc(Replace
.zshrcwith.bashrcif you’re using Bash.) -
Add the following function:
port() { local username="arifpro" local server="srv.us" local port_args="" # Process arguments for port in "$@"; do port_args="$port_args -R ${port}:localhost:${port}" done # Run the SSH command ssh "${username}@${server}" $port_args } -
Save and apply the changes:
source ~/.zshrc -
Usage:
-
For a single port:
port 5001 -
For multiple ports:
port 5001 3000
-
Option 2: Create an Executable Script
-
Create the script file:
nano ~/bin/port # or we can create the file and open it in VS Code # **Open Terminal**: mkdir -p ~/bin # **Create the Script File**: touch ~/bin/port # **Open the Script in VS Code**: code ~/bin/port(If
~/bindoesn’t exist, create it:mkdir ~/binand add it to yourPATHby editing~/.zshrcor~/.bashrcto includeexport PATH="$HOME/bin:$PATH".) -
Add the script content:
#!/bin/bash username="arifpro" server="srv.us" port_args="" # Process arguments for port in "$@"; do port_args="$port_args -R ${port}:localhost:${port}" done # Run the SSH command ssh "${username}@${server}" $port_args -
Make the script executable:
chmod +x ~/bin/port -
Usage:
-
For a single port:
port 5001 -
For multiple ports:
port 5001 3000
-
Explanation
- Dynamic Ports: The script/function loops through all provided arguments and creates
-Rmappings dynamically. - Custom Command: You use a simple command (
port) instead of the fullsshsyntax. - Reusable: Add as many ports as needed in one command.