Monday, January 15, 2018

Dictionaries in Python

Hello Guys,

          In this particular post, I am going to discuss dictionaries in python. The example in this post is tested on Python-3.6.

          Python data structures include lists, dictionaries, tuples, and sets. If you have worked with python, then you may have worked on "lists", "tuples", "sets" and "dictionary" in python.

Dictionary - As the name suggests dictionary is the same as that ordinary dictionary where we will have index and then we'll use that index to search the specific entry. This is similar to book or phonebook index where we search things using names instead of values which we do in other python data structure. Dictionary worked in "key":"value" pair. The "word" is called key and the definition of "word" is called a value.
              Usually, the "values" or "keys" of a dictionary are not in specific order. Another import thing is that we can't sort a dictionary in python, but can make use of the list to sort dictionaries. We will this in the example. Apart from this, we can do all add, remove and modify operations on python dictionary.

Let's go through below given example and see the basic usages in practical-

Code(you can see this on github as well)


print ("\n Lets Play with Dictionary: ")

#Lets make a phone book:
#Define an empty dictionary

ages = {}
#Add few names to the dictionary
ages['Sam'] = 87
ages['Vik'] = 28
ages['Vishu'] = 54
ages['ND'] = 45
ages['Bunti'] = 37

# Display current Dictionary
print("*** Current Dictionary is :\t", ages)
#Use IN operator to check if specifid key is there in Dictionary. Syntax will be of this form:
#" if in "
#this will returns TRUE, if the dictionary has key-name in it
#but returns FALSE if it doesn't. for key, value in ages.items():

if "Sam" in ages:
    print ("\nSam is in the dictionary. He is", \
ages['Sam'], "years old")
    print ("\nSam is not in the dictionary")

#Use the function keys() -
#This function returns a list of all the names of the keys.

