Friday 17 June 2016

Contrail Analytics Application Programming Interfaces (API) -UVE


Contrail Analytics collects information from the various components of the system, and provides the visibility into flows, logs and UVEs that is needed to operate this system. This information is provided via a REST API, which can be used to build management and analytics applications and dashboards. The Contrail UI uses this REST API as well.


To get information about a UVE, you must have the UVE type and the UVE key. In Contrail, UVEs are identified by type, such as virtual network, virtual machine, vRouter, and so on. A system-wide unique key is associated with each UVE. The key type could be different, based on the UVE type

First we need to get the correct UVE key like below:


Then after selecting the required UVE key(vRouter in the below example) we can get the required information:


Selecting the particular vRouter we can get the detailed information:
http://172.16.5.93:8081/analytics/uves/vrouter/vm-hyper3?flat




Thursday 16 June 2016

installing VMwareTools

First download the VMwareTools file from the below location:


https://my.vmware.com/group/vmware/details?productId=529&downloadGroup=VMTOOLS1006

Now follow these steps after copying the VMWareTools file to  /tmp directory

tar -xvzf VMwareTools-xxxxxx.tar.gz

cd vmware-tools-distrib/

./vmware-install.pl


at this point just follow the instructions till you get this message



Enjoy,

--the VMware team

Sunday 5 June 2016

updating python script to SSH with and without Enable password for Cisco devices

python script to SSH with and without Enable password for Cisco devices
i have tried this on Cisco IOS, IOS-XE and NX-OS and works for me.

