Tuesday, August 26, 2014

Generating Diffs with Python

Two similar silhouettes running in rain
Two similar silhouettes running in rain | Internet Archive Book Images
When managing large numbers of servers, one of the hardest problems to deal with is standard configuration drift, especially in big enterprise multi-admin environments. Overtime as various sysadmins fix small issues on individual servers, the systems diverge from the standard settings that other admins expect to find on the boxes. This causes big issues down the road when trying to troubleshoot a followup issue.
As system admins we all have our scrips we write to make our lives easier, and while the standard diff tool that comes with linux is great, sometimes we need a “Pythonic” way to do things. Enter DiffLIb

Take the following example hosts file: hatchylapy hatchylapy.home localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 thehatchers.us printermain.home

The “standard” however doesn't include the last two entries. Perhaps the admin needed this until DNS was updated but forgot to return or they forgot to create a ticket to have DNS changed, either way we need a way to detect that there is a difference and what they are.

To test the local file against a baseline you have in your current working directory:

import difflib
from difflib_data import *

with open ("./hosts", "r") as myfile:
with open ("/etc/hosts", "r") as myfile:
differ = difflib.Differ()
diff = differ.compare(CurHosts, OrgHosts)
print '\n'.join(diff)
this would produce: hatchylapy hatchylapy.home localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
+ thehatchers.us
+ printermain.home

This really comes into its own when using something like Paramiko or Pexpect to crawl many servers to compare against baselines. I used this in my scripts to check and documents outliers in common files such as hosts and nsswitch then compile a html report (with mako) for remediation.

Check out http://pymotw.com/2/difflib/ and https://docs.python.org/2/library/difflib.html for the wealth of options available with this module.

Monday, May 26, 2014

Install Git 1.8 on RHEL 6

While PyCharm does work with the native version of Git that ships with RHEL 6.5, it does complain a lot. I also feared that something would go wrong eventually so I started looking around on updating.

There wasn't a newer version of Git in fast track so I had to go with somthing else. Luckly I found the IUS Community Project. 

The IUS Community Project is aimed at providing up to date and regularly maintained RPM packages for the latest upstream versions of PHP, Python, MySQL and other common software specifically for Redhat Enterprise Linux. IUS can be thought of as a better way to upgrade RHEL, when you need to.

The project is sponsored by Rackspace and is a spin off of their own internal project. A huge plus is that it is SafeRepo Aware. Meaning that when I installed the repo I couldn't just do a yum update and get the latest version of git (along with any other software and totally wreak my system). To install git 1.8 I had to yum install git18

Now when I yum search git, I see (along with others):

git.x86_64 : Fast Version Control System
git18.x86_64 : Fast Version Control System
So I can choose which version I want without upgrading all the great stock RHEL packages on my system.

Well enough of my talking, to the script!!

Latest version is at Github:

As Root:

wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/x86_64/epel-release-6-5.noarch.rpm

yum install epel-release-6-5.noarch.rpm

wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/x86_64/ius-release-1.0-11.ius.el6.noarch.rpm

yum install ius-release-1.0-11.ius.el6.noarch.rpm

yum install git18

Tuesday, August 20, 2013

Exchange 2013 | Enable all users' mailboxes and add all users to distribution group

We are currently in the process of setting up an all new domain and related services. We have some techs doing the creation of users but those needed mailboxes created in exchange. So instead of manually creating the mailboxes in the ECP I just ran this script from the domain administrator login on the exchange  CAS:
Get-User -RecipientTypeDetails User -Filter { UserPrincipalName -ne $Null } | Enable-Mailbox

I had already created an "All Users" distribution group so I needed to add these new users to the group:

Get-User -RecipientTypeDetails UserMailbox  -Filter { UserPrincipalName -ne $Null } | Add-DistributionGroupMember -Identity "All Users" 

I got some errors on the last command, however they were just warnings that some users were already members of the all users group, so no problem at all.

 Hope you find this useful,

Sunday, March 17, 2013

Ubuntu 12.04 LTS Drupal 7 prerequisites

Here is a list of packages needed for Drupal 7 on Ubuntu 12.04 LTS

apt-get install apache2 apache2-mpm-prefork apache2-utils apache2.2-bin apache2.2-common dbconfig-common fontconfig-config libapache2-mod-php5 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libcap2 libdbd-mysql-perl libdbi-perl libfontconfig1 libgd2-xpm libhtml-template-perl libjpeg-turbo8 libjpeg8 libmysqlclient18 libnet-daemon-perl libplrpc-perl libt1-5 libxpm4 mysql-client mysql-common php5 php5-gd php5-mysql postfix psmisc ssl-cert ttf-dejavu-core wwwconfig-common  php-pear

You will also need Drush

pear channel-discover pear.drush.org
pear install drush/drush
pear upgrade-all