Skip to content

Instantly share code, notes, and snippets.

@JohnMeyerhoff
Last active March 18, 2019 09:14
Show Gist options
  • Save JohnMeyerhoff/50e774dc1d65d6d54b6978324949fa8e to your computer and use it in GitHub Desktop.
Save JohnMeyerhoff/50e774dc1d65d6d54b6978324949fa8e to your computer and use it in GitHub Desktop.
Creating a new ssh config for ssh connections to a server.
if [ $# -ne 2 ]; then
echo "this script works for ~/.ssh/ and uses the filename from \n
your first argument and the server from your second argument"
echo "example: $1 keyname_rsa username@serveraddress.net"
exit 1
fi
#not checking if second argument contains @.
echo "generating ssh-key with specified name:"
echo ~/.ssh/$1 | ssh-keygen
echo "next please enter the password for the server:"
ssh-copy-id -i ~/.ssh/$1 $2
address="$(cut -d'@' -f2 <<<$2)"
user="$(cut -d'@' -f1 <<<$2)"
echo "now go edit your ~/.ssh/config"
echo "would you like to automatically fill in this suggestion:"
echo "Host $1"
echo " HostName $address
User $user
IdentityFile ~/.ssh/$1\n"
echo "press y for the suggestion or n to exit."
echo "Do that? [Y,n]"
read input
if [[ $input == "Y" || $input == "y" ]]; then
echo "ok, inserting into ~/.ssh/config ..."
echo "\nHost $1\n HostName $address
User $user
IdentityFile ~/.ssh/$1\n">> ~/.ssh/config
echo "done :)"
else
echo "okay, bye!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment