Python IMAP search and SMTP with Gmail

Recently I had a need to search through my gmail and fwd some emails on a daily basis. I checked out Gmails rest interface but it was a bit of an overkill for what I was doing. So I settled on just plain IMAP and SMTP. The main idea was search through my inbox using several search parameters, get all the emails that match the pattern and fwd them to a list of emails. So I wrote a script to do this and had cron call the script at the end of the day. I put up the source in github for anyone interested.

KVM usage tips

I have been playing around with KVM lately, just to see how it stacks up against VirtualBox and am loving it. I have found the following commands interesting while working with VMs.

List all VMs

virsh list --all

Start stop a VM

virsh start/stop vmName

Clone VM

virt-clone --original vmName --name newVmName --file /some/path/newVmName.qcow2

Clone VM and assign Random Mac address to new image

virt-clone --original vmName --name newVmName --file /some/path/newVmName.qcow2 --mac=RANDOM

Copy over the xml for the new clone which you can use to define your clone if moving the image to another host

cp /etc/libvirt/qemu/newVmName.xml /some/path/newVmName.xml

Shrink/Sparcify VM image

virt-sparsify --format qcow2 --compress vmName.bkp.qcow2 vmName.qcow2 --tmp /some/path

This command assumes you renamed the original VM image file to “vmName.bkp.qcow2” incase the new image has problems you can revert to original image. You can skip the –tmp argument if you have more than double the size of the VM image available in your /tmp directory. You will need to make sure you have package “libguestfs-tools” installed which supplies the sparsify command.

Delete VM

virsh undefine vmName

This removes the XMl configuration for qemu you might have to delete the VM image yourself.

List storage pools

virsh pool-list

Refresh pool data

virsh pool-refresh poolName

You will need to run this command if you delete a VM and you need to reuse the VM name you had used, so make sure you refresh the pool where your deleted image was located.

Move VM to another system Offline
Step 1 from original host shutdown your VM, copy xml and image to backup location.

virsh dumpxml vmName > /bkpLocation/vmName.xml
cp /locationofVMs/vmname.qcow2 /bkpLocation/vmName.qcow2

Bye default if you didn’t change image storage location it will be “/var/lib/libvirt/images”. I prefer to store them in a separate partition from the OS.

Step 2 on new host get VM xml and image and enjoy.

virsh define /bkpLocation/vmName.xml
Domain vmName defined from /bkpLocation/vmName.xml
virsh start vmName

This all works if you are moving from host1 and host2 with similar cpus, for my purpose host2 had a different cpu type so I had to first create a new vm in host2 and compare the xml with my backed up copy then changed a few lines specific to CPU on host2.

Disable sleep keyboard button Linux

Recently I needed to disable the sleep button on my Fedora desktop. At first I was going to use


but the main drawback with this method is that changes made are cleared on reboot so I would need to create a script to be run at boot to maintain my settings.
A little more googling and I found a better way of performing the change using


. By creating a file “/etc/polkit-1/localauthority/50-local.d/somefile.pkla” having extension .pkla with the following entries:

[Disable suspend]

[Disable hibernate]

This has been tested successfully on Fedora 20, my centos box doesn’t need this as sleep is blocked by default.

FYI: Some xmodmap commands i found useful when researching the issue.
To list all keycode mappings

 xmodmap -pke

To disable keyboard mapping for sleep”X86Sleep” button on my system which is mapped to “keycode 150”.

xmodmap -e 'keycode  150 = NoSymbol'

After second reboot KDE ignored the localpolicy file and I had to reconfigure the sleep button within KDE. I can’t explain why it worked after first reboot, but for now am just going with redefining the button in KDE.

Eclipse crashes after update to CentOS 6.6

I updated my centOS to the lates update 6.6 and eclipse started crashing every time I tried to use the editor. At first I thought it was a SELINUX issue but this was happening even after turning off SELINUX. Turns out it’s a bug with kepler and how it checks for installed Cairo version. Launching eclipse from console will give you the error below.

