├── README.md └── xargswin.py /README.md: -------------------------------------------------------------------------------- 1 | # xargswin 2 | Minimalist Xargs for Windows 3 | 4 | Minimalist Xargs for Windows, currently following options 5 | are implemented: 6 | 7 | # Arguments 8 | -I replace_str 9 | -t, --verbose Verbose mode 10 | 11 | # Example Usage 12 | * Create str(s) to operate on and write to a file 13 | echo hi> test.txt 14 | echo there>>test.txt 15 | echo how?>>test.txt 16 | 17 | * Pipe the contents of the text file to the file to execute: 18 | type test.txt | xargswin.exe -I{} "echo {}" 19 | 20 | * Alternatively, query string does not need to be specified: 21 | type test.txt | xargswin.exe echo {} 22 | 23 | -------------------------------------------------------------------------------- /xargswin.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import sys 4 | import argparse 5 | import subprocess 6 | 7 | DESCRIPTION = """Minimalist Xargs for Windows, currently following options 8 | are implemented: 9 | 10 | -I replace_str 11 | -t, --verbose Verbose mode 12 | 13 | Example: 14 | * Create str(s) to operate on and write to a file 15 | echo hi> test.txt 16 | echo there>>test.txt 17 | echo how?>>test.txt 18 | 19 | * Pipe the contents of the text file to the file to execute: 20 | type test.txt | xargswin.exe -I{} "echo {}" 21 | 22 | * Alternatively, query string does not need to be specified: 23 | type test.txt | xargswin.exe echo {} 24 | """ 25 | 26 | def parse_user_args(): 27 | parser = argparse.ArgumentParser(description=DESCRIPTION, 28 | formatter_class=argparse.RawTextHelpFormatter) 29 | parser.add_argument("-I", dest="replace_str", action="store", 30 | help="replace-str") 31 | parser.add_argument("-t","--verbose", dest="verbose_mode", 32 | action="store_true", help="verbose mode") 33 | parser.add_argument(metavar="cmd with args", nargs="+", dest="cmd_with_args", 34 | action="store", help="command to execute with user arguments", 35 | type=str) 36 | args = parser.parse_args() 37 | replace_str = args.replace_str 38 | verbose_mode = args.verbose_mode 39 | cmd_with_args = args.cmd_with_args 40 | 41 | return (replace_str, verbose_mode, cmd_with_args) 42 | 43 | def validate_user_args(replace_str, verbose_mode, cmd_with_args): 44 | verbose_mode_validated = verbose_mode 45 | replace_str_validated = replace_str 46 | cmd_with_args_validated = ' '.join(cmd_with_args) 47 | 48 | return (replace_str_validated, verbose_mode_validated, cmd_with_args_validated) 49 | 50 | def apply_cmd(piped_input, replace_str, cmd_to_exec_template, verbose): 51 | for item in piped_input.split("\n"): 52 | if replace_str: 53 | cmd_to_exec = cmd_to_exec_template.replace(replace_str, item) 54 | else: 55 | cmd_to_exec = cmd_to_exec_template + " " + item 56 | 57 | try: 58 | p = subprocess.Popen(cmd_to_exec, stdout=subprocess.PIPE, 59 | stderr=subprocess.PIPE, shell=True) 60 | if verbose: 61 | print "[*] Executing '" + cmd_to_exec + "'" 62 | (out, err) = p.communicate() 63 | if err: 64 | print err.strip() 65 | if out: 66 | print out.strip() 67 | except WindowsError as e: 68 | print "[-] WindowsError: " + str(e) 69 | 70 | if __name__ == "__main__": 71 | 72 | # Parser user-provided arguments 73 | replace_str, verbose_mode, cmd_with_args = parse_user_args() 74 | 75 | # Take user input via pipe, or directly from stdin 76 | piped_input = sys.stdin.read().strip() 77 | 78 | # Perform any necessary filtering on user-provided arguments 79 | replace_str, verbose_mode, cmd_with_args = validate_user_args(replace_str, verbose_mode, 80 | cmd_with_args) 81 | 82 | # Apply the command on each line of the piped input 83 | apply_cmd(piped_input, replace_str, cmd_with_args, verbose_mode) 84 | 85 | --------------------------------------------------------------------------------