Without enable password requirement to print 'Show hostname' and 'Show version'(you can change that to whatever CLI you need to print.

import paramiko

def sshCall(host,port,user,passw):
  dssh = paramiko.SSHClient()
  dssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  dssh.connect(hostname=host,port=port,username=user,password=passw)
  dssh.exec_command('term len 0')
  stdin, stdout, stderr = dssh.exec_command('show hostname')
  bc=stdout.read()
  print bc
  # b=bc.strip('\n')
  stdin, stdout, stderr = dssh.exec_command('show version')
  a= stdout.read()
  print a
  f = open('/output.txt', 'a')

  f.write(bc)

  f.write(a)
  f.close()
  dssh.close()

sshCall('1.1.1.1',22,"username","password)

With enable password

import paramiko
import time

def sshCall(host,port,user,passw):
  dssh = paramiko.SSHClient()
  dssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  dssh.connect(hostname=host,port=port, username=user,password=passw)
  shell = dssh.invoke_shell()
  time.sleep(1)
  shell.send('enable\n')
  shell.send('FixStream123!\n')
  time.sleep(1)
  shell.send('term len 0\n')

  shell.send('show version\n')
  time.sleep(2)
  resp = shell.recv(9999)
  print resp
  dssh.close()

sshCall('1.1.1.1',22,"usr","pwd")

Saturday 4 June 2016

Juniper Contrail Python API


You can download the Contrail python API Bindings from here

https://github.com/Juniper/contrail-python-api

Install instructions

git clone https://github.com/Juniper/contrail-python-api
cd contrail-python-api
python setup.py install

So the basic example explains how to connect using the Client Method and list Virtual_Networks


import pycontrail.client as client
conn = client.Client(url='http://10.84.34.141:8082',
        auth_params={'type':'keystone',
        'auth_url':'http://10.84.34.141:5000/v2.0',
        'username':'admin',
        'password':'secret',
        'tenant_name':'admin'})

conn.virtual_networks_list()

I am going to explain how to collect Data that will make some sense, although a lot of python enthusiasts will figure it out but if this is any help id be happy
reason why i wanted to share this script is that i had taken time to write the loops to get all the dict data within lists etc. 

I printed all the output in JSON format as final output

from pycontrail import client
import jsonpickle

import jsonpickle
conn = client.Client(
        url='http://1.1.1.1:8082',auth_params={'type':'keystone','auth_url':'http://2.2.2.2:5000/v2.0','username':'admin','password':'password','tenant_name':'admin'})

route_instance_uuid_list=[]
bb = conn.routing_instances_list()
for each_route_instance in bb['routing-instances']:
    route_instance_uuid_list.append(each_route_instance['uuid'])


route_instance_detail_list=[]
for each_route_instance in route_instance_uuid_list:
    vn_detail_dict={'name':None,'route_instance':[],'route_target':[]}
    aa= conn.routing_instance_read(id=each_route_instance)
    vn_detail_dict['name'] =aa.name
    # print aa.name


    #route_instance
    try:
        route_instance=[]


        for each_routing in aa.routing_instance_refs:
            route_instance_dict={}
            route_instance_dict['route_instance']= dict(zip(range(0,len(each_routing['to'])), each_routing['to']))
            route_instance.append(route_instance_dict)
    except:
        pass
    vn_detail_dict['route_instance']=route_instance
    #route_instance

    #route_targets
    try:
        route_target=[]

        for each_routetarget in aa.route_target_refs:
            route_target_dict={}
            route_target_dict['route_target']= dict(zip(range(0,len(each_routetarget['to'])), each_routetarget['to']))
            route_target.append(route_target_dict)
    except:
        pass
    vn_detail_dict['route_target']=route_target
    #route_targets

    route_instance_detail_list.append(vn_detail_dict)



vn_instance_uuid_list=[]
vn=conn.virtual_networks_list()
for each_vn in vn['virtual-networks']:
    vn_instance_uuid_list.append(each_vn['uuid'])








vn_instance_detail_list=[]
for each_vn_instance in vn_instance_uuid_list:

    vn_instance_detail_dict={}
    gg=conn.virtual_network_read(id=each_vn_instance)
    vn_instance_detail_dict['virtual_network_network_id']= gg.virtual_network_network_id
    vn_instance_detail_dict['uuid']= gg.uuid
    vn_instance_detail_dict['name']= gg.name
    vn_instance_detail_dict['parent_name']= gg.parent_name
    vn_instance_detail_dict['parent_uuid']= gg.parent_uuid
    try:
        vn_instance_detail_dict['forwarding_mode']= gg.virtual_network_properties.forwarding_mode

        for each_ipam in gg.network_ipam_refs:
            ipam_subnet_list=[]
            for each_ipam_subnet in each_ipam['attr'].ipam_subnets:
                ipam_subnet_dict={}
                ipam_subnet_dict['default_gateway']= each_ipam_subnet.default_gateway
                ipam_subnet_dict['dns_server_address']= each_ipam_subnet.dns_server_address
                ipam_subnet_dict['subnet_uuid']= each_ipam_subnet.subnet_uuid
                ipam_subnet_dict['subnet_name'] =each_ipam_subnet.subnet_name
                ipam_subnet_dict['ip_prefix_len']= each_ipam_subnet.subnet.ip_prefix_len
                ipam_subnet_dict['ip_prefix']= each_ipam_subnet.subnet.ip_prefix
                ipam_subnet_list.append(ipam_subnet_dict)
                allocation_pool_list=[]
                for each_allocation_pool in  each_ipam_subnet.allocation_pools:
                    allocation_pool_dict={}
                    allocation_pool_dict['start'] =each_allocation_pool.start
                    allocation_pool_dict['end']= each_allocation_pool.end
                    allocation_pool_list.append(allocation_pool_dict)
                ipam_subnet_dict['allocation_pool']=allocation_pool_list

        vn_instance_detail_dict['ipam']=ipam_subnet_list

        network_policy_list=[]
        for each_network_policy in gg.network_policy_refs:
            network_policy_dict={}
            network_policy_dict['uuid']= each_network_policy['uuid']
            network_policy_dict['network_policy'] =dict(zip(range(0,len(each_network_policy['to'])), each_network_policy['to']))
            network_policy_list.append(network_policy_dict)
            vn_instance_detail_dict['network_policy']=network_policy_list

    except:
        pass
    vn_instance_detail_list.append(vn_instance_detail_dict)



np_uuid_list=[]
np_list = conn.network_policys_list()
for each_np in np_list['network-policys']:
    np_uuid_list.append(each_np['uuid'])


each_np_detail_list=[]
for each_np_uuid in np_uuid_list:
    each_np_detail_dict={'policy':[]}
    nppp = conn.network_policy_read(id=each_np_uuid)
    each_np_detail_dict['uuid']= nppp.uuid
    each_np_detail_dict['name']=  nppp.name
    each_np_detail_dict['parent_name']=  nppp.parent_name
    try:
        each_policy_list=[]
        for each_policy_entry in nppp.network_policy_entries.policy_rule:
            each_policy_dict={'dst_addresses':[],'src_addresses':[]}
            each_policy_dict['protocol']= each_policy_entry.protocol
            each_policy_dict['ethertype']= each_policy_entry.ethertype
            each_policy_dict['direction']= each_policy_entry.direction
            each_policy_dict['rule_uuid']= each_policy_entry.rule_uuid
            each_policy_dict['simple_action']=  each_policy_entry.action_list.simple_action
            each_policy_dict['gateway_name']=  each_policy_entry.action_list.gateway_name

            dst_address_list=[]
            for each_dst_address in each_policy_entry.dst_addresses:
                dst_address_dict={}
                dst_address_dict['network_policy']= each_dst_address.network_policy
                dst_address_dict['security_group']= each_dst_address.security_group
                dst_address_dict['subnet']= each_dst_address.subnet
                dst_address_dict['virtual_network']= each_dst_address.virtual_network
                dst_address_list.append(dst_address_dict)

            each_policy_dict['dst_addresses']=dst_address_list
            src_address_list=[]
            for each_src_address in each_policy_entry.src_addresses:
                src_address_dict={}
                src_address_dict['network_policy']= each_src_address.network_policy
                src_address_dict['security_group']=  each_src_address.security_group
                src_address_dict['subnet']=  each_src_address.subnet
                src_address_dict['virtual_network']=  each_src_address.virtual_network
                src_address_list.append(src_address_dict)
            each_policy_dict['src_addresses']=src_address_list
            each_policy_list.append(each_policy_dict)
        each_np_detail_dict['policy']=each_policy_list

    except:
        pass
    each_np_detail_list.append(each_np_detail_dict)



final_output = {
    'virtual_network_instance':vn_instance_detail_list,
    'route_instance':route_instance_detail_list,
    'network_policy':each_np_detail_list
}

print jsonpickle.pickler.encode(final_output)