[Python] executes Linux shell commands with the commands module

Source: Internet
Author: User
Tags linux shell commands disk usage

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)

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

>>> 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.

>>> commands.getstatusoutput (' pwd ') ('/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 3 06M 19G 2%/var/dev/sda3 49G 2.8G 44G 7%/home/dev/sda5 49G 4.5G 42G 10%/opt/dev/s da1 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 IIn 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_lv 751G 14G 699G 2%/backup/dev/mapper/data-data_lv 751G 172G 540G 25%/data "' 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 ') forI in range (0,len (usedpercents_2)): if int (usedpercents_2[i]) >= threshold:chkdisklist_2[i*2 + 1] + = '----Caution!!! space usage >= ' + str (threshold) flag = T RueIf flag = = True: #combine Tile, chkdisklist, chkdisklist_2 result = [title,] Result.extend (chkdisklist) Result.extend (chkdisklist_2) for line in  result: print       Line 

Assume that the current disk utilization is as follows:
[Email protected] ~/admin/python]$ Df-hfilesystem Size used AvailUse% Mounted On/dev/sda220G2.3G17G13%//dev/sda620G306M19g 2%/var/dev/sda3 49g Span class= "number" >2.8g 44g 7%/home/dev/sda5 49g Span class= "number" >4.5g 42g 10%/opt/dev/sda1 194m Span class= "number" >12m 172m 7%/boottmpfs 18g 0 18g 0%/dev/shm/dev/mapper/backup-backup_lv 751g 14g 699g 2%/backup/dev/mapper/ DATA-DATA_LV 751g 174g 539g 25 %/data                

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    ----Caution!!! Space usage >= 10/dev/sda1             194M   12M  172M   7%/boot/dev/mapper/backup-backup_lv                      751G   14G  699G   2%/ Backup/dev/mapper/data-data_lv                      751G  174G  539G  25%/data    ----Caution!!! Space usage >= 10



Transferred from: http://www.cnblogs.com/huanxiyun/articles/5826088.html

[Python] executes Linux shell commands with the commands module

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.