Skip to content

Instantly share code, notes, and snippets.

@kernelkaribou
Last active June 17, 2023 15:56
Show Gist options
  • Save kernelkaribou/f6af4a1c8b6a62983bd98d2f25ab3e7d to your computer and use it in GitHub Desktop.
Save kernelkaribou/f6af4a1c8b6a62983bd98d2f25ab3e7d to your computer and use it in GitHub Desktop.
Flexible script for ssh aliases in Windows
#################################################################################
# Summary:
# This basic function is for when you have multiple servers you use login
# aliases but would not like to manage adding many aliases, especially when
# consistent with minor changes, like hostname.
#
# Setup:
# This script assumes you are on Windows. Add the below function to your
# powershell profile. This can be easily edited (or created) with the
# command:
# notepad $PROFILE.CurrentUserCurrentHost
#
# Usage:
# # Simply update the server list in a "host" = "user" syntax, and update domain
# and port variables. This script assumes you use the same ports and domains
# for the servers to access them. It also assumes you have an SSH agent running
# that will server the SSH keys. If you do not, update the $server_connection
# variable to include the path to your ssh key. Should look similar to:
# "ssh -p $base_port..."
# to
# "ssh -i /path/to/rsa.key -p $base_port..."
#
# After which simply type: "lab servername"
#
# Author:
# kernelkaribou@github
# Last Updated:
# 06/17/2023
#################################################################################
function lab {
# Server List, Syntax: "host" = "user";
$server_list = @{
"serv01" = "admin"
}
$server = $args[0]
$base_domain = "home.lab"
$ssh_port = "22"
if($server_list.ContainsKey($server))
{
$server_connection="ssh -p $ssh_port $($server_list["$server"])@$server.$base_domain"
Invoke-expression $server_connection
}
else
{
Write-Host "$server is unknown"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment