Created
June 4, 2022 12:52
-
-
Save ReinforceZwei/00b424c8673411bf6ee031edeafa286d to your computer and use it in GitHub Desktop.
Powershell script to reduce typing SSH command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#member -in $args | |
[CmdletBinding(PositionalBinding=$false)] | |
param( | |
[Parameter(Position=0)] | |
[string]$Name, | |
[switch]$Add, | |
[switch]$List, | |
[switch]$Help, | |
[Parameter( | |
ValueFromRemainingArguments = $true | |
)] | |
[Object[]]$Remain | |
) | |
function Add-Host { | |
param( | |
[string]$Name, | |
[string]$Hostname, | |
[string]$Username, | |
[string]$AuthType, | |
[string]$Password, | |
[string]$KeyPath | |
) | |
$sshHosts.add($Name, [PSCustomObject]@{ | |
Name = $Name; | |
Host = $Hostname; | |
Username = $Username; | |
AuthType = $AuthType; | |
Password = $Password; | |
KeyPath = $KeyPath; | |
}) | |
Save-Config | |
} | |
function Save-Config { | |
ConvertTo-Json $sshHosts | Out-File $fullPath | |
} | |
function Connect-Name { | |
param([string]$Name) | |
if ($sshHosts.ContainsKey($Name)) { | |
$hostname = $sshHosts[$Name].host | |
$username = $sshHosts[$Name].username | |
$password = $sshHosts[$Name].password | |
$cmd = "ssh $username@$hostname" | |
if ($sshHosts[$Name].authtype -eq 'key'){ | |
$keypath = $sshHosts[$Name].keypath | |
$cmd += " -i ""$keypath""" | |
} | |
Invoke-Expression $cmd | |
exit | |
}else{ | |
echo "Unknown name $Name" | |
echo "Use 'sss -add $Name' to configurate this host" | |
exit | |
} | |
} | |
Add-Type -AssemblyName System.Windows.Forms | |
function Open-FileDialog { | |
$fd = New-Object System.Windows.Forms.OpenFileDialog | |
$fd.ShowDialog() | out-null | |
return $fd.FileName | |
} | |
$configFile = 'sss.json' | |
$fullPath = [System.IO.Path]::Combine($env:USERPROFILE, $configFile) | |
$sshHosts = @{} | |
if ([System.IO.File]::Exists($fullPath)){ | |
(Get-Content $fullPath | ConvertFrom-Json).psobject.properties | Foreach { $sshHosts[$_.Name] = $_.Value } | |
} | |
if ($Help) { | |
echo "Save your time typing SSH hostname and private key path" | |
echo "Usage:`n`tsss [name]`tConnect to host" | |
echo "Parameters:" | |
echo "`t-add [name]`tAdd a SSH host" | |
echo "`t-list`t`tList added hosts" | |
echo "`t-help`t`tShow this message" | |
exit | |
} | |
if ($add) { | |
if (-not $Name) {$Name = Read-Host "Friendly name"} | |
if ($sshHosts.ContainsKey($Name)) {echo "This name have been added."; exit} | |
$hostname = Read-Host "IP address or hostname" | |
$username = Read-Host "Login name" | |
$authtype = Read-Host "Auth type (pw or key)" | |
$password = '' | |
$keypath = '' | |
if ($authtype -eq 'key'){ | |
$keypath = Open-FileDialog | |
} | |
Add-Host $Name $hostname $username $authtype $password $keypath | |
echo "`nHost added" | |
exit | |
} | |
if ($sshHosts.Count -eq 0){ | |
echo "You haven't add any host. Use 'sss -add' to add a host" | |
exit | |
} | |
if ($List) { | |
$sshHosts.Values | select name | ft | |
exit | |
} | |
if ($Name) { | |
Connect-Name $Name | |
} | |
$sshHosts.Values | select name | ft | |
$Name = Read-Host "Enter name" | |
if (-not $Name) {exit} | |
Connect-Name $Name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment