Thursday, July 2, 2015







Write a piece of Python code that prints out one of the following messages:
  • "string involved" if either varA or varB are strings
  • "bigger" if varA is larger than varB
  • "equal" if varA is equal to varB
  • "smaller" if varA is smaller than varB

    if type(varA) == str or type(varB) == str:
        print 'string involved'
    elif varA > varB:
        print 'bigger'
    elif varA == varB:
        print 'equal'
    else:
        # If none of the above conditions are true,
        # it must be the case that varA < varB
        print 'smaller'
     

Thursday, June 25, 2015

python print without newline suppress newline

#Only for use in Python 2.6.0a2 and later
from __future__ import print_function
This allows you to use the Python 3.0 style print function without having to hand-edit all occurrences of print :)
On python 2.x 
simply add   ","  i,e   print  "var1",
>>> for i in range(11):
...     print i,
...
0 1 2 3 4 5 6 7 8 9 10
print() is function in python 3.x
print "xxxx" is statement in python 2.x

Tuesday, June 23, 2015

bash script summary

echo -e "\nServer :" `uptime`; echo "Apache :" `service httpd status | grep -i uptime`; echo "MySQL  :" `echo "status;" | mysql | grep -i uptime`;echo -e "\t" `mysqladmin status`;echo -e "\nType\t Used\t\t Percent"; df -h | sed -n '2,2p' | awk '{print "Disk:\t",$3,"\t\t",$5}';df -i | sed -n '2,2p' | awk '{print "Inodes:\t",$3,"\t",$5,"\n"}';netstat -plan |awk '/.*[0-9]+.[0-9]+.[0-9]+.[0-9].*/{gsub(/::ffff:/,"",$0);print $4"\t" $5}'|cut -sd. -f 1->netstat.log;echo "Netstat report";echo;echo "Number of Connections to each port:";cat netstat.log |awk {'print $1'}|cut -d: -f 2|sort|uniq -c|sort -nk 1|tail;echo;echo "Number of connections from each IP:";cat netstat.log |awk {'print $2'}|cut -d: -f 1|sort|uniq -c|sort -nk 1|tail;echo;echo "The number of instances of a particular IP connecting to particular port";cat netstat.log |awk {'print $1 "\t" $2'}|cut -d: -f 2|sort|uniq -c|sort -nk 1|tail;

Friday, April 3, 2015

python3 send email for high system load average

#!/usr/bin/python3

import os

import smtplib

from email.mime.text import MIMEText

from email.mime.multipart import MIMEMultipart

loadout = os.getloadavg()  #this returns a tuple (0.34, 0.1, 0.07)

if loadout[1] > 0:

    msg = MIMEMultipart()  #msg is instance obj of mimemultipart

    msg['From'] = "nagios@localhost"  # this is seen as from: in receiving end

    msg['To'] = "sysadmin@gmail.com"  #put any thing here

    msg['Subject']="System load warning !!"

    body = str(loadout)

    msg.attach(MIMEText(body, 'plain'))

    fromaddr="pythoncheckload@localhost"    #returnpath

    toaddr="urgen@gmail.com"    #actual receiving address

    server = smtplib.SMTP('localhost')

    server.set_debuglevel(True)  #for verbose output while testing

    text = msg.as_string()

    server.sendmail(fromaddr,toaddr,text)

   #server.sendmail(fromaddr,[toaddr],text)

    server.quit()

Sunday, January 25, 2015

mysqlbackup py 3

root@server:~/hhscpy# cat sqlbackup.py
#!/usr/bin/python
backup_dest = '/root/sqlbackup/'
import os
import time
import datetime
dbhost = 'localhost'
dbuser = 'root'
dbpass = 'hotti@90'
datetime = time.strftime("%Y-%m-%d_%H-%M-%S")
backuppathwithDT = backup_dest + datetime
multi=2
#create directory if it does not exist
if not os.path.exists(backuppathwithDT):
    os.makedirs(backuppathwithDT, 0500)
