Skip to content

Latest commit

 

History

History
25 lines (21 loc) · 402 Bytes

pythonyun_xing_bash.md

File metadata and controls

25 lines (21 loc) · 402 Bytes

Python运行bash

python 简单实现运行linux命令:

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

import time
import os
import sys
import shlex
import getpass
current_user=getpass.getuser()#获取当前用户
if current_user != "root":
 print "Error: You must be root to run this script"
 exit()

def bash(cmd):
  """
  执行bash命令
  """
  return shlex.os.system(cmd)
bash('top')