Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Wednesday, 28 May 2014

python for mysql

# You might need the following module to be installed in the system:
MySQL-python

So you can try using " sudo pip install MySQL-python " or " sudo easy-install MySQL-python"

if you are getting error while installing MySQL-python [ apt-get install python-mysqldb ]

#!/usr/bin/env python
import MySQLdb

db = MySQLdb.connect(host="hostname", # your host, usually localhost
                     user="username", # your username
                      passwd="password", # your password
                      db="database") # name of the data base

# you must create a Cursor object. It will let
#  you execute all the queries you need
cur = db.cursor()

# Use all the SQL you like
cur.execute("select * from Country where Code2 = 'IN';")

# print all the first cell of all the rows
for row in cur.fetchall() :
#    print row[0]
    rows = row   
    print(type(rows))  
    MyList=list(rows)
    print(type(MyList))
    print('Following is in tuple format')
    print(rows)
    print('Following is the list format')
    print(MyList)
#    print row


############ There are other module for connecting the mysql, with different classes." ####

mysql-connector-python


you can try, in python prompt:

>>> dir(MySQLdb) ## to see the modules classes.

## to see installed python module: ## pip list


external link:
http://mysql-python.sourceforge.net/MySQLdb.html

Tuesday, 27 May 2014

python string replace

Lets say we have a file, where we have hosts name and there true and false status. We checked the health status of the health and found we need to make it false, if its true, because of the health check is failing: how to do this:


bash: sed -i 's/true \(host1.*\)/false \1/g' 

bash: to print file from line 5 to 10:
 sed -n '5,10p' filename

file example:
true host1.example.com
true host2.example.com



cat true-false.py
#! /usr/bin/env python

myfile = open('haproxy-file.txt')
for line in myfile:
  if 'host1' in line:
    print (line.replace('true','false'))



# How I will do inline of a file and update the file?, with the new value.

#! /usr/bin/env python

myfile = open('haproxy-file.txt')
for line in myfile:
  if 'host1' in line:
    UpdateLine = line.replace('true','false')
    MyWriteFile = open('haproxy-file.txt','r+')
    MyWriteFile.write(UpdateLine)
    MyWriteFile.close()



In the above script, still some error are there: [will fix and update. ]

what is the error:
# Output:
false host1.example.com
rue host2.example.com #-> NOTE: 't' is missing over here

# Expecting Output:
false host1.example.com
true host2.example.com

Tuesday, 1 April 2014

getting help using ipython for python modules


import subprocess or from subprocess import os

subprocess.<TAB>
subprocess.CalledProcessError  subprocess.call                subprocess.gc                  subprocess.select
subprocess.MAXFD               subprocess.check_call          subprocess.list2cmdline        subprocess.signal
subprocess.PIPE                subprocess.check_output        subprocess.mswindows           subprocess.sys
subprocess.Popen               subprocess.errno               subprocess.os                  subprocess.traceback
subprocess.STDOUT              subprocess.fcntl               subprocess.pickle              subprocess.types



