Difference between revisions of "Useful Bash Scripts"

From MikroTik Wiki
Jump to: navigation, search
Line 35: Line 35:
  
  
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 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.
 
Now copy the below script and paste it in the file you just created.
Line 41: Line 41:
  
 
  #!/bin/bash
 
  #!/bin/bash
 
+
 
  #set -x
 
  #set -x
 
   
 
   
Line 83: Line 83:
 
  echo -e "\n\n\t\t New User Addes with UID=$UID and PASS=$PASS \t\t"
 
  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

Revision as of 03:58, 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