Linux:Scripts

From Cheatsheet
Revision as of 09:17, 23 August 2023 by Patrick (talk | contribs) (Patrick moved page Linux:scripting to Linux:Scripting)
Jump to navigationJump to search


Common

# Execute a command within all objects in a folder.
for i in *; do du -h "$i" ; done
# man test
[]
Brackets are for checking whether something is true or not.
if [ -d "$D" ]
then

else

SSH

centos-create-user.sh

#!/bin/bash
# Execute as root
# Usage: ./centos-create-user.sh USER

USER="$1"

echo "Creating user ${USER}"
useradd -m ${USER} -G wheel

echo "Creating authorized keys file and settings rights for ${USER} user"
mkdir -p /home/${USER}/.ssh

cat << 'EOF' >> /home/${USER}/.ssh/authorized_keys
# PUT ANY KEYS IN HERE
# KEY-1
# KEY-2
# KEY-3
EOF

chown -R ${USER}:${USER} /home/${USER}/.ssh
chmod 700 /home/${USER}/.ssh
chmod 600 /home/${USER}/.ssh/authorized_keys

# EOF

ubuntu-create-user.sh

#!/bin/bash
# Execute as root
# Usage: ./ubuntu-create-user.sh USER

USER="$1"

echo "Creating user ${USER}"
adduser ${USER} --disabled-password

echo "Adding ${USER} to the sudo group"
usermod -aG sudo ${USER}

echo "Creating authorized keys file and settings rights for ${USER} user"
mkdir -p /home/${USER}/.ssh

cat << 'EOF' >> /home/${USER}/.ssh/authorized_keys
# PUT ANY KEYS IN HERE
# KEY-1
# KEY-2
# KEY-3
EOF

chown -R ${USER}:${USER} /home/${USER}/.ssh
chmod 700 /home/${USER}/.ssh
chmod 600 /home/${USER}/.ssh/authorized_keys

# EOF

Mail

Automatic sendmail

#!/bin/bash
#requires: date,sendmail
function fappend {
    echo "$2">>$1;
}
YYYYMMDD=`date +%Y%m%d`

# CHANGE THESE
TOEMAIL="ADMINISTRATOR@MYDOMAIN.COM";
FREMAIL="FROMNOREPLY@MYDOMAIN.com";
SUBJECT="E-mail Subject";
MSGBODY=$(Command);

# DON'T CHANGE ANYTHING BELOW
TMP=`mktemp`

rm -rf $TMP;
fappend $TMP "From: $FREMAIL";
fappend $TMP "To: $TOEMAIL";
fappend $TMP "Reply-To: $FREMAIL";
fappend $TMP "Subject: $SUBJECT";
fappend $TMP "";
fappend $TMP "$MSGBODY";
fappend $TMP "";
fappend $TMP "";
cat $TMP|/usr/sbin/sendmail -t;
rm $TMP;