subprocess.os.<TAB>
Display all 218 possibilities? (y or n)
subprocess.os.EX_CANTCREAT      subprocess.os.TMP_MAX           subprocess.os.ftruncate         subprocess.os.renames
subprocess.os.EX_CONFIG         subprocess.os.UserDict          subprocess.os.getcwd            subprocess.os.rmdir
subprocess.os.EX_DATAERR        subprocess.os.WCONTINUED        subprocess.os.getcwdu           subprocess.os.sep
subprocess.os.EX_IOERR          subprocess.os.WCOREDUMP         subprocess.os.getegid           subprocess.os.setegid
subprocess.os.EX_NOHOST         subprocess.os.WEXITSTATUS       subprocess.os.getenv            subprocess.os.seteuid
subprocess.os.EX_NOINPUT        subprocess.os.WIFCONTINUED      subprocess.os.geteuid           subprocess.os.setgid
subprocess.os.EX_NOPERM         subprocess.os.WIFEXITED         subprocess.os.getgid            subprocess.os.setgroups
subprocess.os.EX_NOUSER         subprocess.os.WIFSIGNALED       subprocess.os.getgroups         subprocess.os.setpgid
subprocess.os.EX_OK             subprocess.os.WIFSTOPPED        subprocess.os.getloadavg        subprocess.os.setpgrp
subprocess.os.EX_OSERR          subprocess.os.WNOHANG           subprocess.os.getlogin          subprocess.os.setregid
subprocess.os.EX_OSFILE         subprocess.os.WSTOPSIG          subprocess.os.getpgid           subprocess.os.setresgid
subprocess.os.EX_PROTOCOL       subprocess.os.WTERMSIG          subprocess.os.getpgrp           subprocess.os.setresuid
subprocess.os.EX_SOFTWARE       subprocess.os.WUNTRACED         subprocess.os.getpid            subprocess.os.setreuid
subprocess.os.EX_TEMPFAIL       subprocess.os.W_OK              subprocess.os.getppid           subprocess.os.setsid
subprocess.os.EX_UNAVAILABLE    subprocess.os.X_OK              subprocess.os.getresgid         subprocess.os.setuid
subprocess.os.EX_USAGE          subprocess.os.abort             subprocess.os.getresuid         subprocess.os.spawnl
subprocess.os.F_OK              subprocess.os.access            subprocess.os.getsid            subprocess.os.spawnle
subprocess.os.NGROUPS_MAX       subprocess.os.altsep            subprocess.os.getuid            subprocess.os.spawnlp
subprocess.os.O_APPEND          subprocess.os.chdir             subprocess.os.initgroups        subprocess.os.spawnlpe
subprocess.os.O_ASYNC           subprocess.os.chmod             subprocess.os.isatty            subprocess.os.spawnv
subprocess.os.O_CREAT           subprocess.os.chown             subprocess.os.kill              subprocess.os.spawnve
subprocess.os.O_DIRECT          subprocess.os.chroot            subprocess.os.killpg            subprocess.os.spawnvp
subprocess.os.O_DIRECTORY       subprocess.os.close             subprocess.os.lchown            subprocess.os.spawnvpe
subprocess.os.O_DSYNC           subprocess.os.closerange        subprocess.os.linesep           subprocess.os.stat
subprocess.os.O_EXCL            subprocess.os.confstr           subprocess.os.link              subprocess.os.stat_float_times
subprocess.os.O_LARGEFILE       subprocess.os.confstr_names     subprocess.os.listdir           subprocess.os.stat_result
subprocess.os.O_NDELAY          subprocess.os.ctermid           subprocess.os.lseek             subprocess.os.statvfs
subprocess.os.O_NOATIME         subprocess.os.curdir            subprocess.os.lstat             subprocess.os.statvfs_result
subprocess.os.O_NOCTTY          subprocess.os.defpath           subprocess.os.major             subprocess.os.strerror
subprocess.os.O_NOFOLLOW        subprocess.os.devnull           subprocess.os.makedev           subprocess.os.symlink
subprocess.os.O_NONBLOCK        subprocess.os.dup               subprocess.os.makedirs          subprocess.os.sys
subprocess.os.O_RDONLY          subprocess.os.dup2              subprocess.os.minor             subprocess.os.sysconf
subprocess.os.O_RDWR            subprocess.os.environ           subprocess.os.mkdir             subprocess.os.sysconf_names
subprocess.os.O_RSYNC           subprocess.os.errno             subprocess.os.mkfifo            subprocess.os.system
subprocess.os.O_SYNC            subprocess.os.error             subprocess.os.mknod             subprocess.os.tcgetpgrp
subprocess.os.O_TRUNC           subprocess.os.execl             subprocess.os.name              subprocess.os.tcsetpgrp
subprocess.os.O_WRONLY          subprocess.os.execle            subprocess.os.nice              subprocess.os.tempnam
subprocess.os.P_NOWAIT          subprocess.os.execlp            subprocess.os.open              subprocess.os.times
subprocess.os.P_NOWAITO         subprocess.os.execlpe           subprocess.os.openpty           subprocess.os.tmpfile
subprocess.os.P_WAIT            subprocess.os.execv             subprocess.os.pardir            subprocess.os.tmpnam
subprocess.os.R_OK              subprocess.os.execve            subprocess.os.path              subprocess.os.ttyname
subprocess.os.SEEK_CUR          subprocess.os.execvp            subprocess.os.pathconf          subprocess.os.umask
subprocess.os.SEEK_END          subprocess.os.execvpe           subprocess.os.pathconf_names    subprocess.os.uname
subprocess.os.SEEK_SET          subprocess.os.extsep            subprocess.os.pathsep           subprocess.os.unlink
subprocess.os.ST_APPEND         subprocess.os.fchdir            subprocess.os.pipe              subprocess.os.unsetenv
subprocess.os.ST_MANDLOCK       subprocess.os.fchmod            subprocess.os.popen             subprocess.os.urandom
subprocess.os.ST_NOATIME        subprocess.os.fchown            subprocess.os.popen2            subprocess.os.utime
subprocess.os.ST_NODEV          subprocess.os.fdatasync         subprocess.os.popen3            subprocess.os.wait
subprocess.os.ST_NODIRATIME     subprocess.os.fdopen            subprocess.os.popen4            subprocess.os.wait3
subprocess.os.ST_NOEXEC         subprocess.os.fork              subprocess.os.putenv            subprocess.os.wait4
subprocess.os.ST_NOSUID         subprocess.os.forkpty           subprocess.os.read              subprocess.os.waitpid
subprocess.os.ST_RDONLY         subprocess.os.fpathconf         subprocess.os.readlink          subprocess.os.walk
subprocess.os.ST_RELATIME       subprocess.os.fstat             subprocess.os.remove            subprocess.os.write
subprocess.os.ST_SYNCHRONOUS    subprocess.os.fstatvfs          subprocess.os.removedirs       
subprocess.os.ST_WRITE          subprocess.os.fsync             subprocess.os.rename        


