File: //usr/share/lve/modlscapi/user/exec_command.py
#!/usr/bin/python2
# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import glob, os, shutil, stat, string, subprocess
def first_quot(line, isquotf):
if line[0] == "\"" and isquotf == 0:
return 1
return 0
def last_quot(line, isquotf):
if line[len(line) - 1] == '\"' and isquotf == 1:
return 1
return 0
def parse_command(command):
command = command.split(" ")
isquot = 0
res = ""
result = []
for i in range(len(command)):
if command[i] != "":
if first_quot(command[i], isquot) == 1:
isquot = 1
res = command[i]
continue
if last_quot(command[i], isquot) == 1:
isquot = 0
res += " " + command[i]
result.append(res)
continue
result.append(command[i])
print result
def exec_command(command):
result = []
try:
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while 1:
output = p.stdout.readline()
if not output:
break
if output.strip() != "":
result.append(output.strip())
except Exception, inst:
print "Call process error: " + str(inst)
return result
def exec_command_out(command):
os.system(command)
def exec_command_find_substring(command, substring):
result = exec_command(command)
for i in result:
if substring in i:
return i
return -1