-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssh-tools.ps1
71 lines (59 loc) · 2.3 KB
/
ssh-tools.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
function global:ssh-which {
if (-not (Test-Path -Path ~/.ssh/.active -PathType Leaf)) {
echo "No active key yet"
} else {
echo "Active key: ""$(Get-Content ~/.ssh/.active)"""
}
}
function global:ssh-list {
echo "Existing profiles:"
Get-ChildItem -Path ~/.ssh -Name -Directory
}
function global:ssh-create {
if ($args.Count -lt 3) {
echo "[usage] ssh-create <profile> <email> ""<username>"""
return
}
$profile=$args[0]
$email=$args[1]
$username=$args[2]
echo "Generating new key for ""$profile"""
Remove-Item –Path ~/.ssh/$profile -Recurse -Erroraction 'silentlycontinue' > $null
mkdir ~/.ssh/$profile -Erroraction 'silentlycontinue' > $null
ssh-keygen -q -b 4096 -t rsa -f "$env:USERPROFILE/.ssh/$profile/id_rsa" -C "$email" -N """"
echo "[user]" " email = $email" " name = $username" "" | Set-Content -Path ~/.ssh/$profile/.gitconfig -Erroraction 'silentlycontinue' > $null
echo "Files created:"; Get-ChildItem -Path ~/.ssh/$profile -Name -File
}
function global:ssh-stash {
if ($args.Count -ge 1) {
$profile=$args[0]
echo $profile | Set-Content -Path ~/.ssh/.active -Erroraction 'silentlycontinue' > $null
}
if (-not (Test-Path -Path ~/.ssh/.active -PathType Leaf)) {
echo "No active key to stash"
return
}
$active=$(Get-Content ~/.ssh/.active)
mkdir ~/.ssh/$active -Erroraction 'silentlycontinue' > $null
Copy-Item -Force ~/.ssh/id_rsa* ~/.ssh/$active/ -Erroraction 'silentlycontinue' > $null
Copy-Item -Force ~/.ssh/known_hosts ~/.ssh/$active/ -Erroraction 'silentlycontinue' > $null
Copy-Item -Force ~/.gitconfig ~/.ssh/$active/ -Erroraction 'silentlycontinue' > $null
echo "SSH key stashed to ""$active"""
}
function global:ssh-switch {
if ($args.Count -lt 1) {
echo "[usage] ssh-switch <profile>"
return
}
$profile=$args[0]
if (-not (Test-Path -Path ~/.ssh/$profile/id_rsa -PathType Leaf)) {
echo "No profile ""$profile"" exists"
return
}
ssh-stash
Copy-Item -Force ~/.ssh/$profile/.gitconfig ~/ -Erroraction 'silentlycontinue' > $null
Copy-Item -Force ~/.ssh/$profile/id_rsa* ~/.ssh -Erroraction 'silentlycontinue' > $null
Copy-Item -Force ~/.ssh/$profile/known_hosts ~/.ssh -Erroraction 'silentlycontinue' > $null
echo $profile | Set-Content -Path ~/.ssh/.active -Erroraction 'silentlycontinue' > $null
ssh-which
}