Python's Commands module

Source: Internet
Author: User
Tags disk usage

Commands module

To execute the Linux shell command, the output of the shell command needs to be written in the following parameters (' command ').

Need to get the status of the command execution needs to determine the value of $, in Python there is a module commands is also easy to achieve the above effect.


Take a look at the three functions:
1). Commands.getstatusoutput (command)

Executes the shell command, returns a tuple of two elements tuple (status, result), status int, and result is of type string.

The cmd command is executed as {cmd;} 2>&1, so the returned result contains standard output and standard error.

>>> Import Commands

>>> commands.getstatusoutput (' Ls/bin/ls ')

(0, '/bin/ls ')

>>> commands.getstatusoutput (' pwd ')

(0, '/home/test ')

>>> commands.getstatusoutput (' Cat/bin/junk ')

(/bin/junk:no, ' cat: Such file or directory ')

>>> commands.getstatusoutput ('/bin/junk ')

(/bin/junk:not, ' sh: Found ')


2). Commands.getoutput (CMD)
Returns only the result of execution, ignoring the return value.

>>> commands.getoutput (' Ls/bin/ls ')

'/bin/ls '


3). Commands.getstatus (file) #现已被弃用
Returns the result of the Ls-ld file execution.

>>> commands.getstatus ('/bin/ls ') #该函数已被python丢弃, it is not recommended, it returns the result of the Ls-ld file (String) (the result is too strange, no wonder it was discarded)

'-rwxr-xr-x 1 root 13352 Oct 1994/bin/ls '


Example 1:

Get System Maximum file descriptor

#!/usr/bin/python

Import Os,sys,commands

_open_file=65533

Try

Getulimit=commands.getstatusoutput (' Source/etc/profile;ulimit-n ')

Except Exception,e:

Pass

If getulimit[0]==0:

Host_open_file=int (Getulimit[1])

If Host_open_file = _open_file:

Print "Max_open_file is OK"


Example 2:

The following script uses the commands module to detect disk usage, identify disks larger than 10% (percentages can be adjusted according to the actual situation, generally set to 90%, this example to better illustrate the situation, set to 10%):


#!/usr/bin/python

Import commands

Threshold = 10

Flag = False

Title=commands.getoutput ("Df-h|head-1")

‘‘‘

Check SDA disk space usage like below format

‘‘‘

Chkdisklist=commands.getoutput ("Df-h|grep SDA"). Split (' \ n ')

Usedpercents=commands.getoutput ("Df-h|grep Sda|awk ' {print $} ' |grep-eo ' [0-9]+ '"). Split (' \ n ')

For I in range (0,len (usedpercents)):

if Int (usedpercents[i]) >= threshold:

Chkdisklist[i] + = '----Caution!!! Space usage >= ' + str (threshold)

Flag = True

‘‘‘

Check disk space usage like below format:

‘‘‘

Chkdisklist_2=commands.getoutput ("Df-h|grep-v sda|grep-v tmp|grep-v System"). Split (' \ n ')

Usedpercents_2=commands.getoutput ("Df-h|grep-v map|grep-v sda|grep-v tmp|grep-v system|awk ' {print $4} ' |grep-eo ' [0- 9]+ ' "). Split (' \ n ')

For I in range (0,len (usedpercents_2)):

if Int (usedpercents_2[i]) >= threshold:

Chkdisklist_2[i*2 + 1] + = '----Caution!!! Space usage >= ' + str (threshold)

Flag = True

If flag = = True:

#combine tile, Chkdisklist, chkdisklist_2

result = [Title,]

Result.extend (Chkdisklist)

Result.extend (chkdisklist_2)

For line in result:

Print Line



This article is from the "Ink" blog, please be sure to keep this source http://jinyudong.blog.51cto.com/10990408/1916433

Python's Commands module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.