how to check if a perl module is allready installed

In this sort tutorial, I will present a simple and easy way to check if a module in perl is installed in your system.

All the work is done by using a simple command:

perl -MModule::Name -e "print \"Module installed.\\n\";"

so for example if I want to check if the Module Net::LDAP::Control::Persistent is installed I have to run this command

perl -MNet::LDAP::Control::PersistentSearch -e "print \"Module installed.\\n\";"

In case that this module is Installed the console will print

Module installed

In case that this module is not installed the console will print something like this:

Can't locate Net/LDAP/Control/PersistentSearch.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .).
BEGIN failed--compilation aborted.

Centos – set selinux permissive without reboot

You can view the current mode SELinux operates in, by executing the sestatus comman:

# sestatus

You can change the mode by editing the file /etc/selinux/config and change the SELINUX=enforcing line to SELINUX=permissive

# vi /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=permissive
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted

By executing once again the sestatus command you ‘ll get the output:

# sestatus

SELinux status: enabled
SELinuxfs mount: /selinux
Current mode: enforcing
Mode from config file: permissive
Policy version: 24
Policy from config file: targeted

My way to change the current mode is to reboot the machine. Today I discovered that you can change the current mode by using the setenforce command.

man pages gives this as usage:

setenforce [ Enforcing | Permissive | 1 | 0 ]

so you can execute

# setenforce 0

to change SELinux currend mode to permissive.

No more reboots and downtime because of selinux!!!

How to copy a file or a directory to multiple directories without a script in bash

Problem description

First of all I want to explictly explain my problem. Some times I have to copy a file or a directory to multiple locations in Unix Systems. I am bored to do a lot of copies and I am also bored to write a looped script for such a trivial thing. If your problem has a pattern (most common problems have a pattern), then all you need is to think as rationally as possible. So this can be assumed to be a How to copy a file or a directory to multiple directories without a script in bash tutorial.

Creating a list of directories where we want to make the copy

I will give an example to make it more clear. Let’s say we have a /var/www/sites*/public_html/ directory tree, where sites* could be all possible domain name’s our server hosts. Now we need to add an .htaccess file in each site’s public_html directory.

First of all you need somehow a list of all directories of /var/www/ directory. Everyone can do it.
ls -lah /var/www/

ok but you have both ../ and ./

just try

ls -ld /var/www/*/

now let’s say that the directories are named according to their domain tld (top-level domain) for example for the webplay.pro domain there is a webplay.pro directory and for the strimpak.com domain there is a strimpak.com directory and you want to copy the .htaccess not to all directories but to the .com domain corresponding directories. You can use grep to do this:

ls -ld /var/www/*/ | grep “.com”

Now it’s time to use awk tool! By using awk you can do a lot of cool things. One of this is that you can split your input to fields separetaed by tab (\t).

Just try this:

ls -ld /var/www/*/ | grep “.com” | awk ‘{print $9}’

and the result is to get a list of full paths which you want to use for a copy command. But you want to copy a file to /var/www/strimpak.com/public_html/ directory and not to
/var/www/strimpak.com/ so all you have to do is to use more the awk tool as below:

ls -ld /var/www/*/ | grep gr | awk ‘{print $9 “public_html/”}’

Using the list to execute commands

And now we have created a list of all directories where we want to copy a file or a directory or do whatever we want in a single line! Now we will use the xargs tool to pass the list we created to another command:

ls -ld /var/www/*/ | grep gr | awk ‘{print $9 “public_html/”}’ | xargs -n 1 echo “copy in here”

In the above example we passed the list to the echo command, just for testing purposes. We used the | (pipe) and passed the list to the xargs, with the ” -n 1″ switches we tell the xargs to use one argument at most per command.

Being safe

I ‘ll get a little bit out of topic but it’s crusial to make a refference to. The command we choosed to use is echo and not cp because you cannot do anything irreversible with echo. By copying file we may do something we would like to undo. Linux (like other unices) doesn’t natively provide an undo feature. The philosophy is that if it’s gone, it’s gone. If it was important, it should have been backed up.

Another way to protect against such accidents is to use a version control system (git, mercurial, subversion, …). It takes a little time to learn, but it pays off awesomely in the medium and long term.

Back to our main topic, we need to copy the file: /var/www/.htaccess to the directories selected:

ls -ld /var/www/*/ | grep gr | awk ‘{print $9 “public_html/”}’ | xargs -n 1 echo cp /var/www/.htaccess

and the job got done!!!

I hope you found how to copy a file or a directory to multiple folders in linux article useful. Whatever is told in this article can be used with your own risk.

New digitalocean promo code May 2014 for new customers!!!

If you believe in the momentum, it’s time to give digitalocean a try!!! A new digitalocean promo code is available for new customers for May 2014!

Go @ digitalocean website and sign up

To get your first Droplet going (Digital Ocean’s name for a VPS deployment) you have to enter your billing details as well as hand over your credit card. Digital Ocean actually offers per-hour billing for all their VPS offerings, so the credit card information is taken just to ensure they can bill you at the end of the month. By the way my choice of payment was through paypal! I added 10$ for the first two months.

Now after confirming your account you can add this promo code:

SSDMAY10

and you will get 10$ free!!!

Go at digitalocean right now and use the above promo code, add 10$ more to your billing and you will have 4 months to try digitalocean and become a fan!!!

New digitalocean promo code April – May 2014 for new customers!!!

If you believe in the momentum, it’s time to give digitalocean a try!!! A new digitalocean promo code is available for new customers!

Go @ digitalocean website and sign up

To get your first Droplet going (Digital Ocean’s name for a VPS deployment) you have to enter your billing details as well as hand over your credit card. Digital Ocean actually offers per-hour billing for all their VPS offerings, so the credit card information is taken just to ensure they can bill you at the end of the month. By the way my choice of payment was through paypal! I added 10$ for the first two months.

Now after confirming your account you can add this promo code:

DEPLOY2GO

and you will get 10$ free!!!

Go at digitalocean right now and use the above promo code, add 10$ more to your billing and you will have 4 months to try digitalocean and become a fan!!!

The reasons I like digitalocean:

Clean and Easy to Use Control Panel

A friendly interface to control all of your virtual servers. Create, resize, rebuild, and snapshot with single clicks. I also like the DNS management section in Control Panel, that provides an excellent way to manage delegated dns zones!

Linux Distributions & One-Click Install Apps

All of your favorite Linux distributions are supported, including the all new Ubuntu 14.04. Additionally, install your favorite applications on your cloud server with a click of a button. When creating a droplet, just click on the ‘Applications’ tab and your app will be live on your droplet in 55 seconds.

SSD-Only Cloud

Digitalocean has created the fastest and most convenient cloud technology to help you easily and more efficiently manage your infrastructure so you can get back to coding. I am fan of their high-performance SSD Hard Drives, flexible API, and the ability to select to nearest data center location. I cannot believe that it’s an SSD-Only Cloud.