python远程执行linux命令unzip_【python】命令行调用SHELL

命令行调用SHELL

# -*- coding: utf-8 -*-

#!/usr/local/bin/python

import os, sys,time,datetime,time,subprocess,pdb,fnmatch

from threading import Thread

def commandline_invoke(command_list):

#stdin_value ="./" + command + " ".join(parameter_list)

stdin_value = command_list

child1 = subprocess.Popen(stdin_value, stdout=subprocess.PIPE)

#print "ExcuteCommand: " + " ".join(stdin_value)

#print "ExcuteResult: "

while True :

line = child1.stdout.readline()

if not line : break

print line

#这个方法在处理大量的返回的数据的时候,会出现异常。还是得用readline方式来操作。所以现在不建议用这种方式

def commandline_invoke_pipline_sunset(command_list):

#stdin_value ="./" + command + " ".join(parameter_list)

stdin_value = command_list

res = subprocess.check_output(stdin_value, shell=True)

#print "ExcuteCommand: " + " ".join(stdin_value)

#print "ExcuteResult: "

line = res.decode()

return line

def commandline_invoke_pipline(command_str):

stdin_value = command_str

child1 = subprocess.Popen(stdin_value, stdout=subprocess.PIPE,shell=True)

while True :

line = child1.stdout.readline()

if not line : break

print line

def get_dst_files(root,patterns='*' , single_level=False ,yieild_folders= False):

#将模式从字符串中取出放入列表中

patterns = patterns.split(';')

for path ,subdirs ,files in os.walk(root):

if yieild_folders :

files.extend(subdirs)

files.sort()

for name in files:

for pattern in patterns:

if fnmatch.fnmatch(name,pattern):

yield os.path.join(path,name)

break

if single_level:

break

def unzipfile(fileName):

#copy

print "copy fileName " + fileName

commandline_invoke(['cp','/Users/wenlezhou/Desktop/mk/source/' + fileName, '/Users/wenlezhou/Desktop/mk/dst/'])

#unzip

print "gunzip fileName " + fileName

commandline_invoke(['gunzip' , "./dst/" + fileName])

#word count status all

#pipline command line

str1 = './dst/' + fileName[:-3] + '|grep '

str2 = "'plugin=102'|grep "

str3 = "'V+8.0.0.1016.r403165' "

str4 = '''|wc'''

print 'cat '+ str1+ str2+str3+str4

wc = commandline_invoke_pipline('cat '+ str1+ str2+str3+str4)

word_count = fileName[:-3] +'\t' + wc

#write the result to file

fp = open("test_result_all","aw")

fp.writelines(word_count)

fp.flush()

fp.close()

#del file

delFileName = './dst/' + fileName[:-3]

print delFileName

commandline_invoke(['rm',delFileName])

def get_all_file_2():

fileList = []

search_path = './source/'

for path in get_dst_files(search_path,'*.gz',True):

fileList.append(path)

return fileList

def test_demo_cat_file_name_and_word_count():

fileNameList = get_all_file_2()

for fileName in fileNameList :

unzipfile(fileName[9:])

def test_pipline_command():

#test command line :find ./dst/* | xargs grep "V+8.0.0.1016.r403165"

commandLineDemo = 'find ./dst/* | xargs grep "V+8.0.0.1016.r403165"'

commandline_invoke_pipline(commandLineDemo)

def test_normal_command():

#test normal commandline : find ./dst/* -exec grep "V+8.0.0.1016.r403165" {} \;

commandLineDemo = 'find ./dst/* -exec grep "V+8.0.0.1016.r403165" {} \;'

commandline_invoke_pipline(commandLineDemo)

if __name__ == '__main__':

#test_pipline_command()

test_normal_command()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值