Created
November 16, 2016 06:29
-
-
Save nishimura/41e7ebb3ca1ecd8744b56777a79353d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -ue -o pipefail | |
export LC_ALL=C | |
DATETIME=`date +"%Y%m%d%H%M%S"` | |
USER="" | |
if [ $# -ge 1 ]; then | |
case "$1" in | |
--help|-h) | |
echo "Usage: pubkey [<username> [<url>]]" | |
echo "" | |
exit 0 | |
;; | |
*) | |
USER=$1 | |
esac | |
else | |
echo "User name: " | |
array=() | |
for home in `ls /home`; do | |
array+=($home) | |
echo -e "\t${#array[@]}) $home" | |
done | |
read -p "select user number: " NO | |
if [ -z "$NO" ]; then | |
exit | |
fi | |
if [ $NO -le ${#array[@]} ]; then | |
USER=${array[NO-1]} | |
else | |
exit 1 | |
fi | |
fi | |
KEYFILE="/home/$USER/.ssh/authorized_keys" | |
if [ -f $KEYFILE -a -s $KEYFILE ]; then | |
awk '{gsub(/^$/,"unknown",$3);print NR, $3}' $KEYFILE | |
echo | |
echo -e "\t1) add" | |
echo -e "\t2) delete" | |
read -p "select action number: " NO | |
if [ "$NO" = "1" ]; then | |
: | |
elif [ "$NO" = "2" ]; then | |
read -p "delete number: " NO | |
cp -a $KEYFILE $KEYFILE.$DATETIME | |
sed -i -e "${NO}d" $KEYFILE | |
echo "" | |
echo "Success!" | |
echo -e "\tBackup file: $KEYFILE.$DATETIME" | |
exit 0 | |
: | |
else | |
exit 0 | |
fi | |
fi | |
cp -a $KEYFILE $KEYFILE.$DATETIME | |
echo | |
echo "add public key from url." | |
if [ $# -ge 2 ]; then | |
URL=$2 | |
else | |
read -p "URL: " URL | |
fi | |
read -p "Comment (whose key is it?): " COMMENT | |
if [ -z $COMMENT ]; then | |
exit 1 | |
fi | |
echo | |
echo -e "get public key file from <$URL>\n and add to $KEYFILE" | |
echo -e "\tComment: $COMMENT" | |
echo | |
echo -e "\t1) OK" | |
echo -e "\t2) NG" | |
read -p "OK? :" NO | |
if [ "$NO" != "1" ]; then | |
exit 0 | |
fi | |
wget $URL -O - | awk "{gsub(/^.*\$/,\"$COMMENT\",\$3);print}" >> $KEYFILE | |
echo "Success!" | |
echo -e "\tBackup file: $KEYFILE.$DATETIME" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment