python2.x using the Commands module to execute Linux shell commands

Source: Internet
Author: User
Tags linux shell commands
When writing operations scripts in Python, it is often necessary to execute the commands of the Linux shell, the commands module in Python is dedicated to invoking the Linux shell command and returning the status and results, here are the 3 main functions of the commands module:

1. Commands.getoutput (' Shell command ')

Execute shell command, return result (String type)

Copy the Code code as follows:


>>> commands.getoutput (' pwd ')
'/home/oracle '

2. Commands.getstatus (' file ')

This function has been discarded by Python and is not recommended, it returns the result of the Ls-ld file (String) (It is too strange to return the result, no wonder it was discarded)

Copy the Code code as follows:


>>> commands.getstatus (' Admin.tar ')
'-rw-rw-r--1 Oracle Oracle 829440 Jan 10:36 Admin.tar '

3. Commands.getstatusoutput (' Shell command ')

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

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

Copy the Code code as follows:


>>> commands.getstatusoutput (' pwd ')
(0, '/home/oracle ')

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%):

Import Commandsthreshold = 10flag = Falsetitle=commands.getoutput ("df-h|head-1") "Check SDA disk space usage like below Format:/dev/sda2 20G 2.3G 17G 13%//dev/sda6 20G 306M 19G 2%/var/dev/sda3 49G 2.8G 44G 7%/home/dev/sda5 49G 4.5G 42G 10 %/opt/dev/sda1 194M 12M 172M 7%/Boot "' 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:/dev/mapper/backup-backup_lv751 G 14G 699G 2%/backup/dev/mapper/data-data_lv751g 172G 540G 25%/data "' Chkdisklist_2=commands.getoutput (" Df-h|grep-v s Da|grep-v tmp|grep-v System "). Split (' \ n ') usedpercents_2=commands.getoutput (" Df-h|grep-v map|grep-v sda|grep-v tmp|g Rep-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 = trueif Flag = = True: #combine tile, chkdisklist, chkdisklist_2result = [title,]r Esult.extend (chkdisklist) result.extend (chkdisklist_2) for line on Result:print line

Assume that the current disk utilization is as follows:

After executing the script, the results are as follows:

Filesystem Size used Avail use% mounted on/dev/sda2 20G 2.3G 17G 13%/----Caution!!! Space usage >= 10/dev/sda6 20G 306M 19G 2%/var/dev/sda3 49G 2.8G 44G 7%/home/dev/sda5 49G 4.5G 42G 10%/opt----Cauti On!!! Space usage >= 10/dev/sda1 194M 12M 172M 7%/boot/dev/mapper/backup-backup_lv751g 14G 699G 2%/backup/dev/mapper/data-d ata_lv751g 174G 539G 25%/data----Caution!!! Space usage >= 10

How to use Python commands modules

To get the output of the shell command you just need ' cmd ' to do it,
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 (CMD)

Executes command cmd with Os.popen () and returns the tuple (status, result) of two elements. CMD executes in the same way as {cmd;} 2>&1, so the return result contains standard output and standard errors.

2). Commands.getoutput (CMD)

Returns only the result of execution, ignoring the return value.

3). Commands.getstatus (file)

Returns the result of the Ls-ld file execution.

Take a look at the examples used by these functions:

>>> Import commands>>> commands.getstatusoutput (' ls/bin/ls ') (0, '/bin/ls ') >>> Commands.getstatusoutput (' Cat/bin/junk ') (in the ' cat:/bin/junk:no such file or directory ') >>> Commands.getstatusoutput ('/bin/junk ') (the "SH:/bin/junk:not found ') >>> commands.getoutput (' ls/bin/ls ') ' /bin/ls ' >>> commands.getstatus ('/bin/ls ') '-rwxr-xr-x 1 root 13352 Oct 1994/bin/ls '
  • 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.