CentOS – Obtain IP address for a new machine

I kept trying ifconfig but never got the IP address of the server. So I had to do ifdown eth0 followed by ifup eth0. After this, ifconfig returned the IP address of the machine. Such a learning curve for CentOS coming from Debian.

LDAP authentication using PHP

A simple script to test out LDAP authentication via PHP.

<?php

$host = 'ldap-host.domain.pvt';
$port = '636';
$protocol = 'ldaps';
$base_dn = 'ou=corp,dc=domain,dc=pvt';
$domain   = "@domain.pvt";

$username = 'username';
$password = 'Password';
$connection_string = "$protocol://$host:$port";
print "$connection_string\n";
$conn = ldap_connect($connection_string) or die("Could not connect: $connection_string");
print $conn."\n";
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($conn, LDAP_OPT_REFERRALS, 0);
$ldaprdn = $username.$domain;
$ldapbind = ldap_bind($conn, $ldaprdn, $password);
print $ldapbind."\n";
$search = ldap_search($conn, $base_dn, "(samaccountname=$username)");
var_dump($search);
$result = ldap_get_entries($conn, $search);
print $result['count']."\n";
print "End\n\n";

?>

Allowing multiple users to pull from a git repository on a shared server

A pretty common situation. Multiple developers working together on their local development machines and connecting to a central git repository with their own credentials. And there’s a dev/staging server where all users need to to have access to go login and pull the code. When you clone the git repository on the server, it essentially sets it up as the user who set it up and doesn’t allow other users to pull the code. Here’s how the config looks like:

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = ssh://varun.verma@server.path.net/var/git/project_repo.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
[branch "dev"]
        remote = origin
        merge = refs/heads/dev
[branch "task/upgrading_django"]
        remote = origin
        merge = refs/heads/task/upgrading_django

And the user can pull the repository using git pull

Now, in order to configure the repository so that all users who have access to the server are able to pull the code, here’s how the configuration should look like:

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = nouser@server.path.net/var/git/project_repo.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
[branch "dev"]
        remote = origin
        merge = refs/heads/dev
[branch "task/upgrading_django"]
        remote = origin
        merge = refs/heads/task/upgrading_django
[remote "varun.verma"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = varun.verma@server.path.net:/var/git/project_repo.git
[remote "other.user"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = other.user@server.path.net:/var/git/project_repo.git

And pull the repository by executing sudo git pull varun.verma task/upgrading_django. The system would ask for your local server password to sudo followed by the password for the remote git repository. This should resolve the problem.

Divide files into folders and zip them

I have a set of files that I usually have to divide into multiple folders and zip them together so I can upload on the cloud. It’s been a long time I’ve been manually doing this but then I thought I’d rather automate this altogether. So now all I have to do is calculate how many folders I need and a prefix for the folder, e.g. Color or BW and the program does the rest for me. It creates the folders, equally divides the files into those folders and zip them. Once the program finishes execution, I’m ready to upload them.

#/usr/bin/bash

# Enter number of folders to be created
# The program will create those folders and divide and move the files equally within those folders

# Exit if number of folders not provided
if [[ $1 -eq 0 ]]; then
	echo 'Enter number of folders to be created'
	exit 0
fi

# Exit if folder_previx is not provided
if [[ $2 = '' ]]; then
	echo 'Enter the prefix for the folder, e.g. Color or BW'
	exit 0
fi

# Get total number of files in directory
file_count=`ls -lrt *.jpg | wc -l`
echo Total files: $file_count

num_folders=$1
files_per_folder=$((file_count / num_folders))
files_per_folder=$((files_per_folder+1))
echo Files per folder: $files_per_folder

# Create folders; Divide and move images to folders and zip them so they are ready to upload
for ((i=1; i<=$num_folders; i++)); do
	if [[ $i -lt 10 ]]; then
		mkdir $2-Part0$i
		find . -name "*.jpg" -maxdepth 1 -type f | head -$files_per_folder | xargs -I{} mv {} $2-Part0$i
		zip -r $2-Part0$i.zip $2-Part0$i
	else
		mkdir $2-Part$i
		find . -name "*.jpg" -maxdepth 1 -type f | head -$files_per_folder | xargs -I{} mv {} $2-Part$i
		zip -r $2-Part$i.zip $2-Part$i
	fi
done

Executing psql from command prompt on Mac OSX after installing PostgreSQL

I installed PostgreSQL but wasn’t able to execute psql on the command prompt to execute queries. I had to add the PostgreSQL path to the PATH variable in order to do that. It’s best to append the path in the bash_profile so it’s ready and available every time the shell loads. Add the following lines (depending on the location of your PostgreSQL version)

# Modify PATH
export PATH=$PATH:/Library/PostgreSQL/9.5/bin

Mac – Update openssl

I had a hard time updating openssl on my Mac OSX. Here’s what I did to update the libraries:

Install and upate brew

brew update
brew install openssl
brew link --force openssl

openssl version

If one of the bad versions come up (1.0.1a-f), you can figure out which version of openssl you’re using, this way:

which openssl

Often this is from /usr/bin. To make sure you get the updated version, drop a symlink into /usr/local/bin to point to the updated openssl, like this:

ln -s /usr/local/Cellar/openssl/1.0.1g/bin/openssl /usr/local/bin/openssl

Note: You may have to exit and start the Terminal session to see the new changes.

As an alternative to that final step, some people replace the openssl in /usr/bin with a symlink to /usr/local/Cellar/openssl/1.0.1g/bin/openssl (or whatever your version is):

mv /usr/bin/openssl /usr/bin/openssl_OLD
ln -s /usr/local/Cellar/openssl/1.0.1g/bin/openssl /usr/bin/openssl

But this is known to cause problems with some more recent versions of OSX. Better to just insert a new symlink into /usr/local/bin, which should take precedence on your path over /usr/bin.

Original article: http://apple.stackexchange.com/questions/126830/how-to-upgrade-openssl-in-os-x