subprocess.sys.<TAB>
subprocess.sys.api_version            subprocess.sys.exitfunc               subprocess.sys.last_value             subprocess.sys.pydebug
subprocess.sys.argv                   subprocess.sys.flags                  subprocess.sys.long_info              subprocess.sys.setcheckinterval
subprocess.sys.builtin_module_names   subprocess.sys.float_info             subprocess.sys.maxint                 subprocess.sys.setdlopenflags
subprocess.sys.byteorder              subprocess.sys.float_repr_style       subprocess.sys.maxsize                subprocess.sys.setprofile
subprocess.sys.call_tracing           subprocess.sys.getcheckinterval       subprocess.sys.maxunicode             subprocess.sys.setrecursionlimit
subprocess.sys.callstats              subprocess.sys.getdefaultencoding     subprocess.sys.meta_path              subprocess.sys.settrace
subprocess.sys.copyright              subprocess.sys.getdlopenflags         subprocess.sys.modules                subprocess.sys.stderr
subprocess.sys.displayhook            subprocess.sys.getfilesystemencoding  subprocess.sys.path                   subprocess.sys.stdin
subprocess.sys.dont_write_bytecode    subprocess.sys.getprofile             subprocess.sys.path_hooks             subprocess.sys.stdout
subprocess.sys.exc_clear              subprocess.sys.getrecursionlimit      subprocess.sys.path_importer_cache    subprocess.sys.subversion
subprocess.sys.exc_info               subprocess.sys.getrefcount            subprocess.sys.platform               subprocess.sys.version
subprocess.sys.exc_type               subprocess.sys.getsizeof              subprocess.sys.prefix                 subprocess.sys.version_info
subprocess.sys.excepthook             subprocess.sys.gettrace               subprocess.sys.ps1                    subprocess.sys.warnoptions
subprocess.sys.exec_prefix            subprocess.sys.hexversion             subprocess.sys.ps2                   
subprocess.sys.executable             subprocess.sys.last_traceback         subprocess.sys.ps3                   
subprocess.sys.exit                   subprocess.sys.last_type              subprocess.sys.py3kwarning           

### Help ##


subprocess.sys.maxsize?
Type:       int
String Form:9223372036854775807
Docstring:
int(x=0) -> int or long
int(x, base=10) -> int or long

Convert a number or string to an integer, or return 0 if no arguments
are given.  If x is floating point, the conversion truncates towards zero.
If x is outside the integer range, the function returns a long instead.

If x is not a number or if base is given, then x must be a string or
Unicode object representing an integer literal in the given base.  The
literal can be preceded by '+' or '-' and be surrounded by whitespace.
The base defaults to 10.  Valid bases are 0 and 2-36.  Base 0 means to
interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4



subprocess.os.stat?
Type:       builtin_function_or_method
String Form:<built-in function stat>
Docstring:
stat(path) -> stat result

Perform a stat system call on the given path.




subprocess.os.uname?
Type:       builtin_function_or_method
String Form:<built-in function uname>
Docstring:
uname() -> (sysname, nodename, release, version, machine)

Return a tuple identifying the current operating system.

Sunday, 30 March 2014

selenium_python

Web applicaiton testing with Selenium and Python:


1: How to install selenium for python:

1a: check if you have easy_install [ for python pkg installation] in your system, if not install the same.
1b: sudo easy_install selenium

[or] you can try pip to install the same [pip install -U selenium ]
[or] download the file form PyPi and use: python setup.py install


2: [ Start Firefox and visit to www.google.com ]
In [1]: from selenium import webdriver

