Category: Tips & Tricks

Dovecot Email Migration

So I’ve just migrated my mail server recently. I was initially using iRedMail but needed to move it to another server. The problem though is that the destination server already had MySQL and Nginx as a web server. I just wasn’t comfortable running a script that has a chance to break what I’ve currently setup. I also didn’t want to take the route of creating my own email server from cratch. I’ve already did it in the past and it was okay but it was a hassle to maintain. So like anyone else I’ve jumped to Google to search for any free open source email server. Lucky for me I was able to find Mailcow relatively quickly. It’s open source and uses popular open source software for the email server components (MTA, Database, WebServer, etc.). The great thing about it though is that its dockerized which would not interfere with what is currently installed in the destination server. Continue reading

Create A Python 3 Environment With virtualenv

Update: If you’re already using Python >=3.3 then there’s is already a package called venv which you can use to create you’re virtual environment with just:

python3 -m venv myennv

virtualenv is very useful in developing Python because you can create your project’s own environment without polluting the or messing with the global or your other projects Python environment.

First make sure that you have already installed virtualenv and pip for Python 3.

$ sudo apt-get install python3-pip 
$ sudo pip3 install virtualenv

Continue reading

MySQL Procedure To Upper Case Table Names

Ever had an experience where you needed to rename a handful of table name into upper case. Below is a procedure to rename all tables into upper case in the current MySQL database/schema.

DELIMITER $$
 
DROP PROCEDURE IF EXISTS `PROC_TABLE_NAME_TO_UPPER_CASE`$$
 
CREATE PROCEDURE `PROC_TABLE_NAME_TO_UPPER_CASE`()
BEGIN
 DECLARE tbname TEXT DEFAULT ;
 DECLARE no_res INT DEFAULT 0;
 DECLARE cur CURSOR FOR
 SELECT t.table_name
 FROM information_schema.tables t
 WHERE t.table_schema = DATABASE() AND t.table_type=BASE TABLE;
 DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_res = 1;
 
 OPEN cur;
 
 rnloop: LOOP
 FETCH cur INTO tbname;
 
 IF no_res = 1 THEN
 LEAVE rnloop;
 END IF;
 
 SET @s = CONCAT(RENAME TABLE `, tbname , ` TO ` , UPPER(tbname), ` );
 PREPARE stmt FROM @s;
 EXECUTE stmt;
 END LOOP;
 
 CLOSE cur;
 
END$$
 
DELIMITER ;

Just run the script and call it using

CALL PROC_TABLE_NAME_TO_UPPER_CASE();

then it will rename the tables. Be reminded though that you need to specify the database to be used before connecting to MySQL or by using the use command.

Read String Console Input Including Spaces Safely In C

Reading a string input with spaces in between words can be tricky. There are two functions you can use here its scanf() and fgets(). Both can be used to read in strings from console safely. By safe I mean not overflowing your buffer.

Using fgets()

This is the common way of retrieving input including spaces safely.

#include 
 
int main( int argc, char* argv[] ) {
 char buffer[1024];
 
 printf( "Please enter a string: " );
 fgets( buffer, 1024, stdin );
 printf( "You entered: %s", buffer );
 return 0;
}

Using scanf()

This is the one I prefer to use over fgets(). Continue reading

CodeIgniter: Remove index.php From URL With The Project Mounted On A URL

If your CodeIgniter project is mounted on URL that looks like

http://www.example.com/codeigniter-project/

and you want to remove the index.php so that your URL to a some page example a blog page will look like this

http://www.example.com/codeigniter-project/blog/view/1

instead of

http://www.example.com/codeigniter-project/index.php/blog/view/1

Then first you need to make sure that mod_rewrite is enabled in your apache by typing this in the terminal:

sudo a2enmod rewrite
sudo service apache2 restart

Then in your CodeIgniter installation folder add an .htaccess containing this:

 RewriteEngine on
 RewriteCond $1 !^(index\.php|resources|robots\.txt)
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php/$1 [L,QSA]

The last thing you need to do is make sure that in your config.php index_page is blank:

 $config[index_page] = ;