print "Checking for db names in file"
print "starting backup of databases.. "
#Starting backup
if multi:
        dumpcmd = "mysqldump -u " + dbuser + " -p" + dbpass + " --all-databases --events " + " > " + backuppathwithDT + "/completedb.sql"
        os.system(dumpcmd)
else:
    print "no database to dump"
print "Backup complete..."
root@server:~/hhscpy# cat sqlbackup.py
#!/usr/bin/python
backup_dest = '/root/sqlbackup/'
import os
import time
import datetime
dbhost = 'localhost'
dbuser = 'root'
dbpass = 'p@$$word'
datetime = time.strftime("%Y-%m-%d_%H-%M-%S")
backuppathwithDT = backup_dest + datetime
multi=2
#create directory if it does not exist
if not os.path.exists(backuppathwithDT):
    os.makedirs(backuppathwithDT, 0500)
print "Checking for db names in file"
print "starting backup of databases.. "
#Starting backup
if multi:
        dumpcmd = "mysqldump -u " + dbuser + " -p" + dbpass + " --all-databases --events " + " > " + backuppathwithDT + "/completedb.sql"
        os.system(dumpcmd)
else:
    print "no database to dump"
print "Backup complete..."

python mysqlbackup scirpt 2

root@vm1:~/pyscript# cat back.py.back
#!/usr/bin/python
db_name_file = '/root/pyscript/dbname.txt'
backup_dest = '/root/pyscript/sqlbackup/'
import os
import time
import datetime
dbhost = 'localhost'
dbuser = 'root'
dbpass = 'redhat'
datetime = time.strftime("%Y-%m-%d_%H-%M-%S")
backuppathwithDT = backup_dest + datetime
#create directory if it does not exist
if not os.path.exists(backuppathwithDT):
    os.makedirs(backuppathwithDT, 0500)
print "Checking for db names in file"
if os.path.exists(db_name_file):
    file1 = open(db_name_file)
    multi = 1
    print "Database names found"
    print "starting backup of databases.. "
else:
    print "Database file not found in file"
#Starting backup
if multi:
        dumpcmd = "mysqldump -u " + dbuser + " -p" + dbpass + " --all-databases --events " + " > " + backuppathwithDT + "/completedb.sql"
        os.system(dumpcmd)
 
else:
    print "no database to dump"
print "Exiting..."

python mysql backup script 1

root@vm1:~/pyscript# cat back.py
#!/usr/bin/python
#Python 2.7.6
db_name_file = '/root/pyscript/dbname.txt'
backup_dest = '/root/pyscript/sqlbackup/'
import os
import time
import datetime
dbhost = 'localhost'
dbuser = 'root'
dbpass = 'redhat'
datetime = time.strftime("%Y-%m-%d_%H-%M-%S")
backuppathwithDT = backup_dest + datetime
#create directory if it does not exist
if not os.path.exists(backuppathwithDT):
    os.makedirs(backuppathwithDT)
print "Checking for db names in file"
if os.path.exists(db_name_file):
    file1 = open(db_name_file)
    multi = 1
    print "Database names found"
    print "starting backup of databases.. "
else:
    print "Database file not found in file"
    multi = 0

#Starting backup
if multi:
    in_file = open(db_name_file,"r")
    flength = len(in_file.readlines())  ##in_file.readlines() returns a list
    in_file.close()
    p = 1
    dbfile = open(db_name_file,"r")
   
    while p <= flength:
        db = dbfile.readline()
        db = db[:-1]  #deletes extra line
        dumpcmd = "mysqldump -u " + dbuser + " -p" + dbpass + " " + db + " > " + backuppathwithDT + "/" + db + ".sql"
        os.system(dumpcmd)
        p = p + 1
    dbfile.close()
else:
    print "no database to dump"
print "Exiting..."