print ("\nThe following guys are in the dictionary:\t", \

#You could use this "keys" function to put all the key names in a list:
keys = ages.keys()

#You can also get a list of all the values in a dictionary.
#You use the values() function:
print ("People are aged the following:\t", \

#Now put all values i.e. ages associated with each guy in a list:
values = ages.values()

#You can sort lists, with the sorted() function. 
#It will sort all values in a list. alphabetically, numerically, etc...
#You can't sort dictionaries -they are in no particular order
print ("\nUnsorted Keys:\t", keys)
print ("Sorted Keys for Dictionary:\t", sorted(keys))
print ("\nUnsorted Values:\t", values)
print ("Sorted Values for Dictionary:\t", sorted(values))

#You can find the number of entries with the len() function:
print ("\nThe dictionary has", \
len(ages), "entries in it")

 Lets Play with Dictionary:
*** Current Dictionary is :      {'Sam': 87, 'Vik': 28, 'Vishu': 54, 'ND': 45, 'Bunti': 37}

Sam is in the dictionary. He is 87 years old

The following guys are in the dictionary:        dict_keys(['Sam', 'Vik', 'Vishu', 'ND', 'Bunti'])
People are aged the following:   dict_values([87, 28, 54, 45, 37])

Unsorted Keys:   dict_keys(['Sam', 'Vik', 'Vishu', 'ND', 'Bunti'])
Sorted Keys for Dictionary:      ['Bunti', 'ND', 'Sam', 'Vik', 'Vishu']

Unsorted Values:         dict_values([87, 28, 54, 45, 37])
Sorted Values for Dictionary:    [28, 37, 45, 54, 87]

The dictionary has 5 entries in it

Friday, October 20, 2017

How to reset mongodb rootadmin password ??

Sometimes we have to manage credentials for lots of things and its obvious that we can forget one of them. Here I am going to explain that how we can reset "rootadmin" password for running mongodb replica set.

I am not going in details about installation, configuration or functionality detail. Instead, I'll directly jump to actual technical hacking stuff. Along with with we'll also learn some sed tricks that can be handy on many occasions.

Things responsible for Authentication/Permissions with in mongodb replica set-

  keyFile: "/data/mongo3.2/node3/mongo_3.2-keyfile"
  authorization: "enabled"
keyFile - This is the path to file that stores the shared secret that all MongoDB instances use to authenticate to each other in a sharded cluster or replica set.
authorization - This will Enable or disable Role-Based Access Control (RBAC) to govern each user’s access to database resources and operations. By default, this will be Disabled

So, in working env when we forget rootadmin password. By the way "rootadmin" is the user who has all admin level access to perform anything on |admin| and other DBs.

$ mongo -u rootadmin -p secret   mongo-server:27017/admin
MongoDB shell version: 3.2.15
connecting to: mongo-server:27017/admin
2017-10-20T13:44:29.431+0200 E QUERY    [thread1] Error: Authentication failed. :
exception: login failed

 So, first of all, stop all nodes in MongoDB replica set. Comment out the three line which will be there in your configuration files. Here I am running three instances on the same node so using |sed| trick to comment and uncomment the multiple config files.

Before making changes -

$ grep -ri -A 1 -B 1 key node?/conf/mongod.conf 
node1/conf/mongod.conf:  keyFile: "/data/mongo3.2/node1/mongo_3.2-keyfile"
node1/conf/mongod.conf-  authorization: "enabled"
node2/conf/mongod.conf:  keyFile: "/data/mongo3.2/node2/mongo_3.2-keyfile"
node2/conf/mongod.conf-  authorization: "enabled"
node3/conf/mongod.conf:  keyFile: "/data/mongo3.2/node3/mongo_3.2-keyfile"
node3/conf/mongod.conf-  authorization: "enabled"

Comment out the security config parameter(Here I am commenting line number 20-22)-

$ sudo  sed  -i '20,22 s/^/#/' node?/conf/mongod.conf 

Check again after making changes(See the diference # ) -

$ grep -ri -A 1 -B 1 key node?/conf/mongod.conf
node1/conf/mongod.conf:#  keyFile: "/data/mongo3.2/node1/mongo_3.2-keyfile"
node1/conf/mongod.conf-#  authorization: "enabled"
node2/conf/mongod.conf:#  keyFile: "/data/mongo3.2/node2/mongo_3.2-keyfile"
node2/conf/mongod.conf-#  authorization: "enabled"
node3/conf/mongod.conf:#  keyFile: "/data/mongo3.2/node3/mongo_3.2-keyfile"
node3/conf/mongod.conf-#  authorization: "enabled"

Start all the nodes, and login without the password. 

$ mongo   mongo-server:27017/admin
MongoDB shell version: 3.2.15
connecting to: mongo-server:27017/admin
Server has startup warnings:
2017-10-20T13:40:19.999+0200 I CONTROL  [initandlisten]
MongoDB Enterprise rs0:PRIMARY> show dbs
admin  0.078GB
local  6.075GB
MongoDB Enterprise rs0:PRIMARY> db
MongoDB Enterprise rs0:PRIMARY> db.changeUserPassword("rootadmin","new-password")
MongoDB Enterprise rs0:PRIMARY>

Now, Uncomment security stuff again in config file and give restart to all nodes.

$ sudo  sed  -i '20,22 s/^#//' node?/conf/mongod.conf
$ grep -ri -A 1 -B 1 key node?/conf/mongod.conf
node1/conf/mongod.conf:  keyFile: "/data/mongo3.2/node1/mongo_3.2-keyfile"
node1/conf/mongod.conf-  authorization: "enabled"
node2/conf/mongod.conf:  keyFile: "/data/mongo3.2/node2/mongo_3.2-keyfile"
node2/conf/mongod.conf-  authorization: "enabled"
node3/conf/mongod.conf:  keyFile: "/data/mongo3.2/node3/mongo_3.2-keyfile"
node3/conf/mongod.conf-  authorization: "enabled" 

After that try to login again using new credentials.

$ mongo -u rootadmin -p new-password mongo-server:27017/admin
MongoDB shell version: 3.2.15
connecting to: mongo-server:27017/admin
Server has startup warnings:
2017-10-20T13:47:30.262+0200 I CONTROL  [initandlisten]
MongoDB Enterprise rs0:PRIMARY> show dbs
admin  0.078GB
local  6.075GB
MongoDB Enterprise rs0:PRIMARY> 
Note: If you are not using the config file and just passing arguments, then you can stop and then start replica set nodes without these parameters.

Tuesday, August 9, 2016

Check dependencies of local RPM package

We all know that for managing packages or softwares on any system, we need some kind of tool which can be used to manage the packages or softwares. Different distributions have its know tool for achieving the same.

                     For example for RHEL/Centos/Fedora we use RPM(RPM Package Manager)
 for all rpm package management which take care of "installation, uninstallation, update, query etc.

              So, sometimes whenever we install any package we get lots of error regarding the dependencies. Here we are discussing that how we can list dependencies associated with particular rpm file.

1.) Check for file i,e, yet package is not installed:
         rpm -qpR {rpm-file}  
e.g. test
    output truncated..
  2.) If packages is already installed:
      rpm -qR {package-name}
e.g. :

   3.) Dry run without installing the package:
         rpm -Uvh --test {rpm-file}