In [2]: browser = webdriver.Firefox()

In [3]: browser.get('http://www.google.com/')




====== Trying for auto login ==========

from selenium import webdriver

#Following are optional required
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException

baseurl = "http://www.irctc.co.in/"
username = "yourUsername"
password = "yourPassword"

xpaths = { 'usernameTxtBox' : "//input[@name='username']",
           'passwordTxtBox' : "//input[@name='password']",
           'submitButton' :   "//input[@name='login']"
         }

mydriver = webdriver.Firefox()
mydriver.get(baseurl)
#mydriver.maximize_window()

#Clear Username TextBox if already allowed "Remember Me"
#mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).clear()

#Write Username in Username TextBox
mydriver.find_element_by_xpath(xpaths['usernameTxtBox']).send_keys(username)

#Clear Password TextBox if already allowed "Remember Me"
mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).clear()

#Write Password in password TextBox
mydriver.find_element_by_xpath(xpaths['passwordTxtBox']).send_keys(password)

#Click Login button
mydriver.find_element_by_xpath(xpaths['submitButton']).click()



########## External #####
Youtube video:

Selenium For Pythonistas: https://www.youtube.com/watch?v=2OA941RLbmU

http://selenium-python.readthedocs.org/

Thursday, 27 March 2014

pulling aws instance details using python boto1


########### Example #########
import boto.ec2

# NOTE: if you don't put the key at conn then it will search for
# your profile file, if not /etc/boto.cfg if not your varible 'BOTO_CONFIG=/file/path'

conn=boto.ec2.connect_to_region('us-east-1')
#conn=boto.ec2.connect_to_region('us-east-1',aws_access_key_id='access_key' , aws_secret_access_key='secret_key')

# Following is the prod key: [ Full access ]
#AWS_ACCESS_KEY_ID = 'access_key'
#AWS_SECRET_ACCESS_KEY = 'secret_key'


reservations = conn.get_all_instances()
for res in reservations:
    for inst in res.instances:
            print "%s " % (inst.tags['Name'])
            print "%s  %s " % ("publicDnsName:", inst.public_dns_name)
            print "%s  %s " % ("internalDnsName:", inst.private_dns_name)
            print "%s  %s " % ("publicIP:", inst.ip_address)
            print "%s  %s " % ("internalIP:", inst.private_ip_address)
            print "%s  %s " % ("architecture:", inst.architecture)
            print "%s  %s " % ("image_id:", inst.image_id)
            print "%s    %s " % ("instance_type", inst.instance_type)
            print ""

###################################################



import boto.ec2

conn=boto.ec2.connect_to_region('us-east-1')

reservations = conn.get_all_instances()
for res in reservations:
    for inst in res.instances:
            print "%s " % (inst.tags['Name'])
            print "%s  %s " % ("publicDnsName:", inst.public_dns_name)
            print "%s  %s " % ("internalDnsName:", inst.private_dns_name)
            print "%s  %s " % ("publicIP:", inst.ip_address)
            print "%s  %s " % ("internalIP:", inst.private_ip_address)
            print "%s  %s " % ("architecture:", inst.architecture)
            print "%s  %s " % ("image_id:", inst.image_id)
            print "%s    %s " % ("instance_type", inst.instance_type)
            print ""



##### To find all the details #####

import boto.ec2

conn=boto.ec2.connect_to_region('us-east-1')

reservations = conn.get_all_instances()
for res in reservations:
    for inst in res.instances:
        print(inst.__dict__)
        break # remove this to list all instances


### Output: ###

{'_in_monitoring_element': False,
 'ami_launch_index': u'0',
 'architecture': u'x86_64',
 'block_device_mapping': {},
 'connection': EC2Connection:ec2.amazonaws.com,
 'dns_name': u'ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com',
 'id': u'i-xxxxxxxx',
 'image_id': u'ami-xxxxxxxx',
 'instanceState': u'\n                    ',
 'instance_class': None,
 'instance_type': u'm1.large',
 'ip_address': u'xxx.xxx.xxx.xxx',
 'item': u'\n                ',
 'kernel': None,
 'key_name': u'FARM-xxxx',
 'launch_time': u'2009-10-27T17:10:22.000Z',
 'monitored': False,
 'monitoring': u'\n                    ',
 'persistent': False,
 'placement': u'us-east-1d',
 'previous_state': None,
 'private_dns_name': u'ip-10-xxx-xxx-xxx.ec2.internal',
 'private_ip_address': u'10.xxx.xxx.xxx',
 'product_codes': [],
 'public_dns_name': u'ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com',
 'ramdisk': None,
 'reason': '',
 'region': RegionInfo:us-east-1,
 'requester_id': None,
 'rootDeviceType': u'instance-store',
 'root_device_name': None,
 'shutdown_state': None,
 'spot_instance_request_id': None,
 'state': u'running',
 'state_code': 16,
 'subnet_id': None,
 'vpc_id': None}

