Insert/Update Scripts
|
|
||
Scripting allows you to add information to new IP address and subnet records when they are added to the database or their data is updated. It also allows you to update or check data that is entered by users.
To access the database scripting functionality, select the Tools ribbon tab then select Insert/Update Scripts.

The Insert script will be executed when a record is added to the database. The Update will be executed when a record is edited.
Table Drop Down |
The table drop down allows you to select the script that you want to edit. Scripts can be created for the IP Address and Subnet tables. |
Enabled |
The script will only be executed if the enabled box is checked. |
Input Field Names |
The list displays all the input fields that are available to the script. Input fields are read only. For testing purposes, the value of a field can be set by double clicking the field or by selecting a field and pressing the F2 key. |
Output Field Names |
The output field name list displays the fields that can be set by the script. The list includes all the string based fields that can be edited by a user. |
Script |
Contains a script written in the Pascal programming language. |
Compile Button |
Attempts to compile the script. The compilation will check for syntax errors, but will not execute the script. |
Test Button |
Compiles then executes the script. Before you run the script you must make sure that you set the appropriate values of the input fields. |
|
Be aware that while scripting can be very handy for specific tasks it can slow down inserts and updates. However, even when running a fairly complex insert script in Easy-IPv6 it will most likely be faster than running the same operation without a script in a previous version of Easy-IP. |
Writing Scripts
Scripting in Pascal is reasonably simple, but it does require a basic understanding of how a programming language works.
To learn more, please refer to a book on Pascal.
If you would like to have a custom script written for you please contact Easy-IP support.
The Script sets the values of the output parameters, based on the values of the input parameters.
For example, the following script could be used with the Subnet table:
begin // Set the comment field if the CIDR Mask is greater than or // equal to 24 if StrToInt(cidr_mask) >= 24 then comment := format('This is a %s bit subnet mask', [CIDR_MASK]); end. |
Every script must start with a "begin" statement and end with an "end." statement. In between these statements you add your script.
So, what happens in the script above?
The second and third lines of the script starting with double slash // are comments.
Comments are placed in a script to make it more readable for humans and have no bearing on the outcome of the script.
First of all we convert the input variable "cidr_mask" from a string to an integer using the "StrToInt" command. We then check to see if the value is greater than or equal to 24. If it is we set the value of the output variable "comment" using the "format" command. The "format" command takes a string and replaces the occurrences of "%s" with the values from a list. For more information about the "format" command please refer to a Pascal reference as it is beyond the scope or this manual.
What effect will the script have when adding new subnets?
For subnets with a CIDR mask less than 24, the script will not have any effect. Subnets with a CIDR mask greater than or equal to 24 will have their "COMMENT" field set to "This is a x bit subnet mask" where x is the value of the CIDR mask.
Although this script does not do anything particularly useful, it demonstrates a very simple use of database scripting.
|
Variable names and functions in Pascal are not case sensitive. Therefore, you can use "strtoint" or "STRTOINT" or "StrToInt". All will work just fine. |
A real world example
A common use of database scripting is to set the value of a field based on its location within a subnet.
For example, your company only uses /24 subnet masks and needs the IP addresses to be allocated in the following fashion:
IP address range |
IP address type |
1-10 |
Printers |
11-250 |
Static IP Addresses |
251-254 |
Router interfaces |
To achieve the desired results you could use a script like this:
var IPPosition: Integer; CidrMask: Integer; begin // Convert the CIDR Mask to an integer CidrMask := StrToInt(CIDR_MASK);
// Only execute the next part of the script if the // subnet is IPv4 and has a CIDR Mask of 24 if (IP_VERSION = '4') and (CidrMask = 24) then begin // Find the position of the IP Address // in the subnet (positions values start at 1) IPPosition := PositionInSubnet(IP_ADDRESS, CidrMask);
case IPPosition of 1..10: Notes := 'Printers'; // First 10 251..254: Notes := 'Router interface'; // Last 4 else Notes := 'Static IP Address'; // The rest end; end; end. |
Once again, the script starts with a "begin" and finishes with an "end.". It then converts the CIDR mask input variable into an Integer and stores it in the local CIDRMask variable.
Next, it checks that the IP_VERSION input variable is set to "4" (signifying that this is an IPv4 subnet) and that the local CidrMask variable is set to 24. If this result is true the script between the next "begin" and "end" is executed.
The local "IPPosition" variable is now set to the position of the IP address within the subnet. This is achieved using the custom function "PositionInSubnet". The position of the first IP address in the subnet will be 1.
Using the Pascal "case" statement we now check the value of the "IPPosition" variable. If the value falls in the range bertween 1 and 10 (inclusive) we set the value our the "Notes" output variable to "Printers". If the value falls in the range between 251 and 254 we set the value of the output variable "Notes" to "Router interface". If neither of the previous conditions are met we set the "Notes" variable to "Static IP Address".
A more advanced real world example
Most subnets are not composed of one size of subnet so how do we go about writing a script that is flexible enough to cope with any subnet mask?
There are several ways to solve this problem. The simplest, but least practical, solution would be to simply repeat the previous script for every available CIDR mask. The downside would be that the script would be extremely long and prone to error. A cleaner, but slightly more complicated method follows.
var IPPosition: Integer; TotalAddresses: Integer; CidrMask: Integer; begin if IP_VERSION = '4' then begin CidrMask := StrToInt(CIDR_MASK); IPPosition := PositionInSubnet(IP_ADDRESS, CidrMask); TotalAddresses := TotalAddressesInSubnet(SUBNET, CidrMask);
if IPPosition <= 10 then Notes := 'Printers' else // First 10 IP Addresses if TotalAddresses - IPPosition <= 4 then Notes := 'Router interface' // Last 4 IP Addresses else Notes := 'Static IP Address'; // All the other addresses end; end. |
What's different about this script?
We still set the local "CidrMask" and "IPPosition" variables and check that the IP version is '4'.
Additionally, we use the custom "TotalAddressesInSubnet" function to retrieve the total number of IP Addresses in the subnet and store the value in the local "TotalAddresses" variable.
Next, we have a series of "if" statements that check the values of the "IPPosition" and "TotalAddresses" local variables. The value of the output variable "Notes" depends on the results of the "if" statements.
First, the "IPPosition" variable is check to see if it is less than or equal to 10. If it is, the "Notes" field is set to "Printers".
If the first "if" statement returns false, the second "if" statement is execute. This statement tests to see if "TotalAddresses" minus "IPPosition" is less than or equal to 4. In effect, this test returns true is the IP address is in the last 4 addresses of the subnet. If it is, the "Notes" variable is set to "Router interface".
If none of the "if" statements return true, the "Notes" variable is set to "Static IP Address".
For more information about Easy-IP Pascal scripting see the glossary.