Python2.x利用commands模块执行Linux shell命令
用Python写运维脚本时,经常需要执行linuxshell的命令,Python中的commands模块专门用于调用Linuxshell命令,并返回状态和结果,下面是commands模块的3个主要函数:
1.commands.getoutput('shellcommand')
执行shell命令,返回结果(string类型)
>>>commands.getoutput('pwd') '/home/oracle'
2.commands.getstatus('file')
该函数已被python丢弃,不建议使用,它返回ls-ldfile的结果(String)(返回结果太奇怪了,难怪被丢弃)
>>>commands.getstatus('admin.tar') '-rw-rw-r--1oracleoracle829440Jan2910:36admin.tar'
3.commands.getstatusoutput('shellcommand')
执行shell命令,返回两个元素的元组tuple(status,result),status为int类型,result为string类型。
cmd的执行方式是{cmd;}2>&1,故返回结果包含标准输出和标准错误.
>>>commands.getstatusoutput('pwd') (0,'/home/oracle')
下面的一个脚本利用commands模块检测磁盘使用率,标识出大于10%的磁盘(百分比可根据实际情况调整,一般设为90%,本例为了更好的说明情况,设为10%):
importcommands threshold=10 flag=False title=commands.getoutput("df-h|head-1") ''' Checksdadiskspaceusagelikebelowformat: /dev/sda220G2.3G17G13%/ /dev/sda620G306M19G2%/var /dev/sda349G2.8G44G7%/home /dev/sda549G4.5G42G10%/opt /dev/sda1194M12M172M7%/boot ''' chkDiskList=commands.getoutput("df-h|grepsda").split('\n') usedPercents=commands.getoutput("df-h|grepsda|awk'{print$5}'|grep-Eo'[0-9]+'").split('\n') foriinrange(0,len(usedPercents)): ifint(usedPercents[i])>=threshold: chkDiskList[i]+='----Caution!!!spaceusage>='+str(threshold) flag=True ''' Checkdiskspaceusagelikebelowformat: /dev/mapper/backup-backup_lv 751G14G699G2%/backup /dev/mapper/data-data_lv 751G172G540G25%/data ''' chkDiskList_2=commands.getoutput("df-h|grep-vsda|grep-vtmp|grep-vsystem").split('\n') usedPercents_2=commands.getoutput("df-h|grep-vmap|grep-vsda|grep-vtmp|grep-vsystem|awk'{print$4}'|grep-Eo'[0-9]+'").split('\n') foriinrange(0,len(usedPercents_2)): ifint(usedPercents_2[i])>=threshold: chkDiskList_2[i*2+1]+='----Caution!!!spaceusage>='+str(threshold) flag=True ifflag==True: #combinetile,chkDiskList,chkDisklist_2 result=[title,] result.extend(chkDiskList) result.extend(chkDiskList_2) forlineinresult: printline
假设当前的磁盘使用率如下:
[oracle@lx200~/admin/python]$df-h FilesystemSizeUsedAvailUse%Mountedon /dev/sda220G2.3G17G13%/ /dev/sda620G306M19G2%/var /dev/sda349G2.8G44G7%/home /dev/sda549G4.5G42G10%/opt /dev/sda1194M12M172M7%/boot tmpfs18G018G0%/dev/shm /dev/mapper/backup-backup_lv 751G14G699G2%/backup /dev/mapper/data-data_lv 751G174G539G25%/data
执行该脚本后的结果如下:
FilesystemSizeUsedAvailUse%Mountedon /dev/sda220G2.3G17G13%/----Caution!!!spaceusage>=10 /dev/sda620G306M19G2%/var /dev/sda349G2.8G44G7%/home /dev/sda549G4.5G42G10%/opt----Caution!!!spaceusage>=10 /dev/sda1194M12M172M7%/boot /dev/mapper/backup-backup_lv 751G14G699G2%/backup /dev/mapper/data-data_lv 751G174G539G25%/data----Caution!!!spaceusage>=10
pythonCommands模块使用方法
要获得shell命令的输出只需要`cmd`就可以了,
需要得到命令执行的状态则需要判断$?的值,在Python中有一个模块commands也很容易做到以上的效果.
看一下三个函数:
1).commands.getstatusoutput(cmd)
用os.popen()执行命令cmd,然后返回两个元素的元组(status,result).cmd执行的方式是{cmd;}2>&1,这样返回结果里面就会包含标准输出和标准错误.
2).commands.getoutput(cmd)
只返回执行的结果,忽略返回值.
3).commands.getstatus(file)
返回ls-ldfile执行的结果.
看一下这些函数使用的例子:
>>>importcommands >>>commands.getstatusoutput('ls/bin/ls') (0,'/bin/ls') >>>commands.getstatusoutput('cat/bin/junk') (256,'cat:/bin/junk:Nosuchfileordirectory') >>>commands.getstatusoutput('/bin/junk') (256,'sh:/bin/junk:notfound') >>>commands.getoutput('ls/bin/ls') '/bin/ls' >>>commands.getstatus('/bin/ls') '-rwxr-xr-x1root13352Oct141994/bin/ls'