Skip to content

Instantly share code, notes, and snippets.

@MoElaSec
Created December 25, 2021 14:47
Show Gist options
  • Save MoElaSec/5268e6c52c9bb9c0f1f378f2e8c90eb4 to your computer and use it in GitHub Desktop.
Save MoElaSec/5268e6c52c9bb9c0f1f378f2e8c90eb4 to your computer and use it in GitHub Desktop.
Python OS and Subprocess: helpful to execute commands within Python scripts. {Not finished yet}

OS

clean the screen:

import os, platform

def c():
   p = platform.system()
   if p == "Windows":
    os.system("cls")
   else:
    os.system("clear")

Not really needed use in Linux: cmd/ctr + l in MacOS: cmd + K


Subprocess:

excute a command and capture output in a str:

import commands
status, output = commands.getstatusoutput("cat /etc/services")
import subprocess
output = subprocess.check_output("cat /etc/services", shell=True)

import subprocess

proc = subprocess.Popen(["cat", "/etc/services"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print "program output:", out
import subprocess

def exc(*cmd):
  if len(cmd) > 1:
    li = list(cmd)
  else:
    li = cmd
  proc = subprocess.Popen(li, stdout=subprocess.PIPE, shell=True)
  (out, err) = proc.communicate()
  print "program output:", out
  
def exc(*cmd):
  li = list(cmd)
  proc = subprocess.Popen(li, stdout=subprocess.PIPE, shell=True)
  (out, err) = proc.communicate()
  print "program output:", out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment