Author Archives: CooLMinE

How to create a swap file on Ubuntu

Swap is particularly useful in situations where physical memory is limited, especially when dealing with low memory VPS/systems. This does not mean that swap is useless in all the other situations. Any system with close to 100% RAM utilization without swap enabled will cause application crashes or even worse, a system crash.

Moreover, some applications, in rare cases, require swap to be enabled regardless of memory capacity/usage, otherwise they simply crash.

To enable swap on Ubuntu follow the steps below (requires administration rights):

Create an empty file called “swapfile” using fallocate:

Feel free to change the “2G” value if you believe you require more or less swap space.

Change the permissions of the swap file to 600 in order to make it accessible only for the root user:

Mark the “swapfile” as swap space:

Enable the swap file:

Note: It is extremely important to note that any changes we have made so far are temporary and will be removed when we reboot the machine.

In order to make our changes permanent we will need to update the fstab file located at /etc/fstab. You can do so by editing the file and adding the following line inside:

Or simply by using this command do insert the line for you in the file:

We put together a one line command using the instructions above. It comes in handy when deploying new machines and we want to speed up the process. Feel free to use and modify it to suit your needs.

Wake-on-Lan (WoL) in C#

Wake on LAN is a computer networking standard that allows devices to be powered on when they receive a specific network message, if your device supports this feature.

In this example we will demonstrate how to craft this “magic” packet in C#. The concept of the packet is fairly simple. The packet must contain anywhere in the payload six (6) 255 bytes in a row (FF in hexadecimal) followed by the MAC address of the device repeated sixteen times.

As an example, if your device MAC address is 01-00-00-00-00-02 the payload will look like this:

You can find the implementation of WoL in C# below:

And the result…
Wireshark WoL Result

Feel free to leave your questions or feedback in the comment section below.

Automatically check for MikroTik software updates

If you own multiple MikroTik devices (or even one) you already know how annoying and time consuming it is to keep checking if there are new updates available for your devices all the time.

The purpose of this tutorial is to help you setup your devices in order to receive an email notification if a new software update is available to download for your device.

Step 1

First of all, we need to setup the SMTP settings of your MikroTik device to allow it to send emails when required.

Using WinBox, or the web interface, navigate to Tools --> Email and change your settings as shown in the image below. Please note that the image contains sample data and requires from you to change the settings with a working SMTP as well the correct credentials, if required.

mikrotik smtp settings

If you prefer to use the CLI over the web interface or WinBox, you can use the command below:

Make sure to send a test email to verify that everything is working properly.

Step 2

Now that our MikroTik device is able to send emails it is time to create our script which will be responsible for checking for new updates and sending us an email if one is available.

Navigate to System --> Scripts, click Add, change the title to “Check for software updates” or to something else if you prefer (don’t forget the name, you will need it in step 3) and finally, paste the following script in the source area:

CLI Version (pay attention to the highlighted line)

Step 3

It is time for the last step. In order for everything to work properly, we need to trigger our script every now and then in order for the update check to take place. To accomplish this, we will use the scheduler feature which is located under System --> Scheduler

Simply add a new scheduled task, give it a name and specify how often it should run in the interval section (I run mine with a 2d 00:00:00 interval).

In the “On Event” section paste the line below.

Important: Make sure the name of the script is exactly the same as the one you have created in step 2!

CLI Version

And you are done! If you have successfully followed this example your MikroTik device will check every 2 days for new updates and will let you know by email if one is available.

This method has been tested in the last few versions of RouterOS (currently 6.38). For older versions you might need to alter the script functions a bit to get everything working correctly.

If you have any questions don’t hesitate to ask them in the comment section below.

Update: As of v6.43.7 the release channels have been renamed. If you want to use the above script on a MikroTik that is running an older RouterOS version you will need to use the old software update channel name.

For RouterOS equal or newer than v6.43.7

For RouterOS older than v6.43.7

* release channels renamed – “bugfix” to “long-term”, “current” to “stable” and “release candidate” to “testing”

Using PowerShell to backup your MySQL databases

A few months ago we had to find a way to backup our MySQL databases, both remotely and locally. Instead of trying to find a software that did something similar I decided to write a few lines of code that solved our problem. Why spend time and money evaluating software when can create your own solution to the problem with little to no cost, right ?

The snippet below is fairly simple. The $databases variable holds an array of all the databases you want to dump. You will need to specify the database name, the path you wish to save the backup and the backup filename. I took the liberty of commenting the code in order to make it a bit more clear.

Please keep in mind that this solution uses mysqldump. This executable is usually located under C:\Program Files\MySQL\MySQL Server 5.6\bin, assuming you have MySQL server installed. In addition to mysqldump, since all our Windows servers we have WinRAR installed, this snippet uses WinRAR in order to compress the database files. You can always modify the code either skip the compression or to use .NET’s ZipFile class or 7zip.

This solution still needs some tweaking. For example, creating a temporary file with the passwords and passing it as an argument to mysqldump to avoid the “Specifying a password on the command line should be considered insecure” message, as well as some code cleanup and error catching. Until then, you are free to modify this example to better suit your needs.

If you have any questions feel free to post them in the comment section below.

Create a MySQL table dynamically in C#

Sometimes you might need to create database tables dynamically, based on new users, products etc. The snippet below allows you to do exactly that, create a new table in your database dynamically, straight from your code.

This example is heavily based on How to connect to a MySQL database and retrieve data in C# as the same concept of connecting to the database is required.

The first thing you need to do, as mentioned in the article linked above, is that you will need to add a reference to MySQL.Data in your project. After you have done that, you can modify the snippet below to create the table with the columns you want based on your requirements.

If you are unsure as to which database engine to use, please take a look at as every database engine has different type of limitations, so it is better to choose the one that better suits your needs.