Useful Bash Scripts

From MikroTik Wiki
Jump to: navigation, search

Overview

Here are some small and very useful bash scripts that I have written and I use them from WebMin to perform some small tasks. These Include

  • Adding a user in PPP -> Secrets
  • Enable / Disable a user in PPP -> Secrets
  • Show all users and Show online users.

Please make sure that you follow this article and setup ssh-keys to execute command from Linux to Mikrotik using SSH without password

Once you have everything setup properly. Then follow the instructions below to create new commands in Webmin and add scripts to that to make everything work properly. The scripts can be used from almost any Linux Distribution that supports Bash.

For now I will only add some scripts with basic functionality. Very soon I will add more scripts, and will also tell you all how to create scripts based on "Dealer / Re-seller" having their own custom support panel to perform tasks only for their users.

Adding a User

Make sure that you have webmin installed and running properly. Then follow these snapshots to create a new Custom Command that will perform the task of Adding New User to Mikrotik

Open Webmin in your favorite internet browser. Then follow below instructions.

Step 1:


Webmin-new-custom-command-1.jpg


Step 2:

Please check that all marked settings are properly filled as per the snapshot below.


Webmin-new-custom-command-2.jpg


Now open Linux Command Shell and create a file "/home/panel/useradd" (if you change this, also make sure that you change the command path and similarly the path to the package file in the command created in above snapshot step2).

Now copy the below script and paste it in the file you just created.


#!/bin/bash

#set -x

USER="mikrotikuser"
MTIP="192.168.100.101"
PORT="12345"
 
if [ "$UID" = "" ]; then
	echo -e "User ID can not be blank"
	exit 0

else
if [ "$PASS" = "" ]; then
	echo -e "Password can not be blank"
	exit 0

else
# Setting minimum password length to 6 characters
LEN_PASS=${#PASS}
if [ "$LEN_PASS" -le "6" ]; then
	echo -e "Password Must Be Greater Then 6 Characters"
	exit 0

else
if [ "$PACK" = "" ]; then
	echo -e "Please Select a package"
	exit 0

else
if [ "$INFO" = "" ]; then
        echo -e "Description can not be blank"
	exit 0

fi
fi
fi
fi
fi

ssh $USER@$MTIP -p $PORT / ppp secret add name=\"$UID\" password=\"$PASS\"  profile=$PACK comment=\"\# $INFO\" disabled=no 
echo -e "\n\n\t\t New User Addes with UID=$UID and PASS=$PASS \t\t"
#####

The main script is now at proper place. Now if you remember during the step 2 of creating custom command, we did create a field called Package and provided it a path to file which is "/home/panel/package". This is the list of packages same as present in the PPP -> Profiles. For making thins easy to understand, I am pasting my own Package file which is below.

root@crystalnetworks:~# cat /home/panel/package

128 128
256 256
512 512
768 768
nopay nopay

If everything went all and according to the instructions, you should now be able to create new users using this WebMin Custom Command


Disable / Enable a User

This section will cover the scripts that will Disable and / or Enable a selected user. This is a very simple and very small script but comes in very handy. Please follow Step 1 from above snapshot of how to create a custom command, I will only be showing Step2 because it contains some different settings then of "Adding a New User to Mikrotik".


Disable Mikrotik User:

Please follow below snapshot to create custom command for disabling mikrotik user.

Webmin-new-custom-command-2-disable.jpg


Notice that it only requires single filed (i.e. UserID of the user). Now create a new file "/home/panel/disable" and copy the below script in this file.


#!/bin/bash

#set -x

USER="mikrotikuser"
MTIP="192.168.100.101"
PORT="12345"


ssh $USER@$MTIP -p $PORT / ppp secret disable \"$UID\"
ssh $USER@$MTIP -p $PORT / ppp active remove [/ ppp active find name=\"$UID\"]

echo -e "\t\t User $UID has been disabled."
echo -e "\t\t Kicking User $UID if Online..."

This script simply disables the user, and kicks the user if found online.


Enable Mikrotik User:

Please follow above snapshot to create custom command for enable mikrotik user. Just replace all instances of Disable with Enable.

Now create a file "/home/panel/enable" and copy the below script in that file.


#!/bin/bash

#set -x

USER="mikrotikuser"
MTIP="192.168.100.101"
PORT="10022"

ssh $USER@$MTIP -p $PORT / ppp secret  enable \"$UID\"
 
echo -e "\t\t User $UID has been Enabled."

Script is almost the same as of Disabling the user, it just sets the disabled parameter of the user to NO in mikrotik.


List Users ( Online / All )

In this section, I will guide you in setting up commands that will show you online users and also show you total users of Mikrotik.

Show Online Users

We need to create a new custom command, Step 1 is same for creating custom command as in the "Adding New User" section. For step 2 please see below snapshpt

Webmin-new-custom-command-2-showonline.jpg


Notice that this does not require any parameter. Because it will only fetch the online users.

Now create a file "/home/panel/showonlineusers" , and copy the below script in this file.


#!/bin/bash

#set -x

USER="mikrotikuser"
MTIP="192.168.100.101"
PORT="12345"
FPATH="/home/panel"
DRL=$REMOTE_USER
FILE=$DRL

rm -fr $FPATH/$DRL.txt

ssh $USER@$MTIP -p $PORT / ppp active print file=$DRL
scp -P 10022 $USER@$MTIP:$DRL.txt $FPATH/$DRL.txt
cat $FPATH/$DRL.txt
echo
echo
TTL=`cat $FPATH/$DRL.txt | wc -l`
echo -e "\t\t\n Total Number of Active Users $TTL"

This script will ssh the mikrotik router and fill fetch all the active users in a file at /home/panel. Then will cat the file and will show you the output.


Show All Users

We need to create a new custom command, Step 1 is same for creating custom command as in the "Adding New User" section. For step 2 please see below snapshpt

Webmin-new-custom-command-2-showall.jpg


Notice that this does not require any parameter. Because it will only fetch the online users.

Now create a file "/home/panel/showallusers" , and copy the below script in this file.


#!/bin/bash

#set -x

USER="mikrotikuser"
MTIP="192.168.100.101"
PORT="12345"
FPATH="/home/panel"
DRL=$REMOTE_USER
FILE=$DRL

rm -fr $FPATH/$DRL.txt

ssh $USER@$MTIP -p $PORT / ppp secret print file=$DRL
scp -P 10022 $USER@$MTIP:$DRL.txt $FPATH/$DRL.txt
cat $FPATH/$DRL.txt
echo
echo
echo
TTL=`cat $FPATH/$DRL.txt | wc -l`
echo -e "\t\t\n Total Number of Users $TTL"

What this script is that this will ssh the mikrotik router and fill fetch all the users in a file at /home/panel. Then this will cat the file and will show you the output.



Article by Mudasir Mirza

All scripts are my own creation and I have not copied any material from anywhere.