NOTE: You can use above any of this sub value to get only those information.

Wednesday, 26 March 2014

Auto monitoring of aws instance using python boto

Over here, we can discuss about " monitoring automation ", a generic way: which will work for any one who is using "AWS" "Nagios" for monitoring.

Features:
[1]: automatically add the new host into monitoring, when ever we add a new system into the aws system.
[2]: automatically will remove the system from monitoring if we terminate the system from aws system.
[3]: read group information from custom tags.

NOTE: As we are going auto monitoring, few rule we have to maintain, else the monitoring will fail.

Rule1: We can have only two tags to any of our aws instance. [1. default: Name, 2. groups ] NOTE, these are case sensitive, so please maintain the same.

Rule2: As of now we have the following key words that can be part of the groups custom tags: [Note: if  you need new, you have to let me know before putting the value. This is also case sensitive ] [ you can update the nagios hostgroup config file to add new hostgroup, before adding them into groups custom tags.]

        hostgroup_name      hadoop
        hostgroup_name      db
        hostgroup_name      http


Following is the python boto script:

#!/usr/bin/env python
import boto.ec2
import subprocess
#import os, subprocess

conn=boto.ec2.connect_to_region('us-east-1')

reservations = conn.get_all_instances()
for res in reservations:
    for inst in res.instances:
            print ("define host{")
            print "%s \t %s" % ("use","generic-host") # \t for tab
            print "%s  %s" % ("host_name", inst.tags['Name'])
            if inst.tags['Name'] == 'qa1':
                print "%s \t%s" % ("check_command", "check_ssh")
                # different check for qa1 as it is fedora system.
            print "%s \t %s: %s" % ("alias", inst.tags['Name'], inst.public_dns_name)          
            print "%s  %s" % ("address", inst.private_ip_address)
            # Swapped the alias and address value, because of cost effective)
            ## Following few code block will check for a custom tags knonw as groups
            ## if its find the groups, then that host will be part of those hosts.
            alltags = (inst.tags) # Will get all the other tags.
            alltagsC = str(alltags) # changing the variable type to string.
            isgroup = (alltagsC.find('groups'))
            if isgroup > 0:
                sp = isgroup+11 #found the groups index value and picking the other groups
                #global otherGroups
                otherGroups = alltagsC[sp:-2]
                #print  "%s  %s  %s" % ("hostgroups", inst.instance_type, otherGroups)
                print "%s  %s" % ("hostgroups", otherGroups)
            #else:
                #print "%s %s" % ("hostgroups", inst.instance_type)
            print ("}\n")

NOTE: As of now I don't know how to get the custom tags value so did some hacks.
NOTE: Removing instance type as part of group, because the monitor will fail, if we have define any group with a instance type and no host is part of that group.


And put the following script into a file and put the file under root crontab:

#!/bin/bash
sudo /path/to/getInstanceDetails.py > /path/to/all_hosts.cfg
sleep 2
sudo service nagios3 restart

##Added this above script in cron as root user: sudo crontab -e
## */15 * * * * sudo /path/to/aboveScrptName.sh


## Now where I will update, what to check where ##

define service{
        hostgroup_name                  db ;<-NOTE: over here you just have to put hostgroup.
        service_description             MYSQL
        check_command                   check_nrpe_1arg!check_mysql
        use                             generic-service-after-15 ; Name of service template to use
        notification_interval           0 ; set > 0 if you want to be renotified
}

NOTE: you can create generic-service-xxx names with its own properties and add them over here.

Tuesday, 25 March 2014

aws hosts information for auto monitoring using python boto 1

import boto.ec2

conn=boto.ec2.connect_to_region('us-east-1')

reservations = conn.get_all_instances()
for res in reservations:
    for inst in res.instances:
        if 'Name' in inst.tags:
            print ("define host{")
            print ("use\tgeneric-host") # \t for tab
            print "%s  %s " % ("host_name", inst.tags['Name'])
            #print "%s "% (inst.tags['Name'])
            print "%s \t %s " % ("alias", inst.tags['Name'])
            print "%s  %s " % ("address", inst.public_dns_name)
            print(inst.instance_type)
            print ("}\n")
            #print "%s (%s) [%s] [%s]" % (inst.tags['Name'], inst.id, inst.state, inst.public_dns_name)
        else:
            print "%s [%s]" % (inst.id, inst.state)


