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.us
with the usernamearifpro
. - Shell Configuration: You should have a
.zshrc
or.bashrc
file in your home directory. ssh-keygen -t ed25519
to 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
.zshrc
with.bashrc
if 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
~/bin
doesn’t exist, create it:mkdir ~/bin
and add it to yourPATH
by editing~/.zshrc
or~/.bashrc
to 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
-R
mappings dynamically. - Custom Command: You use a simple command (
port
) instead of the fullssh
syntax. - Reusable: Add as many ports as needed in one command.