Unblock IP From fail2ban SSH

Okay, Ive done this sometimes where I typed in my password hastily and ended entering it incorrectly mutiple times which results to my IP being ban by fail2ban. To unban your IP you need to either wait for the ban time you set to expire or if you cant you will need to access your server using a different IP address.

Once you have gained access to your server type this in:

iptables -L -n

Search for the line where you IP is. It should be below the line of:

Chain fail2ban-ssh (1 references)

If its there then your IP is still banned from using SSH. To remove it type:

iptables -D fail2ban-SSH -s [your-ip] -j DROP

Change “[your-ip]” to your actual IP. Now check again the iptables and the entry for your IP is should now be gone and this means that you are now unban from fail2ban-ssh.

Auto Enter Password For sudo Command

Normally using sudo will request the user a password after executing it but sometimes we might need to run a command with sudo from a script(bash script) or from a program. To auto enter a password for a script that will execute a sudo command you will need to do it like this in your script:

echo [password] | sudo -S echo "Sudo with auto enter of password"

Just replace the “[password]” with your actual password. Notice that we have “-S” as the option we pass for the sudo command. This option enables sudo to read the password from stdin where we have echoed our password.

Just an opinion but if you find yourself needing to auto enter a password in sudo command then maybe you should consider running it as a root user instead.

Access Django Development Server Remotely

By default Django development server will only be accessible through the local machine(127.0.0.1). If you want to access the server remotely then you need to run the the development server with your IP as an argument:

python manage.py runserver [your-ip-here]:8000

Change the “[your-ip-here]” to your actual IP. If you IP address is 174.143.2.123 then you will need to run the server using this:

python manage.py runserver 174.143.2.123:8000

If you have multiple network configured or you want the server to be automatically available on what your IP now currently is. Then enter 0.0.0.0 as your IP, like this:

python manage.py runserver 0.0.0.0:8000

This will bind the development server of Django on all available network/IP address. Then you will the see same message when you use just use python manage.py runserver  upon successfully creating the server but instead of http://127.0.0.1:8000/ you will see a http://0.0.0.0:8000/, then you will now be able to access your server remotely.

Validating models...
 
0 errors found
February 01, 2014 - 10:13:18
Django version 1.5.1, using settings mysite.settings
Development server is running at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

Warning: This server is only meant for development use so you must not use this as a production solution. This is also discouraged by Django team.

Android: Using An Adapter For Dynamic ListView

This a continuation of my Android: Using An Adapter For ListView post. So please read that first you if haven’t done yet. The projects files for this tutorial is also there.

Adding The Dynamic Functionality

To add the dynamic functionality for the ListView we will need to add 2 functions for adding and removing items in our class that implemented the BaseAdapter. For the function of adding strings:

public void addString( String val ) {
 m_data.add(val);
 notifyDataSetChanged();
}

Aside from just normally adding the string to our data container called m_data. We are also calling the notifyDataSetChanged(), this function will notify the View where are our Adapter is connected. For this instance we will notify the ListView that the underlying data has changed and it must refresh its display/content.

Removing items is also the same, we will remove it normally from the List and notify the ListView of the changes done on the data.

public boolean removeString( int location ) {
 if ( location >= m_data.size() ) {
 return false;
 }
 m_data.remove(location);
 notifyDataSetChanged();
 return true;
}

We also added the location of the item that we need to remove. The important part of these 2 function is the line where we call the notifyDataSetChanged() since without calling it your view will not refresh and show the changes you’ve done.

Android: Using An Adapter For ListView

Creating A Custom Adapter

To use an Adapter with a ListView you must first extends an Adapter.

 class MyAdapter extends BaseAdapter {

Create a List member for the data container.

List<String> m_data = ArrayList<String>();

m_data will contain all string items that you will want to be displayed in the ListView. So in our constructor we will populate m_data with the strings we want to be shown.

 public MyAdapter() {
 m_data.add("item 1");
 m_data.add("item 2");
 m_data.add("item 3");
 }

Continue reading

© 2019 James Baltar

Theme by Anders NorenUp ↑