######NOTE#########

I believe in the following script output you can see the output name " example: instance_type " so you can use the same for getting that information:

example: for my above script:
inst.public_dns_name will give public_dns_name
inst.region will give the region details and so..on

NOTE: To get the details of the instance that is part of a custom tags then:
reservations = conn.get_all_instances(filters={'tag-key': 'groups'})


############ external script ##############

from pprint import pprint
from boto import ec2

AWS_ACCESS_KEY_ID = 'XXXXXXXXXXXXXXXXXX'
AWS_SECRET_ACCESS_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

ec2conn = ec2.connection.EC2Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
reservations = ec2conn.get_all_instances()
instances = [i for r in reservations for i in r.instances]
for i in instances:
    pprint(i.__dict__)
    break # remove this to list all instances 
 
 
{'_in_monitoring_element': False,
 'ami_launch_index': u'0',
 'architecture': u'x86_64',
 'block_device_mapping': {},
 'connection': EC2Connection:ec2.amazonaws.com,
 'dns_name': u'ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com',
 'id': u'i-xxxxxxxx',
 'image_id': u'ami-xxxxxxxx',
 'instanceState': u'\n                    ',
 'instance_class': None,
 'instance_type': u'm1.large',
 'ip_address': u'xxx.xxx.xxx.xxx',
 'item': u'\n                ',
 'kernel': None,
 'key_name': u'FARM-xxxx',
 'launch_time': u'2009-10-27T17:10:22.000Z',
 'monitored': False,
 'monitoring': u'\n                    ',
 'persistent': False,
 'placement': u'us-east-1d',
 'previous_state': None,
 'private_dns_name': u'ip-10-xxx-xxx-xxx.ec2.internal',
 'private_ip_address': u'10.xxx.xxx.xxx',
 'product_codes': [],
 'public_dns_name': u'ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com',
 'ramdisk': None,
 'reason': '',
 'region': RegionInfo:us-east-1,
 'requester_id': None,
 'rootDeviceType': u'instance-store',
 'root_device_name': None,
 'shutdown_state': None,
 'spot_instance_request_id': None,
 'state': u'running',
 'state_code': 16,
 'subnet_id': None,
 'vpc_id': None} 




External Links:
http://www.saltycrane.com/blog/2010/03/how-list-attributes-ec2-instance-python-and-boto/

Tuesday, 11 March 2014

how to find what all methods an object have in python, when you import that.


>>> import boto

>>> dir(boto)
Out[9]:
['BotoConfigLocations',
 'BucketStorageUri',
 'Config',
 'FileStorageUri',
 'InvalidUriError',
 'NullHandler',
 'UserAgent',
 'Version',
 '__builtins__',
 '__doc__',
 '__file__',
 '__name__',
 '__package__',
 '__path__',
 '__version__',
 '_aws_cache',
 '_get_aws_conn',
 'boto',
 'check_extensions',
 'config',
 'connect_autoscale',
 'connect_cloudformation',
 'connect_cloudfront',
 'connect_cloudwatch',
 'connect_dynamodb',
 'connect_ec2',
 'connect_ec2_endpoint',
 'connect_elb',
 'connect_emr',
 'connect_euca',
 'connect_fps',
 'connect_gs',
 'connect_ia',
 'connect_iam',
 'connect_mturk',
 'connect_rds',
 'connect_route53',
 'connect_s3',
 'connect_sdb',
 'connect_ses',
 'connect_sns',
 'connect_sqs',
 'connect_sts',
 'connect_swf',
 'connect_vpc',
 'connect_walrus',
 'exception',
 'handler',
 'init_logging',
 'log',
 'logging',
 'lookup',
 'os',
 'plugin',
 'pyami',
 're',
 'resultset',
 'set_file_logger',
 'set_stream_logger',
 'storage_uri',
 'storage_uri_for_key',
 'sys',
 'urlparse']


>>> hasattr(boto,"ec2_connect")
 False

>>> hasattr(boto,"connect_ec2")
 True

>> help(boto.connect_ec2)

connect_ec2(aws_access_key_id=None, aws_secret_access_key=None, **kwargs)
    :type aws_access_key_id: string
    :param aws_access_key_id: Your AWS Access Key ID
    
    :type aws_secret_access_key: string
    :param aws_secret_access_key: Your AWS Secret Access Key
    
    :rtype: :class:`boto.ec2.connection.EC2Connection`
    :return: A connection to Amazon's EC2