eclipse: cairo-misc.c:380: _cairo_operator_bounded_by_source: Assertion `NOT_REACHED’ failed.

This issue is fixed in Luna if you want to upgrade or you can disable Cairo in eclipse.ini with the command below to keep using your current eclipse version.


Thanks to @Riley for the workaround.

Love OOP Python

Python makes it easy to automate manual tasks. Over time I have accumulated some scripts each for a different task but not sharing any code between them but having some similar logic, basically a few of them were having duplicate logic. So this week as I was adding another script to download and add the latest plex media server to my local repo, I came over an article about OOP python on Stackoverflow.
After creating a class for all the common logic in my scripts I was able to reduce the size of most of them by over half, I make my living writing OOP code for java but OOP for python was new to me. I wish there was OOP for bash 🙂 I have a bunch of bash scripts that need culling.

Cloud heaven = Plex + Android + CloudSync + Linux server + Own Cloud

I recently started hosting my own cloud service and it has been an awesome experience. After having my smart phone for sometime i started running low on storage, while I had backed up some pictures to a local share this is not really convenient. We take pictures on our phones so that we can have them close by so having to be at home to view my photos and videos was not very interesting. With a few apps and a Linux server I was able to put together a cloud service for the whole family that we are all loving very much.

The setup
Own Cloud installed on the Linux server. I tried a couple own cloud plugins for multimedia and none worked out for me. Using own cloud’s android sync app I ran into issues as it would lock up my phone when syncing. To fix this two issues I decided to have Plex handle the multimedia stuff and CloudSync handle the syncing process. Since I already had Plex serving up my video content adding photos was a breeze, plex also displays all your photos as thumbnails on your phone which you can click to view while the own cloud app displays a file name list which you have to click on a file to view it. I installed cloudSync on all the phones that needed backing up, this is like the swiss knife of sync apps it supports several cloud services plus regular services such as sftp,ftp and webdav just to name a few. You can back up your data to different locations by setting up filters for each data type which is awesome as I don’t want pictures and videos in one folder.
With this setup am able to have pictures and videos synced instantly and rsynced between different storage clients for redundancy. Within minutes of any of any of the phones taking a picture or a video any of the other phones can view that content so no need to send each other whatever you just captured.

While regular cloud providers offer you a few gigs for free and you have to pay once you get over 20gigs by hosting your own cloud you have terabytes of data readily accessible under your own control. You can use this cloud providers for just backing up your sensitive documents that you need an offsite backup in case of fire etc but remember to encrypt it first.

So now with everything backed up I have cleared my phones storage barely using a quarter of it and I still have access to all the pictures and clips that have been taken by any phone in the house for the last few years.

PHP Startup: Unable to load dynamic library ‘/usr/lib/php/modules/’

If you are using rpmforge with Centos 6.x you might run into this issue I found 2 packages with this issue mbcrypt and php-tidy. Both had the wrong setting in their .ini files. Just edit the files and replace “module” with the right module name.




The .ini files are located under /etc/php.d/

Dual boot with Fedora 17+

After years of using Fedora on my desktop I wanted to try something else I decided to go with Mageia. Install went ok until I got to setting up Grub2 for dual boot. While in the past I have multi booted several OS’s with classic grub I have never done that with Grub2. This thread pointed me in the right direction while this site was a great resource on grub2 it’s more focused on Ubuntu’s version of grub2 and there are a few differences with the fedora version, I found that the command “update-grub” does not work in Fedora other than that I did not notice much difference. I also did not go through all the steps in the tutorial just what I needed to get my setup working.
So how is my setup? I have several partitions on my HD and one of this partitions has fedora which boots on startup. I installed Mageia on an unused partition and made sure that it installed it’s bootloader in it’s partition. This is very important if you are trying to multiboot properly as explained in the grub2 tutorial. Only one application can have it’s bootloader installed in the MBR check the grub2 tutorial if you want further explanation about this otherwise each new OS you install will keep taking over the boot process.
At this point even though Mageia has been installed on reboot it does not show up on Fedora’s grub menu. To my knowledge I had 2 options add a script as described in the grub2 tutorial or just add Mageia to Fedora’s grub menu. I had already prepared my script when i fiugred I would not need it since my distributions would have totally different names, this would not be the case if I was trying to boot similar OS’s like kubuntu and Ubuntu. To add Mageia to Fedora grub menu I run the command below, it scans your HD for any other OS’s and automatically adds them. That’s it no editing scripts or grub files.

grub2-mkconfig -o /boot/grub2/grub.cfg

FreeNX/NX No suitable cache file found.

I ran into this error after my connection got disconnected due to a power outage during a storm. Googling around I could not find a solution to fix my situation. On a hunch I connected a keyboard and monitor to the server logged on and off in the GUI then tried the NX connection and it worked, so in my situation it seems some Xorg files got corrupted causing NX to fail starting an X session.
If this ever happens again I would like to try fixing it from an ssh connection by clearing X related files and locks for the user, as some fixes online involved adding a new user to NX and using the new account. So it looks as if the problem might be caused by unreleased locks or session files.

Update enom dynamic IP address

I have been using LqConsulting for about five years now to register my domain, if you are a linux user and you have used Linuxquestions to get some issues resolved consider using this registrar as it it is owned by the same person.

Back to the point this registrar currently uses enom to register your domains, which offers DyDns services and you can get a list from both places on the clients you can use to update your Dynamic IP. The problem comes with configuration, I like most people who rum linux to host personal websites have never setup one of this Dydns clients before and the help I got from my registrar was not very helpful. So for sometime as you can see from this post I was using a script to notify me when my ip changed and i would log in to my registrar and update my IP manually, this is not a very ideal way of doing it. Am not trying to bash my registrar here they provide me with a great service, they are a small shop and the main part is they do a lot for linux users, this being a personal site I was not loosing any sleep or money due to my site going down as a result of my IP changing.

But life is about convenience so I finally crawled the net and was able to find configuration info for ddclient with enom services, straight away I setup ddclient in my Centos box, and life was great anytime my ip changed I would get an email notification and my registrar would be updated automatically. The problem with life is once you have gone forward it’s hard to go back so when I updated my Centos box to the next version it came with a new version of ddclient which would not take the patch to make it work with enom but it also did not work with enom even though it was supposed to support it. So I could go back to manually updating my IP or get another client that would work with enom, I ended up putting together this python script to update my IP changes and to also send me an email whenever my dynamic IP changed. This is my first ever python code, I have worked with other languages and it was a real pleasure to see how simple python makes some tasks that would take a lot more coding in other languages I have used. Most of the code is made up of different pieces i found around the net so I can’t claim to have done it all by myself. So in the spirit of others before me am putting it out there to help anyone else who might need it feel free to use as you please and change it to your liking. If it works for you or not let me know but it is not a requirement.