Bash script to send messages to Microsoft Teams

Copy and save into file. Make executable. Change web hook, Run!

The script is a modified Slack script from off the web.

# bash script to send messages to Microsoft Teams.
function usage {
echo "HELP ME!"
echo "description: send messages to Microsoft Teams channels"
echo "special notes: You'll need to change the teamsUrl variable to contain your webhook from Teams."
echo "usage: ${0} -b \"Message contents\""
echo " -m Message body"
echo " -h This help info"
exit 1
while getopts "m:h" opt; do
case ${opt} in
m) msgBody="$OPTARG"
h) usage
\?) echo "Invalid option -$OPTARG" >&2
# Add/Change the webhook to one you created in Teams
if [[ ! "${msgBody}" ]]; then
echo "You didn't specify a message!"
read -d '' payLoad << EOF
"text": "${msgBody}",
statusCode=$(curl \
--write-out %{http_code} \
--silent \
--output /dev/null \
-H 'Content-type: application/json' \
--data "${payLoad}" ${teamsUrl})
echo ${statusCode}

Bash script to monitor system service

This bash script runs and checks to see if a service like httpd, or mysql is running and alerts if it is not.

Script Usage httpd mariadb

Where httpd and mariadb are the services you want to monitor/check.

Setup Script

Create file and paste the following contents in.


timeHour=`date +%H` # date/time just shows the hour
quietHour="02"    # If it is this hour, then exit program, useful if services are expected to go down during a particular time for maintenance
if ( echo ${timeHour} | grep ${quietHour}); then
         echo "Is during quiet time.  Quiting."

 function ALERT {
 msg="~/ -b"  # Sends a message to Microsoft Teams channel.  Needs the script in the users home directory.
 ${msg} "$1"
 function SERVICECHECK {
 if (systemctl status ${serviceName} | grep Active | grep inactive); then
         ALERT "ERROR: $(hostname) - ${serviceName} - ${0} is inactive"
         echo "ERROR: ${serviceName} is inactive!"
         echo "Running!"
 for i in $@
 echo Checking ${i}

Note the script that is called is another script that is called that sends an alert to Microsoft Teams. Is not needed for this script to run, but allows for remote alerting.

Save file and make it executable

chmod +x

Add script to crontab (Optional)

crontab -e

The following runs the script every 5 minutes. Can change the 5 to 1 to run every minute. Change httpd and mariadb to the service you want to monitor.

*/5 * * * * /home/UserName/ httpd mariadb

BASH Script to add new SFTP user and setup permissions

This script adds a new SFTP user with only sftp access.  Refer to this post on setting up a SFTP server.

Download script


Make executable

chmod +x

Run with the new user you want to create.

./ sftpUsername

You may need to edit the script and modify the location parameters.

# Automatically setup and add SFTP user
# Script creates new user and setups permissions
if grep -q ${newUser} /etc/passwd ;then
echo ${newUser} Already exsists. Aborting!
exit 1
mkdir -p ${sftpDir}/${newUser}/files
useradd -g sftpusers -d ${sftpDir}/${newUser}/files -s /sbin/nologin ${newUser}
passwd ${newUser}
chown ${newUser}:sftpusers /sftp/CareMark/files

Add multiple IP bindings to Mikrotik Hotspot – Bash Script

Copy and paste the following into a file named and then “chmod +x”  Or download from this direct link.  Be sure to change the username and password


for i in `cat ${filelist}`
address=`echo $i | cut -d= -f1`
mac=`echo $i | cut -d= -f2`
sshpass -p ${password} ssh ${username}@${ip} "ip hotspot ip-binding add address=${address} mac-address=${mac} type=bypassed"

Now create a file named bypasslist.txt and put all the addresses you want bypassed.  You’ll need the Mac Address and the IP Address.

Example file

Usage of script is

./ MikrotikIP



Upload ssh key to multiple servers automatically

Here is a quick script I created to automate copying a ssh key to multiple remote servers.

Basic command – the command uses sshpass to upload the ssh key to a remote server, this allows you to execute the command and not have to enter in a password to authenticate.

sshpass -p password ssh-copy-id -o StrictHostKeyChecking=no admin@remotehost




for host in `cat ${remotehosts}`
sshpass -p${password} ssh-copy-id -o StrictHostKeyChecking=no ${username}@${host}
echo "Uploaded key to " ${host}

echo "Finished!"


Using the script

  1. Download here.
  2. Make it executable
    chmod +x
  3. Edit the script and change the username and password.
  4. Create a file that contains each host’s IP address or hostname.
  5. Run script (change hostlist.txt to your host list you created in step 3.)
    ./ hostlist.txt
  6. Wait for the script to finish.


chmod +x
sed -i s/admin/bob/g                      <-- Change username - you can just manually edit the file,
sed -i s/MyCoolPassword123/password/g     <-- Change password - it might be easier than using sed
echo "" >> host.txt                     <-- Add to the host list
echo "Bob" >> host.txt                               <-- Add hostname bob to host list
./ host.txt                                <-- Upload ssh key to all host's in the host file i.e. "bob" and ""

Control LED from Command Line – Raspberry Pi

Replace “4” with the GPIO pin your using.

echo "4" > /sys/class/gpio/export

Setup the direction.  If it was a button or switch we would change “out” to “in”.

echo "out" > /sys/class/gpio/gpio4/direction

Turn the LED on.

echo "1" > /sys/class/gpio/gpio4/value

Turn the LED off.

echo "0" > /sys/class/gpio/gpio4/value

1 = on and 0 = off.