this repo has no description
dotfiles
1# SSH Agent Functions
2# Mark Embling (http://www.markembling.info/)
3#
4# How to use:
5# - Place this file into %USERPROFILE%\Documents\WindowsPowershell (or location of choice)
6# - Import into your profile.ps1:
7# e.g. ". (Resolve-Path ~/Documents/WindowsPowershell/ssh-agent-utils.ps1)" [without quotes]
8# - Enjoy
9#
10# Note: ensure you have ssh and ssh-agent available on your path, from Git's Unix tools or Cygwin.
11
12if ($env:COMPUTERNAME -ne 'PC490') {
13 exit
14}
15
16# Retrieve the current SSH agent PId (or zero). Can be used to determine if there
17# is an agent already starting.
18function Get-SshAgent() {
19 $agentPid = [Environment]::GetEnvironmentVariable("SSH_AGENT_PID", "User")
20 if ([int]$agentPid -eq 0) {
21 $agentPid = [Environment]::GetEnvironmentVariable("SSH_AGENT_PID", "Process")
22 }
23
24 if ([int]$agentPid -eq 0) {
25 0
26 } else {
27 # Make sure the process is actually running
28 $process = Get-Process -Id $agentPid -ErrorAction SilentlyContinue
29
30 if(($process -eq $null) -or ($process.ProcessName -ne "ssh-agent")) {
31 # It is not running (this is an error). Remove env vars and return 0 for no agent.
32 [Environment]::SetEnvironmentVariable("SSH_AGENT_PID", $null, "Process")
33 [Environment]::SetEnvironmentVariable("SSH_AGENT_PID", $null, "User")
34 [Environment]::SetEnvironmentVariable("SSH_AUTH_SOCK", $null, "Process")
35 [Environment]::SetEnvironmentVariable("SSH_AUTH_SOCK", $null, "User")
36 0
37 } else {
38 # It is running. Return the PID.
39 $agentPid
40 }
41 }
42}
43
44# Start the SSH agent.
45function Start-SshAgent() {
46 # Start the agent and gather its feedback info
47 [string]$output = ssh-agent
48
49 $lines = $output.Split(";")
50 $agentPid = 0
51
52 foreach ($line in $lines) {
53 if (([string]$line).Trim() -match "(.+)=(.*)") {
54 # Set environment variables for user and current process.
55 [Environment]::SetEnvironmentVariable($matches[1], $matches[2], "Process")
56 [Environment]::SetEnvironmentVariable($matches[1], $matches[2], "User")
57
58 if ($matches[1] -eq "SSH_AGENT_PID") {
59 $agentPid = $matches[2]
60 }
61 }
62 }
63
64 # Show the agent's PID as expected.
65 Write-Host "SSH agent PID:", $agentPid
66}
67
68# Stop a running SSH agent
69function Stop-SshAgent() {
70 [int]$agentPid = Get-SshAgent
71 if ([int]$agentPid -gt 0) {
72 # Stop agent process
73 $proc = Get-Process -Id $agentPid
74 if ($proc -ne $null) {
75 Stop-Process $agentPid
76 }
77
78 # Remove all enviroment variables
79 [Environment]::SetEnvironmentVariable("SSH_AGENT_PID", $null, "Process")
80 [Environment]::SetEnvironmentVariable("SSH_AGENT_PID", $null, "User")
81 [Environment]::SetEnvironmentVariable("SSH_AUTH_SOCK", $null, "Process")
82 [Environment]::SetEnvironmentVariable("SSH_AUTH_SOCK", $null, "User")
83 }
84}
85
86# Add a key to the SSH agent
87function Add-SshKey() {
88 if ($args.Count -eq 0) {
89 # Add the default key (~/id_rsa)
90 ssh-add
91 } else {
92 foreach ($value in $args) {
93 ssh-add $value
94 }
95 }
96}
97
98# Start the agent if not already running; provide feedback
99#$agent = Get-SshAgent
100#if ($agent -eq 0) {
101# Write-Host "Starting SSH agent..."
102# Start-SshAgent # Start agent
103# Add-SshKey # Add my default key
104#} else {
105# Write-Host "SSH agent is running (PID $agent)"
106#}