Useful Bash Scripts

From MikroTik Wiki
Revision as of 03:54, 11 September 2011 by Mudasir (talk | contribs)
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
  • 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 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"
#####