Delete a Key from authorized_keys
#!/bin/bash
#/usr/local/bin/delete_key
echo " "
echo " "
echo " "
echo "current number of keys before deletion: " $(cat /home/user/.ssh/authorized_keys |wc -l)
echo " "
echo "Backing up keys..."
cp -fv /home/user/.ssh/authorized_keys /home/user/.ssh/authorized_keys.$(date +%Y.%m.%d)
echo " "
if [ $1 ]; then
keytodelete=$1
if [ ${#keytodelete} -gt 4 ]; then
keys=$(cat /home/user/.ssh/authorized_keys |grep $keytodelete |awk '{print $3}')
echo "key(s) will be deleted: " $keys
echo " "
if [ $2 ]; then
if [ $2 == "yes" ]; then
sed -i /$1/d /home/user/.ssh/authorized_keys;
echo "current number of keys after deletion: " $(cat /home/user/.ssh/authorized_keys |wc -l)
fi
else
echo "The above keys will be deleted, if this is what you want run again with 'yes' as your second argument"
fi
fi
else
echo "Key you want to delete must be at least 5 chars..."
echo "Syntax: delete_key npp56"
fi
Backup Keys
#!/bin/bash
#/usr/local/bin/backup_keys
echo "current number of keys: " $(cat /home/user/.ssh/authorized_keys |wc -l)
cp -fv /home/user/.ssh/authorized_keys /home/user/.ssh/authorized_keys.$(date +%Y.%m.%d)
Show Keys
#!/bin/bash
#/usr/local/bin/show_keys
echo "current number of keys: " $(cat /home/user/.ssh/authorized_keys| wc -l)
echo " "
echo " "
echo "SSH Keys:"
echo " "
cat /home/user/.ssh/authorized_keys |awk '{print $3}'
echo " "
echo " "
No comments:
Post a Comment