Finally, yes of course if you don't make these checks and just try to install then you will get list of missing dependencies as well.

Monday, April 25, 2016

Remount multiple NFS mount points on Client in one go

                                     Sometimes we may have number of mount points available on NFS clients and after making changes for any one of parameters, we have to remount all partitions. Doing umount and mount on multiple partitions will really be hectic job and there may be chances of human errors.
    We can do this using single command to achieve the same. Sharing some other commands as well, before moving to exact one :).

  • Get the list of all NFS mount points available on System:
                     Before moving ahead with changes, lets see how many partitions are their on system. Below command will do trick and give you all the nfs mount points without any header and headers.
    • Without headers -
                     #df -PF nfs | awk '{if(NR>1)print}'     # This command will suppress header line 

    • With headers-
                   #df -PF nfs 

  • Here goes the actual thing, where we need to mount and remount multiple nfs partitions after making changes to parameters.
                    #for M in $(mount | awk '/type nfs / {print $3;}'); do echo $M; sudo umount $M && sudo mount $M && echo "ok :)"; done

[Note: Execute at your own risk after doing testing on test environments ;) ]

Monday, March 21, 2016

"ERROR: Could not find cookbook in your cookbook path, skipping it" in Chef

Chef is a automation framework tool which help us to deploy code or configuration across multiple systems which may be physical, virtual or cloud systems.

Here I am just want to highlight one small issue which I got when I was trying to upload cookbook from my workstation. Everything was in place, but still it was throwing below error:

$ knife  cookbook upload cookbook_nameERROR: Could not find cookbook cookbook_name in your cookbook path, skipping it
ERROR: Failed to upload 1 cookbook.

Usually, by default knife will use default location specified in ~/.chef/knife.rb file for cookbook. In my everything was correctly configured as below :

$ cat ~/.chef/knife.rb | grep cookbook_path
cookbook_path [ '.', '..' ]
I was trying to upload as per directory name given to cookbook.

After, doing lots of search finally got to know that knife command will compare the cookbook name from metadata.rb file in cookbook directory. Then I made the correction in metadata.rb file and it works like charm as below:

Friday, October 2, 2015

Get Oracle Version details

In this small post, I am sharing the commands to get details about the oracle version you are using. Although much detail is there in Oracle documentation, but thought of sharing this small tips :).


Connect to Oracle DB using CLI or UI tool as you wish. Here I am connecting with Oracel SQL Developer. There are number of ways for getting the details. I sharing below ones.

  1. select * from v$version;

  1.    2. select version from v$instance;

  2.             3. select * from product_component_version;*

  3. Regarding the release number format, there is very good explanation on Oracle Doc site. Please go through it for details.
  4. Thanks!!

Thursday, October 1, 2015

boot2docker Error

I have installed boot2docker and when I tried to play with docker, I have started getting below error for every docker commands that I run.


FATA[0000] Get http:///var/run/docker.sock/v1.18/version: dial unix /var/run/docker.sock: An address incompatible with the requested protocol was used.. Are you trying to connect to a TLS-enabled daemon without TLS?

I have checked about the vm status and everything seems good, as below:

After some troubleshooting, I found that issue was with some system variables. Basically there are three variables, which you need to set to make this working. In windows you can use set command and in Linux you can use export command:

   set DOCKER_HOST=tcp://    
   set DOCKER_CERT_PATH=C:\Users\kuldeep.d.sharma\.boot2docker\certs\boot2docker-vm   

Note: When you initialize your boot2docker, it will provide you all details and ask to update variables. 
P.S.- Change above values accordingly :).

Below is the screen shot, where I tried to set each variable and saw the different result and dependencies of these variables on each other.