From e6bc2294790481611742c0ae8c26f7760e6c5b8d Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 30 Jun 2020 11:45:58 -0600 Subject: [PATCH] add ssh-pubkey.ps1 --- ssh-pubkey/install.ps1 | 1 + ssh-utils/ssh-pubkey.ps1 | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 120000 ssh-pubkey/install.ps1 create mode 100644 ssh-utils/ssh-pubkey.ps1 diff --git a/ssh-pubkey/install.ps1 b/ssh-pubkey/install.ps1 new file mode 120000 index 0000000..f874a74 --- /dev/null +++ b/ssh-pubkey/install.ps1 @@ -0,0 +1 @@ +../ssh-utils/ssh-pubkey.ps1 \ No newline at end of file diff --git a/ssh-utils/ssh-pubkey.ps1 b/ssh-utils/ssh-pubkey.ps1 new file mode 100644 index 0000000..3af6a59 --- /dev/null +++ b/ssh-utils/ssh-pubkey.ps1 @@ -0,0 +1,46 @@ +#!/usr/bin/env pwsh + +# TODO: can we use some of this? +# https://github.com/PowerShell/openssh-portable/blob/latestw_all/contrib/win32/openssh/FixUserFilePermissions.ps1 + +if (!(Test-Path -Path "$Env:USERPROFILE/.ssh")) +{ + New-Item -Path "$Env:USERPROFILE/.ssh" -ItemType Directory + & icacls "$Env:USERPROFILE/.ssh" /inheritance:r + & icacls "$Env:USERPROFILE/.ssh" /grant:r "%username%":"(F)" +} + +if (!(Test-Path -Path "$Env:USERPROFILE/.ssh/config")) +{ + New-Item -Path "$Env:USERPROFILE/.ssh/config" -ItemType "file" -Value "" + & icacls "$Env:USERPROFILE/.ssh/config" /inheritance:r + & icacls "$Env:USERPROFILE/.ssh/config" /grant:r "%username%":"(F)" +} + +if (!(Test-Path -Path "$Env:USERPROFILE/.ssh/authorized_keys")) +{ + New-Item -Path "$Env:USERPROFILE/.ssh/authorized_keys" -ItemType "file" -Value "" + & icacls "$Env:USERPROFILE/.ssh/authorized_keys" /inheritance:r + & icacls "$Env:USERPROFILE/.ssh/authorized_keys" /grant:r "%username%":"(F)" +} + +if (!(Test-Path -Path "$Env:USERPROFILE/.ssh/id_rsa")) +{ + & ssh-keygen -b 2048 -t rsa -f "$Env:USERPROFILE/.ssh/id_rsa" -q -N "" + echo "" +} + +if (!(Test-Path -Path "$Env:USERPROFILE/.ssh/id_rsa.pub")) +{ + & ssh-keygen -y -f "$Env:USERPROFILE/.ssh/id_rsa" > "$Env:USERPROFILE/.ssh/id_rsa.pub" + echo "" +} + +# TODO use the comment (if any) for the name of the file +echo "" +echo "~/Downloads/id_rsa.$Env:USERNAME.pub": +echo "" +#rm -f "$Env:USERPROFILE/Downloads/id_rsa.$Env:USERNAME.pub": +Copy-Item -Path "$Env:USERPROFILE/.ssh/id_rsa.pub" -Destination "$Env:USERPROFILE/Downloads/id_rsa.$Env:USERNAME.pub" +& type "$Env:USERPROFILE/Downloads/id_rsa.$Env:USERNAME.pub" +echo "" -- 2.25.1