Monday, 8 August 2016

How to install VMware tools on linux based vm


vSphere needs VMware tools to detect the IP of the vm. Following are the steps to install the VMware tools:

# To see the installed version 

vmware-toolbox-cmd -v
                             

# To see the currently installed packages

sudo apt --installed list | grep tools



# Steps to remove open-vm-tools

sudo apt-get remove --purge open-vm-tools
sudo apt-get -f install
sudo apt-get autoremove



# Steps to add vmware vm-tools 

sudo add-apt-repository "deb http://packages.vmware.com/tools/esx/6.0u1/ubuntu precise main"
sudo apt-get update
sudo apt-get install vmware-tools-core
sudo apt-get install vmware-tools-services
sudo apt-get install vmware-tools-esx-nox



# See the latest installed packages  

sudo apt --installed list | grep tools

           

# Verify latest version is installed

vmware-toolbox-cmd -v


Bash script to monitor uptime, cpu, memory, swap memory, load average of linux system

#!/bin/bash

# Print system resources
echo "HOSTNAME: "`hostname`
echo "SYSTEM: "`uname -a`
echo "IP ADDRESS: "; ifconfig eth0
echo "ROUTE TABLE:"; route -n
echo ""


# Start monitoring the system resources
while true
do
    echo ------ `date` ------
    top -b n1 | head -n 6
    sleep 60
done

Friday, 29 April 2016

Python code to read and write array to file in json format

First install and import 'json' module

import json

Reading JSON object from file:

def read_json_from_file(json_file):
    """
    Read array of objects from a file in json format.
    :param json_file: input json file name
    :return: array of objects from the json
    """
    with open(json_file, 'r') as f:
        exp_string = json.load(f)
        json_array = json.loads(exp_string)
    return json_array


Writing JSON to the file:

def write_json_to_file(array, out_file_name):
    """
    Dump an array to file in json format.
    :param array: Array to write to the file
    :param out_file_name: output file name
    """
    with open(out_file_name, 'w') as out_f:
        s = json.dumps([o.__dict__ for o in array], sort_keys=True)
        json.dump(s, out_f)

Get recursive file listing in python

Below is the python code:

def get_recursive_filelist(rootdir):
    outfiles = []
    if os.path.isfile(rootdir):
        outfiles.append(rootdir)
    for (dir, _, files) in os.walk(rootdir):
        for f in files:
            path = os.path.join(dir, f)
            if os.path.exists(path):
                outfiles.append(path)
    return outfiles

Easy way to check internet speed using command line speedtest tool

Here is the light weight command line speedtest tool to check the internet speed.

$ wget -O speedtest-cli https://raw.github.com/sivel/speedtest-cli/master/speedtest_cli.py
$ chmod +x speedtest-cli
$ ./speedtest-cli
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from Comcast Cable (x.x.x.x)...
Selecting best server based on ping...
Hosted by FiberCloud, Inc (Seattle, WA) [12.03 km]: 44.028 ms
Testing download speed........................................
Download: 32.29 Mbit/s
Testing upload speed..................................................
Upload: 5.18 Mbit/s

Easy way to parse command line arguments in python using argparse

Here is the easiest way to parse command line arguments in python.


import argparse

args = argparse.ArgumentParser(description='Description of the script.')
args.add_argument('-i', '--input', help='Input file name', required=True)
args.add_argument('-o', '--output', help='Output file', required=True)
args = args.parse_args()

input = args.input
output = args.output

print "Input:" + input
print "Ouput:" + output



Output:
Rahuls-MacBook-Pro:exception rahuldiyewar$ python test.py -h
usage: test.py [-h] -i INPUT -o OUTPUT

Description of the script.

optional arguments:
  -h, --help            show this help message and exit
  -i INPUT, --input INPUT
                        Input file name
  -o OUTPUT, --output OUTPUT
                        Output file

Rahuls-MacBook-Pro:exception rahuldiyewar$ python test.py -i myinput -o myoutput 
Input:myinput 
Ouput:myoutput

Python code to search and create bug in JIRA using rest api

Here is the Python code to search/open issues in JIRA:
  1. Search for open issues
  2. Create new issue

from jira import JIRA


class JIRARestAPI:
    url = 'https://<company-name>.atlassian.net'    
    username = '<username>'    
    password = '<password>'    
    jira = JIRA(url, basic_auth=(username, password))

    def search_issues(self, search_string):
        return JIRARestAPI.jira.search_issues(search_string)

    def print_bugs(self, bugs):
        for bug in bugs:
            self.print_bug(bug)

    def print_bug(self, bug):
        print ("%s, %s, \"%s\"" % (bug.key, bug.fields.status.name, bug.fields.summary))

    def create_cyg_bug(self, summary, description, label):
        issue_dict = {
            'project': {'id': 10000},
            'summary': summary,
            'description': description,
            'issuetype': {'name': 'Bug'},
            'labels': [label]
        }
        return JIRARestAPI.jira.create_issue(fields=issue_dict)