Dec 09 2016

Check Service Status With Python on OEL 7 / Centos 7

Category: Linux & Unix,PythonFatih Acar @ 21:42

I write a python code to check mysql service open or close. It is simple and you can change service_name parameter in the code to check another services. You can download from here check_service_status.py Below code maybe can not run because of python language format is tab structure. You can use with download original code check_service_status.py

Check MySQL Service Status


#-*- coding: utf-8 -*-

import os
import commands

service_name = "mysql"

def check_service_status(service_name):

        status = os.system('systemctl status '+service_name+ ' > /dev/null')
        return status

def main():

        if (check_service_status(service_name) == 0):
                print "Running"
        else:
                print "Stopped"
main()

Download

Execute Script
Continue reading “Check Service Status With Python on OEL 7 / Centos 7”

11,012 total views, 6 views today

Tags: Linux Administration, Python, System Administration


Feb 28 2013

Python Commands and Functions

Category: PythonFatih Acar @ 16:50

Take value from user.

input(“Enter a number : “)
raw_input(“Enter a character : “)

Counters

range(100) # write from 0 to 100
range(100,200) # write from 100 to 200
range(100,200,2) # write from 100 to 200 as 2 by 2
len(“Fatih”) # character count result 5

File and Directory Operations

listdir(“/home”/) # list all file or directory in /home/ directory

a=os.listdir(“/home/”)
for files in a:
print files

getcwd() # show current directory
chdir(“/var/lib”) # change directory
mkdir(“test”) # create directory
makedirs(“/test/test1/test2”) # create nested directories
rmdir(“test”) # remove directory
removedirs(“/test/test1/test2”) # remove directories
open(“test.txt”,”w”) # create file and delete all exist values and open with write mode
open(“test.txt”,”a”) # open exist file with append mode
open(“test.txt”,”r”) # open exist file with read mode

file_a.open(“test.txt”,”w”)
file_a.write(“Hello World with Python !”)
file_a.close()

file_a.open(“test.txt”,”r”)
file_a.read()
file_a.readlines() # read line by line

remove(“test.txt”) # delete file
rename(“test.txt”,”test1234.txt”) # rename file
str(a) # convert to string
int(a) # convert to integer

31,142 total views, no views today

Tags: Linux Administration, Programming, Python, System Administration


Feb 08 2013

Mail Send With Python

Category: Linux & Unix,PythonFatih Acar @ 11:39

Mail Send Function | File name : sender.py

import string
import smtplib
from email.mime.text import MIMEText
def mailSend(subject,content):

mailserver = “192.168.5.2” # Mail Server IP Address

mail = MIMEText(content)

mail[“From”] = “fromaddress@example.com”

mail[“To”] = “toaddress@example.com”

mail[“Subject”] = subject

gonder = smtplib.SMTP(mailserver)

gonder.sendmail(mailfrom,mailto,mail.as_string())

gonder.quit()

Notification Example for Disk Status Check | File name : diskcheck.py

from sender import *
import os
import commands
def main():

subject = “Nodes Disk Status”

content = “Node 1” + ‘\n\n’

query = “df -h”

query = commands.getoutput(query)

query = query.split(‘\n’)

for answer in query:

content = content + answer + ‘\n’

query = “df -i”

query = commands.getoutput(query)

query = query.split(‘\n’)

content = content + ‘\n’

for answer in query:

content = content + answer+ ‘\n’

query = “ssh node2 df -h”

query = commands.getoutput(query)

query = query.split(‘\n’)

content = content + ‘\n\n’ “Node 2” + ‘\n\n’

for answer in query:

content = content + answer + ‘\n’

query = “ssh node2 df -i”

query = commands.getoutput(query)

query = query.split(‘\n’)

content = content + ‘\n’

for answer in query:

content = content + answer + ‘\n’

mailSend(subject,content)
main()

11,559 total views, 5 views today

Tags: Python, System Administration


Jan 29 2013

Python Programing Language Structure

Category: PythonFatih Acar @ 20:48

Python supports multiple programming paradigms, including object-oriented, imperative and functional programming styles. It features a fully dynamic type system and automatic memory management, similar to that of Scheme, Ruby, Perl, and Tcl. Like other dynamic languages, Python is often used as a scripting language, but is also used in a wide range of non-scripting contexts. Using third-party tools, Python code can be packaged into standalone executable programs. Python interpreters are available for many operating systems.

If you are a database or a system admin, you can use python language for any script works. Python allows that writes quickly scripts.

Python-Programming-Language

Basic Python Code Structure, File Name : test.py

#!/usr/bin/env python
#-*- coding: utf-8 -*-

import os #This choosing allows that you can run os module’s functions with writing os.
from os import * #This choosing allows that you can run os module’s functions without writing os.

def test_function():

print “Hello World.. :)”

def main():

try:

test_funtion()

except NameError:

print “Can not write any text”

14,140 total views, no views today

Tags: Python, System Administration