(END)


link:
http://www.diveintopython.net/power_of_introspection/index.html
http://stackoverflow.com/questions/34439/finding-what-methods-an-object-has
http://en.wikipedia.org/wiki/Python_%28programming_language%29

Monday, 10 March 2014

python script with system command 1


NOTE: sudo apt-get install ipython [ To install ipython ]

#!/usr/bin/env python
# System Information Gethering Script
import subprocess

# Example:
# subprocess.call(["ls","-l","/tmp/"])
# Example: You can also use as following for the above command:
# subprocess.call("df -h", shell=True)

#Command 1
uname = "uname"
uname_arg = "-a"
print "Gethering system information with %s command:\n" %uname
subprocess.call([uname,uname_arg])

#Command 2
diskspace = "df"
diskspace_arg = "-h"
print "Gathering diskspace information %s command:\n" %diskspace
subprocess.call([diskspace,diskspace_arg])

Tuesday, 25 February 2014

aws python api with boto note1

* How to install boto:
1. Need python.
2. download boto [ https://github.com/boto/boto/downloads/ ]
3. Install boto:
2a. tar xfz boto-2.1.tar.gz
2b. cd boto-2.1
2c. sudo python setup.py install

other way:
1. sudo apt-get install python-setuptools
2. sudo easy_install boto

NOTE: Some time few of the boto function don't work, e:g: boto 1.9 don't have instance.tags support, in that case you have to update the boto.

How to update boto: [ I am using easy_install: part of "python-setuptools: "
easy_install -U boto

To see your current boto version:
$python
>>> import boto
>>> boto.Version
'2.27.0'



example of setting the /etc/boto.cfg file:

[Credentials]
aws_access_key_id = ABCDEFGHIJK
aws_secret_access_key = ABCDEFASDFAS123ASF123


########### Example #########
import boto.ec2

# NOTE: if you don't put the key at conn then it will search for
# your profile file, if not /etc/boto.cfg if not your varible 'BOTO_CONFIG=/file/path'

conn=boto.ec2.connect_to_region('us-east-1')
#conn=boto.ec2.connect_to_region('us-east-1',aws_access_key_id='access_key' , aws_secret_access_key='secret_key')

# Following is the prod key: [ Full access ]
#AWS_ACCESS_KEY_ID = 'access_key'
#AWS_SECRET_ACCESS_KEY = 'secret_key'


reservations = conn.get_all_instances()
for res in reservations:
    for inst in res.instances:
            print "%s " % (inst.tags['Name'])
            print "%s  %s " % ("publicDnsName:", inst.public_dns_name)
            print "%s  %s " % ("internalDnsName:", inst.private_dns_name)
            print "%s  %s " % ("publicIP:", inst.ip_address)
            print "%s  %s " % ("internalIP:", inst.private_ip_address)
            print "%s  %s " % ("architecture:", inst.architecture)
            print "%s  %s " % ("image_id:", inst.image_id)
            print "%s    %s " % ("instance_type", inst.instance_type)
            print ""


########## ###################### ################

$ to see help with in boto:
>>>help(boto)

>>> dir(boto)
['BUCKET_NAME_RE', 'BotoConfigLocations', 'BucketStorageUri', 'Config', 'ENDPOINTS_PATH', 'FileStorageUri', 'GENERATION_RE', 'InvalidUriError', 'NullHandler', 'TOO_LONG_DNS_NAME_COMP', 'UserAgent', 'VERSION_RE', 'Version', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', 'auth', 'auth_handler', 'boto', 'cacerts', 'compat', 'config', 'connect_autoscale', 'connect_beanstalk', 'connect_cloudformation', 'connect_cloudfront', 'connect_cloudsearch', 'connect_cloudtrail', 'connect_cloudwatch', 'connect_directconnect', 'connect_dynamodb', 'connect_ec2', 'connect_ec2_endpoint', 'connect_elastictranscoder', 'connect_elb', 'connect_emr', 'connect_euca', 'connect_fps', 'connect_glacier', 'connect_gs', 'connect_ia', 'connect_iam', 'connect_kinesis', 'connect_mturk', 'connect_opsworks', 'connect_rds', 'connect_rds2', 'connect_redshift', 'connect_route53', 'connect_s3', 'connect_sdb', 'connect_ses', 'connect_sns', 'connect_sqs', 'connect_sts', 'connect_support', 'connect_swf', 'connect_vpc', 'connect_walrus', 'connection', 'datetime', 'ec2', 'exception', 'gs', 'handler', 'https_connection', 'init_logging', 'log', 'logging', 'os', 'perflog', 'platform', 'plugin', 'provider', 'pyami', 're', 'regioninfo', 'resultset', 's3', 'set_file_logger', 'set_stream_logger', 'storage_uri', 'storage_uri_for_key', 'sys', 'urlparse', 'utils']

>>> dir(boto.connect_ec2)
['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']

>>> dir(boto.connect_ec2.__get__)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__name__', '__new__', '__objclass__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

>>> dir(boto.ec2)
['EC2Connection', 'RegionData', 'RegionInfo', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'address', 'attributes', 'blockdevicemapping', 'bundleinstance', 'connect_to_region', 'connection', 'ec2object', 'get_region', 'get_regions', 'group', 'image', 'instance', 'instanceinfo', 'instancestatus', 'instancetype', 'keypair', 'launchspecification', 'load_regions', 'networkinterface', 'placementgroup', 'regioninfo', 'regions', 'reservedinstance', 'securitygroup', 'snapshot', 'spotdatafeedsubscription', 'spotinstancerequest', 'spotpricehistory', 'tag', 'volume', 'volumestatus', 'zone']

>>> dir(boto.ec2.connect_to_region)
['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']


* Installing easy_install to install rest of the python package.
1.  sudo apt-get install python-setuptools
2. sudo easy_install paramiko [ ssh2 like tool. ]
3. sudo apt-get install -y euca2ools [ same as aws command line tools. ]

* aws credentials for boto:
1. at the code itself:  [example: ]
>>> import boto
>>> ec2 = boto.connect_ec2(aws_access_key_id='my_access_key',
aws_secret_access_key='my_secret_key')

2. at the system variable: as:
~/.bashrc:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY

3. Boto configuration files:
3a. ~/.boto
3b. /etc/boto.cfg
3c. set the varible 'BOTO_CONFIG=/file/path'
with following format:
[Credentials]
aws_access_key_id = your_access_key
aws_secret_access_key = your_secret_key

##### to test the above settings: ######
##### the /etc/boto.cfg file should have the read access for the Credentials ]
>>> import boto
>>> ec2 = boto.connect_ec2()
>>> ec2.get_all_zones()
[Zone:us-east-1a, Zone:us-east-1b, Zone:us-east-1c, Zone:us-east-1d, Zone:us-east-1e]

>>> ec3 = boto.connect_ec2()
>>> ec3.get_all_zones()
[Zone:us-east-1a, Zone:us-east-1b, Zone:us-east-1c, Zone:us-east-1d, Zone:us-east-1e]

>>> boto.ec2.regions()
[RegionInfo:eu-west-1, RegionInfo:sa-east-1, RegionInfo:us-east-1, RegionInfo:ap-northeast-1, RegionInfo:us-west-2, RegionInfo:us-west-1, RegionInfo:ap-southeast-1, RegionInfo:ap-southeast-2]

default region for boto is "us-east-1"


If you want to connect to a different regions, then:
import boto.ec2
ec_conn = boto.ec2.connect_to_region('eu-west-1')
>>> ec_conn.get_all_zones()
[Zone:eu-west-1a, Zone:eu-west-1b, Zone:eu-west-1c]

NOTE: If you want by default to connect to some other region then you can update your boto config file as:
[Boto]
ec2_region_name = eu-west-1


Q) If I have multiple aws account, then ?:
I can think of set the variable BOTO_CONFIG = /path/to/file/account1 [ on boto scripts. ]


############
[1] Following is one of the python code to get few of the param of the instances:

import boto.ec2
conn=boto.ec2.connect_to_region('us-east-1', aws_access_key_id='<access_key>', aws_secret_access_key='<secret key>')
reservations = conn.get_all_instances()
for res in reservations:
    for inst in res.instances:
        if 'Name' in inst.tags:

            print "%s (%s) [%s]" % (inst.tags['Name'], inst.id, inst.state)
        else:
            print "%s [%s]" % (inst.id, inst.state)



[2] In following example, the script is accessing the variable details from the profile file to get the access and the secret key:

import boto.ec2
conn=boto.ec2.connect_to_region("us-east-1")
reservations = conn.get_all_instances()
for res in reservations:
    for inst in res.instances:
        if 'Name' in inst.tags:

            print "%s (%s) [%s]" % (inst.tags['Name'], inst.id, inst.state)
        else:
            print "%s [%s]" % (inst.id, inst.state)