MOTDfetch/install.sh
2023-02-07 16:10:46 +07:00

158 lines
6.2 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# installation script for MOTDfetch
# repository: https://git.tkapias.net/tkapias/MOTDfetch
# author: Tomasz Kapias
# email: tomasz@tkapias.net
#
# run with sudo or as root user, or directly from the repository:
# sudo wget -q https://git.tkapias.net/tkapias/MOTDfetch/raw/master/install.sh -O - | sudo bash
# locale
export LC_ALL="C.UTF-8"
export GREP_COLORS="ms=01;32:mc=01;32:sl=:cx=:fn=35:ln=31:bn=31:se=36"
N="\e[0m"
T="\e[1;35m"
S="\e[0;32m"
W="\e[0;33m"
E="\e[0;31m"
# check if executed by root or sudo
if (( ! `id -u` == 0 )); then
echo -e "${E}Please do run as root or with sudo${N}"
exit
fi
# check if the user uses Bash
if [[ ! $BASH_VERSION ]]; then
echo -e "\n\e[1;33mThis tool is written for Bash, its compatibility is not tested with other shells.\e[0m"
echo -e "You are currently using \e[1;31m $SHELL\e[0m, do you want to continue? [\e[1;32mY\e[0m/\e[1;31mn\e[0m]\n"
read -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
ERRORCOUNT=0
if [[ -f "/etc/ssh/sshd_config" ]]; then
echo -e "${T}# setup ssh server (/etc/ssh/sshd_config) to not print MOTD or LASTLOGIN${N}"
sed -ri 's/^(\#?)(PrintMotd|PrintLastLog)([[:space:]]+)(yes|no)/\2\3no/' /etc/ssh/sshd_config
resultsshno=$(/usr/bin/grep -E "PrintMotd\s+(no)?|\PrintLastLog\s+(no)?" /etc/ssh/sshd_config 2>/dev/null)
if [[ $resultsshno == "" ]]; then
echo -e "${E}FAIL - check manually \"PrintMotd no\" & \"PrintLastLog no\"${N}\n"
((ERRORCOUNT++))
else
echo -e "$resultsshno" | /usr/bin/grep --color=auto "no"
echo -e "${S}DONE${N}\n"
fi
echo -e "${T}# setup ssh server (/etc/ssh/sshd_config) to accept custom SSH_MOTD env variable${N}"
sed -ri '/SSH_MOTD/! s/^(\#?)(AcceptEnv)(.+)/\2\3 SSH_MOTD/' /etc/ssh/sshd_config
resultsshmotd=$(/usr/bin/grep -E "^AcceptEnv.+SSH_MOTD" /etc/ssh/sshd_config 2>/dev/null)
if [[ $resultsshmotd == "" ]]; then
echo -e "${E}FAIL - check manually \"AcceptEnv SSH_MOTD\"${N}\n"
((ERRORCOUNT++))
else
echo -e "$resultsshmotd" | /usr/bin/grep --color=auto "SSH_MOTD"
echo -e "${S}DONE${N}\n"
fi
else
echo -e "${E}/etc/ssh/sshd_config DOES NOT EXIST${N}\n"
((ERRORCOUNT++))
fi
echo -e "${T}# setup PAM's sshd (/etc/pam.d/sshd) to not print MOTD and MAIL${N}"
if [[ -f "/etc/pam.d/sshd" ]]; then
sed -ri 's/^(\#?)(session[[:space:]]+optional[[:space:]]+)(pam_motd.so|pam_mail.so)(.+)/#\2\3\4/' /etc/pam.d/sshd
resultpamcom=$(/usr/bin/grep -E "^\#.+pam_motd.so|^\#.+pam_mail.so" /etc/pam.d/sshd 2>/dev/null)
if [[ $resultpamcom == "" ]]; then
echo -e "${E}FAIL - comment manually lines with \"pam_motd.so\" & \"pam_mail.so\"${N}\n"
((ERRORCOUNT++))
else
echo -e "$resultpamcom" | /usr/bin/grep --color=auto -E "^\#"
echo -e "${S}DONE${N}\n"
fi
else
echo -e "${E}FAIL - /etc/pam.d/sshd DOES NOT EXIST${N}\n"
((ERRORCOUNT++))
fi
echo -e "${T}# restart ssh server if it's active${N}"
if [[ $(systemctl is-active ssh 2>/dev/null) == "active" ]]; then
systemctl restart ssh.service
echo -e "${S}DONE${N}\n"
else
echo -e "${E}SSHD NOT ACTIVE${N}\n"
fi
echo -e "${T}# install minimal dependencies${N}"
apt install -f figlet coreutils bc wget nano dnsutils openssl s-nail
echo
echo -e "${T}# install ANSI_Shadow figlet font${N}"
downloadfigletfont=$(wget -nv -O "/usr/share/figlet/ANSI Shadow.flf" https://raw.githubusercontent.com/xero/figlet-fonts/master/ANSI%20Shadow.flf)
if $downloadfigletfont ; then
echo -e "${S}DONE${N}\n"
else
echo -e "${E}FAIL${N}\n"
((ERRORCOUNT++))
fi
echo -e "${T}# check directories & backup old & obsolete files${N}"
if [[ ! -d "/etc/update-motd.d/backup" ]]; then
mkdir -vp /etc/update-motd.d/backup
fi
if [[ ! -d "/etc/motdfetch" ]]; then
mkdir -vp /etc/motdfetch
fi
chmod -v 755 /etc/update-motd.d/ /etc/update-motd.d/backup/ 2>/dev/null
echo -e "${S}CHMOD DONE${N}\n"
if [[ -d "/etc/update-motd.d/motdfetch.d" ]]; then
cp -p -Rv /etc/update-motd.d/*motdfetch* /etc/update-motd.d/backup/ 2>/dev/null
echo -e "${S}MOTDfetch BACKUP DONE${N}"
fi
if [[ -d "/etc/motd" ]]; then
mkdir -vp /etc/update-motd.d/backup/etc
mv -vf /etc/motd /etc/update-motd.d/backup/etc/motd 2>/dev/null
echo -e "${S}Static MOTD BACKUP DONE${N}"
fi
if [[ -d "/etc/update-motd.d/10-uname" ]]; then
mv -vf /etc/update-motd.d/10-uname /etc/update-motd.d/backup/10-uname 2>/dev/null
echo -e "${S}Debian uname MOTD BACKUP DONE${N}"
fi
echo
echo -e "${T}# install/update MOTDfetch${N}"
downloadmotd=$(wget -c -nv https://git.tkapias.net/tkapias/MOTDfetch/archive/master.tar.gz -O - \
| tar -xz --strip-components=1 -C /etc/update-motd.d/ MOTDfetch-master/motdfetch.d/ MOTDfetch-master/00-motdfetch MOTDfetch-master/motdfetch.sample.conf 2>/dev/null)
if $downloadmotd ; then
mv -vf /etc/update-motd.d/motdfetch.sample.conf /etc/motdfetch/motdfetch.sample.conf
if [[ -f "/etc/motdfetch/motdfetch.conf" ]]; then
echo -e "${S}SUCCESSFUL UPDATE${N}\n"
else
cp -p -Rv /etc/motdfetch/motdfetch.sample.conf /etc/motdfetch/motdfetch.conf
echo -e "${S}SUCCESSFUL INSTALLATION${N}\n"
fi
if (( $ERRORCOUNT == 1 )); then
echo -e "${W}But CAUTION, $ERRORCOUNT configuration steps have failed.${N} Please check if you need to adjust manually.\n"
elif (( $ERRORCOUNT > 1 )); then
echo -e "${W}But CAUTION, $ERRORCOUNT configuration step has failed.${N} Please check if you need to adjust manually.\n"
fi
echo -e " - ${S}edit the config file${N}:
# sudo nano /etc/motdfetch/motdfetch.conf
${S}or create a new user config${N}:
# userdir=~/.config/motdfetch \\
&& mkdir \$userdir \\
&& cp /etc/motdfetch/motdfetch.sample.conf \$userdir/motdfetch.conf \\
&& nano \$userdir/motdfetch.conf\n"
echo -e " - ${S}add an alias \"motd\" command to your .bashrc${N}:\n alias motd='/etc/update-motd.d/00-motdfetch'\n"
echo -e " - ${S}how to use MOTDfetch as an alternative MOTD for SSH sessions${N}:
+ ${S}on the ssh server, add the new \"motd\" alias at the end of the ssh user .bashrc${N}:
if [[ -n \$SSH_CONNECTION ]]; then\n motd\n fi
+ ${S}on the ssh client, add an alias \"sshmotd\" to your .bashrc${N}:\n alias sshmotd='ssh -o SetEnv=SSH_MOTD=1'\n"
else
echo -e "${E}INSTALLATION FAILED - please retry${N}\n"
fi