I would like to automate ssh-keygen with an optional file and optional passphrase and overwrite if a file is found. If I write out the script in Terminal, it asks me three questions. I do not want it to prompt me at all basically and just run and return the response or return an error.
Here is my code so far:
#!/usr/bin/osascript
on run
set email to "myemail@email.com"
set result to do shell script "ssh-keygen -t rsa -C \"" & email & "\""
return result
end run
NOTE: This question is very similar but the poster does not mention passing in an optional file and does not mention how he handles files that exist already.
Specify the passphrase (or no passphrase) with -N
and the file location with -f
, and delete the keys first if they exist. For example:
set keyPath to "~/.ssh/my_rsa_key"
do shell script ("rm " & keyPath & " " & keyPath & ".pub &> /dev/null;:")
set email to "myemail@email.com"
set myPassPhrase to "abcdefgh"
do shell script ¬
"ssh-keygen -t rsa -C \"" & email & "\" -N \"" & myPassPhrase & "\" -f " & keyPath
(result
is an automatic variable assigned to the result of the last instruction, so you don't need to explicitly specify it, but you could replace it with some other variable name if you do want a variable assigned.)
Also, considering most of these operations are happening within do shell script
, you might be better off just doing the whole thing as a Bash script, e.g.:
#!/bin/bash
keyPath="~/.ssh/my_rsa_key"
email="myemail@email.com"
myPassPhrase="abcdefgh"
rm $keyPath $keyPath.pub &> /dev/null
ssh-keygen -t rsa -C "$email" -N "$myPassPhrase" -f "$keyPath"