marc:linux:bash_snippets
This is an old revision of the document!
Some useful bash snippets:
Checking if file exists:
if [[ -f ~/.ssh/id_rsa ]]
then
echo "id_rsa exists"
else
echo "id_rsa does not exist"
fi
Ask for variable input:
# Read Password echo -n Password: read -s password echo # Run Command echo $password
Ecnrypting / Decrypting file:
A useful tool for encrypting/decrypting files is ccrypt. Using this you can make use of git containing things like SSH keys:
apt-get install ccrypt -y #install ccrypt ccencrypt ~/.ssh/id_rsa* #encrypt private key ccdecrypt id_rsa.cpt -P "Enter password to decrypt file: " #decrypt private key mv id_rsa ~/.ssh/ && chown -R user: ~/.ssh #move private key and set ownership chmod 600 ~/.ssh/id_rsa #set private key permissions
Install a list of programs:
progs=(
mc
iotop
htop
tmux
wget
ccrypt
openssh-server
)
for program in "${progs[@]}"; do
if ! command -v "$program" >/dev/null 2>&1; then
apt-get install "$program" -y
fi
done
A simple menu (ssh):
#!/bin/bash
clear
echo """
##################################################################################################
# #
# Welcome to sshmenu #
# #
# Which server would you like to connect to? #
# #
# 1) wekan (kanban) #
# 2) wiki #
# 3) nextcloud #
# 4) dockervm1 #
# 5) dockertestvm #
# #
##################################################################################################
input number:
"""
read -s server
echo $server
sleep 1
if [ $server = "1" ]; then
clear
echo "Setting up connection to wekan server..."
sleep 2
ssh 192.168.1.118
elif [ $server = "2" ]; then
clear
echo "Setting up connection to wiki server..."
sleep 2
ssh 192.168.1.117
elif [ $server = "3" ]; then
clear
echo "Setting up connection to nextcloud server..."
sleep 2
ssh 192.168.1.119
elif [ $server = "4" ]; then
clear
echo "Setting up connection to dockervm1 server..."
sleep 2
ssh 192.168.1.116
elif [ $server = "5" ]; then
clear
echo "Setting up connection to dockertestvm server..."
sleep 2
ssh 192.168.1.120
else
echo "Incorrect choice, programm will now terminate..."
sleep 3
clear
exit
fi
clear
exit
marc/linux/bash_snippets.1652799143.txt.gz · Last modified: (external edit)
