Difference between revisions of "Useful Bash Scripts"

From MikroTik Wiki
Jump to: navigation, search
Line 104: Line 104:
  
 
[[File:Webmin-new-custom-command-2-disable.jpg]]
 
[[File: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.

Revision as of 04:08, 11 September 2011

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
  • Deleting a user in PPP -> Secrets
  • Editing user credentials (such as ID, Password, Profile, MAC-Address, etc...) in PPP -> Secrets
  • Disable a user in PPP -> Secrets

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.

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". 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.


  1. !/bin/bash
  1. 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.