Skip to content

Instantly share code, notes, and snippets.

@tobiasehlert
Last active March 1, 2020 11:13
Show Gist options
  • Save tobiasehlert/4b0d23832a30f4e322c5cb35d931a82a to your computer and use it in GitHub Desktop.
Save tobiasehlert/4b0d23832a30f4e322c5cb35d931a82a to your computer and use it in GitHub Desktop.
Lindberg server toolkit
#!/bin/sh
#
# add-sshkey-and-rights.sh
# Script by Tobias Lindberg
#
# Print what the script does..
scriptname=`basename "$0"`
printf "Starting $scriptname\n"
printf "Description: add ssh-keys defined below to specified users home-directory to .ssh/authorized_keys file\n\n"
# Getting username, in case this is run with sudo
if [ $SUDO_USER ]
then
username=$SUDO_USER
sudo=true
else
username=`whoami`
sudo=false
fi
# Ask user for username to add ssh-key too
read -p "Enter name of user you want to add ssh-key to: [$username] " user
# Filling $user with userif
if [ -z "$user" ]
then
user=$username
fi
# Check if user is not sudo and ask if he wants to get sudo level
if [ $sudo = false ]
then
# Check if user is not the same as the user that is target of change
if [ $user != $username ]
then
printf "ERROR: You will not succeed adding your key to user $user, since you are not running with sudo and require more rights!\n"
read -p "DO you wan to continue by getting sudo rights (yes/no)? [yes] " request_sudo
# Set default value to request_sudo to yes
if [ -z "$request_sudo" ]
then
request_sudo="yes"
fi
# Check if yes was selected and then execute a sudo command and create prefix
if [ $request_sudo = "yes" ] || [ $request_sudo = "y" ] || [ $request_sudo = "Yes" ] || [ $request_sudo = "Y" ]
then
sudo printf "Now we can continue with sudo..\n"
prefix_sudo="sudo"
else
printf "ERROR: Failed to continue, since we are missing rights.. stopping script!\n\n"
exit 1
fi
fi
fi
# Checking if it's a value user
id -u $user > /dev/null 2>&1
if [ $? -ne 0 ]
then
printf "ERROR: User $user doesn\'t exists.. please validate and re-run the script!\n"
exit 1
fi
# Defining dirs to work in..
if [ $user != $username ]
then
homedir=$( getent passwd "$user" | cut -d: -f6 )
else
homedir=$HOME
fi
sshdir=$homedir/.ssh
authorized_keys_file=$sshdir/authorized_keys
# Setting right umask
umask 0077
# Creating .ssh folder, if it doesn't exists
$prefix_sudo mkdir -p $sshdir
# The [email protected] ssh key
sshrsa_tobiasehlertgmailcom_key="ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAu48hZJJwQUjfQEXE637P6KdGi3cTs+B5B2V2ZLdVeCHS5dMQ8HDAbRuGvxuK9Ty3OMqylQJ66UDa4dzMOzvnxZnThcP6bJ5e5ZoLP0BVe2c25HYSmJYt+4KTaur/OJmVuMaWiY/vAQRpjREl8z6UPh8/QibBDSreuk06Ln+jcFho5pXdvq+nmHxC8pyJV4QNSbXk+dwkeBMgimXtO3Tn6Dg7WR2Rnxt7Run5r9GfplWFobweT/AupXFYFJ9FJrXQXXIgdxh9h2wX4cqemUtD0kcIrskGdfWHWAsyt1UdJo0rW3nsuja+emTnd1e1YIhJ16ZaxlSdmBVnqDuYn7+7nQ== [email protected]"
# Check if authorized_Keys exists and adding key to file.. or creating the file and adding key to file
if [[ -f $authorized_keys_file ]]
then
# Searching for the file and adding all the keys, that should be inserted there.. that don't exist yet
printf "Found $authorized_keys_file file.\n"
# sshrsa_tobiasehlertgmailcom_key
printf "Looking for key sshrsa_tobiasehlertgmailcom_key in $authorized_keys_file.. adding if needed!\n"
$prefix_sudo grep -q -F "$sshrsa_tobiasehlertgmailcom_key" $authorized_keys_file || $prefix_sudo echo $sshrsa_tobiasehlertgmailcom_key >> $authorized_keys_file
else
# Could not find file.. so adding the different keys now
printf "Could not find $authorized_keys_file.\n"
# sshrsa_tobiasehlertgmailcom_key
printf "Adding sshrsa_tobiasehlertgmailcom_key to $authorized_keys_file\n."
$prefix_sudo echo $sshrsa_tobiasehlertgmailcom_key >> $authorized_keys_file
fi
printf "Finished adding key(s) to $authorized_keys_file!\n"
# Adjusting rights of the folder
printf "Performing chmod and chown commands on: $sshdir\n"
$prefix_sudo chmod -R go= $sshdir
$prefix_sudo chown -R $user:$user $sshdir
printf "Complete!\nScript done!\n"
exit 0
#!/bin/sh
#
# centos-software-installation.sh
# Script by Tobias Lindberg
#
# Print what the script does..
scriptname=`basename "$0"`
printf "Starting $scriptname\n"
printf "Description: installing software to CentOS servers\n\n"
# Defining what softwares to install
SOFTWARE="git vim open-vm-tools epel-release python3 httpd-tools"
# Telling user what is being installed
printf "Starting installation of: $SOFTWARE\n"
# Creating car for later use
prefix_sudo=""
# Check if the user is not run with root rights
if (( $EUID != 0 ))
then
# Telling user that the command is missing root rights, but we inject sudo
printf "ERROR: You will not succeed, since we are missing rights!\n"
read -p "DO you wan to continue by getting sudo rights (yes/no)? [yes] " request_sudo
# Set default value to request_sudo to yes
if [ -z "$request_sudo" ]
then
request_sudo="yes"
fi
# Check if yes was selected and then execute a sudo command and create prefix
if [ $request_sudo = "yes" ] || [ $request_sudo = "y" ] || [ $request_sudo = "Yes" ] || [ $request_sudo = "Y" ]
then
sudo printf "Now we can continue with sudo..\n"
prefix_sudo="sudo"
else
printf "ERROR: Failed to continue, since we are missing rights.. stopping script!\n\n"
exit 1
fi
fi
# installing various software things..
$prefix_sudo dnf install -y $SOFTWARE
printf "Script done!\n"
exit 0

