raspiblitz/home.admin/config.scripts/internet.sshpubkey.sh

42 lines
1.1 KiB
Bash
Raw Normal View History

2019-04-16 01:50:41 +01:00
#!/bin/bash
# command info
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
echo "# config script to init/show/transfer ssh pub keys."
2019-04-28 14:40:23 +02:00
echo "# -> return pubkey (and will init if needed):"
2019-04-16 01:50:41 +01:00
echo "# internet.sshpubkey.sh get"
2019-04-28 14:40:23 +02:00
echo "# -> transfer ssh-pub to a authorizedkey of remote server:"
2019-04-16 01:50:41 +01:00
echo "# internet.sshpubkey.sh transfer [REMOTEUSER]@[REMOTESERVER]"
echo "err='just informational output'"
exit 1
fi
# 1. parameter MODE
MODE="$1"
2020-07-06 17:37:28 +02:00
# root as default user
2019-04-16 01:50:41 +01:00
# its used for all ssh tunnel/back action
# make sure the ssh keys for that user are initialized
2019-04-16 02:04:57 +01:00
sshKeysExist=$(sudo ls /root/.ssh/id_rsa.pub | grep -c 'id_rsa.pub')
2019-04-16 01:50:41 +01:00
if [ ${sshKeysExist} -eq 0 ]; then
2019-04-16 02:04:57 +01:00
echo "# generation SSH keys for user root"
sudo mkdir /root/.ssh 2>/dev/null
sudo sh -c 'yes y | sudo ssh-keygen -b 2048 -t rsa -f /root/.ssh/id_rsa -q -N ""'
2019-04-16 01:50:41 +01:00
fi
if [ "${MODE}" == "get" ]; then
# get ssh pub key and print
2019-04-16 02:04:57 +01:00
sshPubKey=$(sudo cat /root/.ssh/id_rsa.pub)
echo "user='root'"
2019-04-16 01:50:41 +01:00
echo "sshPubKey='${sshPubKey}'"
elif [ "${MODE}" == "transfer" ]; then
2019-04-16 02:10:58 +01:00
sudo ssh-copy-id $2
2019-04-16 01:50:41 +01:00
else
2020-07-06 17:37:28 +02:00
echo "err='parameter not known - run with -help'"
2019-04-16 01:50:41 +01:00
fi