首页 > 系统相关 >利用subprocess.run执行shell命令,并将结果日志写入文件

利用subprocess.run执行shell命令,并将结果日志写入文件

时间:2024-06-07 17:34:46浏览次数:16  
标签:shell run subprocess command result file output

将标准输出和标准错误输出都写入文件

import subprocess

def execute_shell_command(command, output_file):
    with open(output_file, 'w') as file:
        result = subprocess.run(command, stdout=file, stderr=subprocess.STDOUT,shell=True)
    print("Command return value:", result.returncode)

command = "bash /home/zcy/download/temp2.sh"
output_file = "output.log"

# 执行shell命令,并将标准输出和标准错误写入同一个文件
execute_shell_command(command, output_file)

将标准错误输出重定向到标准输出

import subprocess

def execute_shell_command(command):
    result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,shell=True)
    print("Command return value:", result.returncode)
    print(result.stdout.decode())

command = "bash /home/zcy/download/temp2.sh"
output_file = "output.log"

execute_shell_command(command)

 

标签:shell,run,subprocess,command,result,file,output
From: https://www.cnblogs.com/testzcy/p/18237591

相关文章

  • Shell脚本语言用法详解(超详细~)
    Shell目录Shell一、Shell是什么?二、Shell怎么使用?1.变量变量的命名和赋值变量类型变量的作用域只读变量删除变量环境变量系统预定义变量变量的使用特殊变量和位置参数2.运算符3.条件判断4.流程控制if判断case语句for循环while循环5.读取控制台输入6.函数系统函数自定......
  • 反弹shell不成功排查
    反弹不成功排查今天遇到shell反弹不成功的问题,顺便记下来0.低权限环境/tmp写入bash反弹脚本执行127.0.0.1|Echo‘bash-i>&/dev/tcp/x.x.x.x/77770>&1’>/tmp/1.sh127.0.0.1|bash/tmp/1.shphp反弹shell127.0.0.1|php-r'$sock=fsockopen("x.x.x.x",7777);exec......
  • Hershell反向shell生成器+msf加密通信免杀
    转自:https://www.cnblogs.com/Chuantouli/p/12298579.html简介 Hershell1Hershell(<ahref="github.com/sysdream/hershell" target="_blank" rel="noopener">github.com/sysdream/hershell</a>)是基于golang开发的一款反向shell生成......
  • shell脚本之证书到期监控和企微告警
    shell脚本之证书到期监控和企微告警shell脚本实现ssl证书过期及webhook推送脚本https.sh检测和告警脚本, https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxxxxxxx替换为自己企微的webhook地址即可#!/bin/bash#*******************************************......
  • [GDOUCTF 2023] Shellcode
    [GDOUCTF2023]Shellcoderet2shellcode|shellcodeint__fastcallmain(intargc,constchar**argv,constchar**envp){charbuf[10];//[rsp+6h][rbp-Ah]BYREFsetbuf(stdin,0LL);setbuf(stderr,0LL);setbuf(stdout,0LL);mprotect((&stdout......
  • MySQL Shell 使用指南
    前言:MySQLShell是官方提供的MySQL周边适配组件,是新一代的高级客户端,在MySQL8.0及其以后的版本得以慢慢推广应用。之前笔者因为MySQL8.0用得比较少,一直没有详细使用过这个工具,近期在捣鼓MySQL8.0,趁此机会,一起来学习下吧。1.MySQLShell介绍与安装使用MySQLShell......
  • 在 Powershell 管道中创建新对象
    我希望将某些文件递归复制到目标目录,但保持相同的文件夹结构。我希望能够执行以下操作:gci-Recurse-File*.csproj|rvpa-Relative|select{@{src=$_;dst=[System.IO.Path]::GetDirectoryName($baseDir+$_)}}}|%{mkdir$_.dst&&cp$_.src$_.dst}......
  • MySQL Shell 的简单使用
    util.dumpTables():导出表util.dumpSchemas():导出单个或多个schemautil.dumpInstance():导出整个实例 util.dumpTables()的使用语法:util.dumpTables(schema,tables,outputUrl[,options])其中:·schema:是表所在的schema·tables:是一个数组,表示要导出的表的列表·outp......
  • Xshell或其他命令行终端中,提示符(prompt)中的主机名太长,影响视觉体验或阅读方便性
    如果在Xshell或其他命令行终端中,你发现提示符(prompt)中的主机名太长,影响视觉体验或阅读方便性,你可以通过修改Linux系统的配置来缩短或美化这个提示符。这里有两种方法来解决这个问题:###1.暂时修改提示符你可以在当前终端会话中临时改变提示符,这不会影响其他用户或重启后的设置。......
  • CodeWars Shell Question
    CodeWarsShellQuestionClock#https://www.codewars.com/kata/55f9bca8ecaa9eac7100004ah=$1m=$2s=$3echo"(($h*60+$m)*60+$s)*1000"|bcEvenOrOdd#https://www.codewars.com/kata/53da3dbb4a5168369a0000feEvenOrOdd(){if(($1%2==0));then......