Lindberg server toolkit README file

Repo contains

  • setup.sh to as starter script to select what to do
  • add-sshkey-and-rights.sh to add ssh-key(s) to user(s)
  • centos-software-installation.sh to install software on servers

Installation

git clone https://gist.github.com/4b0d23832a30f4e322c5cb35d931a82a.git lindberg-server-toolkit; cd lindberg-server-toolkit; sh setup.sh

Do require to have git installed on your system, to perform a git clone.

Author

Tobias Lindberg

#!/bin/sh
#
# setup.sh
# Script by Tobias Lindberg
#
# Getting username, in case this is run with sudo
if [ $SUDO_USER ]
then
username=$SUDO_USER
sudo=true
else
username=`whoami`
sudo=false
fi
printf "\n"
printf "Hi $username!!\n"
printf "This script is there, so that you can easy choose what you want to do on the server.\n\n"
# Doing a while loop so that you acn run several options after each other..
while true
do
# Asking the user to return a value
PS3="Enter the value of the script you want to run: "
# set menu_option list
select menu_option in add-sshkey-and-rights centos-software-installation exit
do
case $menu_option in
add-sshkey-and-rights|centos-software-installation)
printf "You choose: $menu_option.sh\n\n"
sh $menu_option.sh
printf "\n"
;;
exit)
printf "\nExiting application..\nScript done!\n\n"
exit 0
;;
*)
printf "ERROR: You need to choose a numeric value above\n"
;;
esac
done
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment