Skip to content

Instantly share code, notes, and snippets.

@starikcetin
Created August 10, 2021 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starikcetin/dc11649e7145fad4c972189efd89ed84 to your computer and use it in GitHub Desktop.
Save starikcetin/dc11649e7145fad4c972189efd89ed84 to your computer and use it in GitHub Desktop.
ssh setup scripts for windows
#############################################################################################################
#
# SSH connect test for Github, Gitlab, and Bitbucket.
#
# Copyright (c) 2021, S. Tarık Çetin.
# MIT licence.
#
#############################################################################################################
Write-Host "`n"
Write-Host "====== Connecting to Github via SSH"
ssh -T git@github.com
Write-Host "`n"
Write-Host "`n"
Write-Host "====== Connecting to Gitlab via SSH"
ssh -T git@gitlab.com
Write-Host "`n"
Write-Host "`n"
Write-Host "====== Connecting to Bitbucket via SSH"
ssh -T git@bitbucket.org
Write-Host "`n"
Write-Host "`n"
Write-Host "====== All done. You can close this window now."
#############################################################################################################
#
# WARNING: This script renames your existing .ssh folders to '.ssh.backup_DATETIME'. Use it with caution.
#
# SSH setup script.
#
# Copyright (c) 2021, S. Tarık Çetin.
# MIT licence.
#
#############################################################################################################
param([switch]$Elevated)
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Test-Admin) -eq $false) {
if ($elevated) {
Write-Host "======! Tried to elevate to admin priviliges, did not work, aborting. Try running the script as an Administrator."
} else {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}
function Out-FileUtf8NoBom {
[CmdletBinding()]
param(
[Parameter(Mandatory, Position=0)] [string] $LiteralPath,
[switch] $Append,
[switch] $NoClobber,
[AllowNull()] [int] $Width,
[switch] $UseLF,
[Parameter(ValueFromPipeline)] $InputObject
)
$dir = Split-Path -LiteralPath $LiteralPath
if ($dir) { $dir = Convert-Path -ErrorAction Stop -LiteralPath $dir } else { $dir = $pwd.ProviderPath}
$LiteralPath = [IO.Path]::Combine($dir, [IO.Path]::GetFileName($LiteralPath))
if ($NoClobber -and (Test-Path $LiteralPath)) {
Throw [IO.IOException] "The file '$LiteralPath' already exists."
}
$sw = New-Object System.IO.StreamWriter $LiteralPath, $Append
$htOutStringArgs = @{}
if ($Width) {
$htOutStringArgs += @{ Width = $Width }
}
try {
$Input | Out-String -Stream @htOutStringArgs | % {
if ($UseLf) {
$sw.Write($_ + "`n")
}
else {
$sw.WriteLine($_)
}
}
} finally {
$sw.Dispose()
}
}
Write-Host "`n"
Write-Host "====== Installing OpenSSH Client"
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
Write-Host "`n"
Write-Host "====== Setting SSH agent to automatic start"
Set-Service ssh-agent -StartupType Automatic
Write-Host "`n"
Write-Host "====== Starting SSH agent"
Start-Service ssh-agent
$sshFolder = "$env:USERPROFILE\.ssh"
$userName = $env:username
$userName = $userName.replace(' ','-')
$hostName = hostname
$hostName = $hostName.replace(' ','-')
$dateTime = Get-Date -UFormat "%Y-%m-%d_%H-%M"
$keyId = ($userName + "@" + $hostName + "_" + $dateTime)
$keyFilePath = ($sshFolder + "\" + $keyId)
$knownHostsFilePath = ($sshFolder + "\" + "known_hosts")
$sshBackupName = ".ssh.backup_$dateTime"
Write-Host "`n"
Write-Host "====== Renaming directory $sshFolder to $sshBackupName"
Rename-Item -LiteralPath "$sshFolder" -NewName "$sshBackupName"
Write-Host "`n"
Write-Host "====== Renaming directory $env:USERPROFILE\Documents\.ssh to $sshBackupName"
Rename-Item -LiteralPath "$env:USERPROFILE\Documents\.ssh" -NewName "$sshBackupName"
Write-Host "`n"
Write-Host "====== Creating directory $sshFolder"
mkdir $sshFolder
Write-Host "`n"
Write-Host "====== Generating an SSH key (rsa 4096, no passphrase) to $keyFilePath"
ssh-keygen -t rsa -b 4096 -C "$keyId" -f "$keyFilePath" -N """"
Write-Host "`n"
Write-Host "====== Adding the generated SSH key to SSH agent"
ssh-add "$keyFilePath"
Write-Host "`n"
Write-Host "====== Writing the SSH config file"
echo "IdentityFile $keyFilePath" >> "$sshFolder\config"
echo "UserKnownHostsFile $sshFolder\known_hosts" >> "$sshFolder\config"
echo "HashKnownHosts false" >> "$sshFolder\config"
echo "" >> "$sshFolder\config"
echo "Host github.com" >> "$sshFolder\config"
echo " HostName github.com" >> "$sshFolder\config"
echo " StrictHostKeyChecking false" >> "$sshFolder\config"
echo "" >> "$sshFolder\config"
echo "Host gitlab.com" >> "$sshFolder\config"
echo " HostName gitlab.com" >> "$sshFolder\config"
echo " StrictHostKeyChecking false" >> "$sshFolder\config"
echo "" >> "$sshFolder\config"
echo "Host bitbucket.org" >> "$sshFolder\config"
echo " HostName bitbucket.org" >> "$sshFolder\config"
echo " StrictHostKeyChecking false" >> "$sshFolder\config"
(Get-Content "$sshFolder\config") | Out-FileUtf8NoBom "$sshFolder\config" # Add -UseLF for Unix newlines
Write-Host "`n"
Write-Host "====== Launching Github, Gitlab, and Bitbucket SSH settings pages"
start chrome "/new-window https://github.com/settings/keys https://gitlab.com/-/profile/keys https://bitbucket.org/account/settings/ssh-keys/"
Write-Host "`n"
Write-Host "====== Opening the generated public key file"
notepad.exe "$keyFilePath.pub"
Write-Host "`n"
Write-Host "`n"
Write-Host "=========================================================================================="
Write-Host "`n"
Write-Host "Now you need to copy your public key and add it to your Git remotes. Follow these steps:"
Write-Host "1. Your public key is opened for you in Notepad. Copy everything in it."
Write-Host "2. Settings pages of Github, Gitlab, and Bitbucket are opened in a Chrome window:"
Write-Host " i. (optional) Remove the existing keys on the ones that you use."
Write-Host " ii. Add the public key you copied to the ones that you use."
Write-Host "`n"
Write-Host "Important paths you might want to remember for when you need this SSH key the future:"
Write-Host "* .ssh folder: $env:USERPROFILE\.ssh"
Write-Host "* Public key: $keyFilePath.pub"
Write-Host "* Private key: $keyFilePath"
Write-Host "`n"
Write-Host "IMPORTANT: Do not share your PRIVATE key with anyone!"
Write-Host "The only keys you should be sharing are PUBLIC keys (those that end with .pub extension)."
Write-Host "`n"
Write-Host "=========================================================================================="
Write-Host "`n"
Write-Host "`n"
Write-Host "====== All done. You can close this window now."
Write-Host "`n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment